-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
111 lines (96 loc) · 3.33 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package persist
import (
"context"
"github.com/godzie44/d3/orm"
"github.com/godzie44/d3/orm/persistence"
"github.com/jackc/pgx/v4"
"github.com/stretchr/testify/assert"
)
func createSchema(db *pgx.Conn) error {
_, err := db.Exec(context.Background(), `CREATE TABLE IF NOT EXISTS shop_p(
id SERIAL PRIMARY KEY,
profile_id integer,
name character varying(200) NOT NULL
)`)
if err != nil {
return err
}
_, err = db.Exec(context.Background(), `CREATE TABLE IF NOT EXISTS profile_p(
id SERIAL PRIMARY KEY,
description character varying(1000) NOT NULL
)`)
if err != nil {
return err
}
_, err = db.Exec(context.Background(), `CREATE TABLE IF NOT EXISTS book_p(
id SERIAL PRIMARY KEY,
name text NOT NULL,
shop_id integer
)`)
if err != nil {
return err
}
_, err = db.Exec(context.Background(), `CREATE TABLE IF NOT EXISTS author_p(
id SERIAL PRIMARY KEY,
name character varying(200) NOT NULL
)`)
if err != nil {
return err
}
_, err = db.Exec(context.Background(), `CREATE TABLE IF NOT EXISTS book_author_p(
book_id integer NOT NULL,
author_id integer NOT NULL,
PRIMARY KEY (book_id,author_id)
)`)
return err
}
func deleteSchema(db *pgx.Conn) error {
_, err := db.Exec(context.Background(), `
DROP TABLE book_p;
DROP TABLE author_p;
DROP TABLE book_author_p;
DROP TABLE shop_p;
DROP TABLE profile_p;
`)
return err
}
func clearSchema(db *pgx.Conn) error {
_, err := db.Exec(context.Background(), `
delete from book_p;
delete from author_p;
delete from book_author_p;
delete from shop_p;
delete from profile_p;
`)
return err
}
func fillDb(assert *assert.Assertions, s orm.Driver) {
tx, err := s.BeginTx()
assert.NoError(err)
ps := s.MakePusher(tx)
err = ps.Insert("shop_p", []string{"id", "name", "profile_id"}, []interface{}{1001, "shop1", 1001}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("shop_p", []string{"id", "name", "profile_id"}, []interface{}{1002, "shop2", 1002}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("profile_p", []string{"id", "description"}, []interface{}{1001, "desc1"}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("profile_p", []string{"id", "description"}, []interface{}{1002, "desc2"}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("book_p", []string{"id", "shop_id", "name"}, []interface{}{1001, 1001, "book1"}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("book_p", []string{"id", "shop_id", "name"}, []interface{}{1002, 1001, "book2"}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("book_p", []string{"id", "shop_id", "name"}, []interface{}{1003, 1002, "book3"}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("author_p", []string{"id", "name"}, []interface{}{1001, "author1"}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("author_p", []string{"id", "name"}, []interface{}{1002, "author2"}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("book_author_p", []string{"book_id", "author_id"}, []interface{}{1001, 1001}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("book_author_p", []string{"book_id", "author_id"}, []interface{}{1002, 1001}, persistence.Undefined)
assert.NoError(err)
err = ps.Insert("book_author_p", []string{"book_id", "author_id"}, []interface{}{1002, 1002}, persistence.Undefined)
assert.NoError(err)
assert.NoError(tx.Commit())
}