-
Notifications
You must be signed in to change notification settings - Fork 5
/
goerd_test.go
141 lines (119 loc) · 2.72 KB
/
goerd_test.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package goerd_test
import (
"context"
_ "embed"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/covrom/goerd"
"github.com/covrom/goerd/schema"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
//go:embed shop.yaml
var shopSchemeYaml []byte
func TestBasicUsage(t *testing.T) {
if db == nil {
t.Fatal("run TestMain before")
return
}
targetSchema := &schema.Schema{}
if err := targetSchema.UnmarshalYAML(shopSchemeYaml); err != nil {
t.Error("targetSchema.UnmarshalYAML error:", err)
return
}
cats, err := NewCategorys(targetSchema)
if err != nil {
t.Error("NewCategorys error:", err)
return
}
prods, err := NewProducts(targetSchema)
if err != nil {
t.Error("NewProducts error:", err)
return
}
err = Migrate(db, targetSchema)
if err != nil {
t.Error("Migrate error:", err)
return
}
c := Category{
ID: uuid.New(),
Name: "category 1",
}
p := Product{
ID: uuid.New(),
CategoryID: c.ID,
Name: "product 1",
Code: "1000",
Unit: "pack",
}
ctx := goerd.WithSqlxDb(context.Background(), db)
if err := cats.CategoryToStore(ctx, c); err != nil {
t.Errorf("CategoryToStore error: %s", err)
return
}
if err := prods.ProductToStore(ctx, p); err != nil {
t.Errorf("ProductToStore error: %s", err)
return
}
ls, err := cats.ListCategoriesUpdatedFrom(ctx, time.Now().AddDate(-1, 0, 0))
if err != nil {
t.Errorf("cats.ListCategoriesUpdatedFrom error: %s", err)
return
}
if len(ls) != 1 {
t.Errorf("cats.ListCategoriesUpdatedFrom count != 1")
return
}
pids, err := prods.AllProductIDs(ctx)
if err != nil {
t.Errorf("prods.AllProductIDs error: %s", err)
return
}
if len(pids) != 1 {
t.Errorf("prods.AllProductIDs count != 1")
return
}
}
func Migrate(d *sqlx.DB, migsch *schema.Schema) error {
dbsch, err := goerd.SchemaFromPostgresDB(d.DB)
if err != nil {
return fmt.Errorf("cannot migrate database: %w", err)
}
qs, err := goerd.GenerateMigrationSQL(dbsch, migsch)
if err != nil {
return err
}
tx, err := d.Begin()
if err != nil {
return fmt.Errorf("cannot migrate database: %w", err)
}
for i, q := range qs {
// skip all dropping DDL queries
if strings.HasPrefix(strings.ToUpper(q), "DROP") {
fmt.Println(i+1, "skip: ", q)
continue
}
if strings.Contains(strings.ToUpper(q), "DROP COLUMN") {
fmt.Println(i+1, "skip: ", q)
continue
}
fmt.Println(i+1, q)
_, err = tx.Exec(q)
if err != nil {
_ = tx.Rollback()
fmt.Println("db schema:")
dbsch.SaveYaml(os.Stdout)
fmt.Println("target schema:")
migsch.SaveYaml(os.Stdout)
return fmt.Errorf("cannot migrate database %q: %w", q, err)
}
}
if err = tx.Commit(); err != nil {
return fmt.Errorf("cannot migrate database: %w", err)
}
return nil
}