-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathobjmodel.go
194 lines (159 loc) · 3.83 KB
/
objmodel.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package goerd
import (
"fmt"
"os"
"strings"
"github.com/covrom/goerd/schema"
"github.com/jmoiron/sqlx"
)
type ObjectField[T any] struct {
v *T
dbn string
c *schema.Column // nil == virtual
}
func (f *ObjectField[T]) dbName() string { return f.dbn }
func (f *ObjectField[T]) column() *schema.Column { return f.c }
func (f *ObjectField[T]) new() any { return new(T) }
func (f *ObjectField[T]) Column() *schema.Column { return f.c }
func Field[T any](column *schema.Column) *ObjectField[T] {
return &ObjectField[T]{
v: new(T),
dbn: column.Name,
c: column,
}
}
func FieldVirtual[T any](dbName string) *ObjectField[T] {
return &ObjectField[T]{
v: new(T),
dbn: dbName,
c: nil,
}
}
type ObjectModel[T any] struct {
v *T
fields map[string]objectField
tbls *schema.Table
}
func (m *ObjectModel[T]) objectModel() interface{} {
return m
}
func (m *ObjectModel[T]) Field(dbName string) objectField {
return m.fields[dbName]
}
func (m *ObjectModel[T]) SchemaTable() *schema.Table {
return m.tbls
}
func (m *ObjectModel[T]) WithType(t string) *ObjectModel[T] {
m.tbls.Type = t
return m
}
func (m *ObjectModel[T]) WithIndex(idxs ...*schema.Index) *ObjectModel[T] {
m.tbls.Indexes = append(m.tbls.Indexes, idxs...)
return m
}
func (m *ObjectModel[T]) WithConstraint(ctrs ...*schema.Constraint) *ObjectModel[T] {
m.tbls.Constraints = append(m.tbls.Constraints, ctrs...)
return m
}
func (m *ObjectModel[T]) WithComment(c string) *ObjectModel[T] {
m.tbls.Comment = c
return m
}
func (m *ObjectModel[T]) WithDef(def string) *ObjectModel[T] {
m.tbls.Def = def
return m
}
func (m *ObjectModel[T]) New() *T {
return new(T)
}
type objectField interface {
dbName() string
column() *schema.Column
new() any
}
var _ objectField = &ObjectField[int]{}
func Model[T any](table string, fields ...objectField) *ObjectModel[T] {
t := &schema.Table{
Name: table,
}
fmap := make(map[string]objectField, len(fields))
for _, fld := range fields {
if fc := fld.column(); fc != nil {
t.Columns = append(t.Columns, fc)
}
fmap[fld.dbName()] = fld
}
ret := &ObjectModel[T]{
v: new(T),
fields: fmap,
tbls: t,
}
return ret
}
type objectModel interface {
objectModel() interface{}
SchemaTable() *schema.Table
}
var _ objectModel = &ObjectModel[struct{}]{}
type ModelSet struct {
ms []objectModel
rels []*schema.Relation
}
func NewModelSet(mds ...objectModel) *ModelSet {
return &ModelSet{
ms: mds,
}
}
func (md *ModelSet) WithRelations(rels ...*schema.Relation) *ModelSet {
md.rels = append(md.rels, rels...)
return md
}
func (md *ModelSet) Migrate(d *sqlx.DB, dbSchema string) error {
migsch := &schema.Schema{
CurrentSchema: dbSchema,
Relations: md.rels,
}
for _, m := range md.ms {
migsch.Tables = append(migsch.Tables, m.SchemaTable())
}
if err := migsch.Repair(); err != nil {
return err
}
dbsch, err := SchemaFromPostgresDB(d.DB)
if err != nil {
return fmt.Errorf("cannot migrate database: %w", err)
}
qs, err := GenerateMigrationSQL(dbsch, migsch)
if err != nil {
return fmt.Errorf("cannot migrate database: %w", 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: %w", err)
}
}
if err = tx.Commit(); err != nil {
return fmt.Errorf("cannot migrate database: %w", err)
}
return nil
}