-
Notifications
You must be signed in to change notification settings - Fork 232
/
migrate.go
268 lines (226 loc) · 7.02 KB
/
migrate.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package schema
type (
// A Change represents a schema change. The types below implement this
// interface and can be used for describing schema changes.
//
// The Change interface can also be implemented outside this package
// as follows:
//
// type RenameType struct {
// schema.Change
// From, To string
// }
//
// var t schema.Change = &RenameType{From: "old", To: "new"}
//
Change interface {
change()
}
// Clause carries additional information that can be added
// to schema changes. The Clause interface can be implemented
// outside this package as follows:
//
// type Authorization struct {
// schema.Clause
// UserName string
// }
//
// var c schema.Clause = &Authorization{UserName: "a8m"}
//
Clause interface {
clause()
}
// AddSchema describes a schema (named database) creation change.
// Unlike table creation, schemas and their elements are described
// with separate changes. For example, "AddSchema" and "AddTable"
AddSchema struct {
S *Schema
Extra []Clause // Extra clauses and options.
}
// DropSchema describes a schema (named database) removal change.
DropSchema struct {
S *Schema
Extra []Clause // Extra clauses and options.
}
// ModifySchema describes a modification change for schema attributes.
ModifySchema struct {
S *Schema
Changes []Change
}
// AddTable describes a table creation change.
AddTable struct {
T *Table
Extra []Clause // Extra clauses and options.
}
// DropTable describes a table removal change.
DropTable struct {
T *Table
Extra []Clause // Extra clauses.
}
// ModifyTable describes a table modification change.
ModifyTable struct {
T *Table
Changes []Change
}
// AddColumn describes a column creation change.
AddColumn struct {
C *Column
}
// DropColumn describes a column removal change.
DropColumn struct {
C *Column
}
// ModifyColumn describes a change that modifies a column.
ModifyColumn struct {
From, To *Column
Change ChangeKind
}
// AddIndex describes an index creation change.
AddIndex struct {
I *Index
}
// DropIndex describes an index removal change.
DropIndex struct {
I *Index
}
// ModifyIndex describes an index modification.
ModifyIndex struct {
From, To *Index
Change ChangeKind
}
// AddForeignKey describes a foreign-key creation change.
AddForeignKey struct {
F *ForeignKey
}
// DropForeignKey describes a foreign-key removal change.
DropForeignKey struct {
F *ForeignKey
}
// ModifyForeignKey describes a change that modifies a foreign-key.
ModifyForeignKey struct {
From, To *ForeignKey
Change ChangeKind
}
// AddCheck describes a CHECK constraint creation change.
AddCheck struct {
C *Check
}
// DropCheck describes a CHECK constraint removal change.
DropCheck struct {
C *Check
}
// ModifyCheck describes a change that modifies a check.
ModifyCheck struct {
From, To *Check
Change ChangeKind
}
// AddAttr describes an attribute addition.
AddAttr struct {
A Attr
}
// DropAttr describes an attribute removal.
DropAttr struct {
A Attr
}
// ModifyAttr describes a change that modifies an element attribute.
ModifyAttr struct {
From, To Attr
}
// IfExists represents a clause in a schema change that is commonly
// supported by multiple statements (e.g. DROP TABLE or DROP SCHEMA).
IfExists struct{}
// IfNotExists represents a clause in a schema change that is commonly
// supported by multiple statements (e.g. CREATE TABLE or CREATE SCHEMA).
IfNotExists struct{}
)
// A ChangeKind describes a change kind that can be combined
// using a set of flags. The zero kind is no change.
type ChangeKind uint
const (
// NoChange holds the zero value of a change kind.
NoChange ChangeKind = 0
// Common changes.
// ChangeAttr describes attributes change of an element.
// For example, a table CHECK was added or changed.
ChangeAttr ChangeKind = 1 << (iota - 1)
// ChangeCharset describes character-set change.
ChangeCharset
// ChangeCollation describes collation/encoding change.
ChangeCollation
// ChangeComment describes comment chang (of any element).
ChangeComment
// Column specific changes.
// ChangeNull describe a change to the NULL constraint.
ChangeNull
// ChangeType describe a column type change.
ChangeType
// ChangeDefault describe a column default change.
ChangeDefault
// Index specific changes.
// ChangeUnique describes a change to the uniqueness constraint.
// For example, an index was changed from non-unique to unique.
ChangeUnique
// ChangeParts describes a change to one or more of the index parts.
// For example, index keeps its previous name, but the columns order
// was changed.
ChangeParts
// Foreign key specific changes.
// ChangeColumn describes a change to the foreign-key (child) columns.
ChangeColumn
// ChangeRefColumn describes a change to the foreign-key (parent) columns.
ChangeRefColumn
// ChangeRefTable describes a change to the foreign-key (parent) table.
ChangeRefTable
// ChangeUpdateAction describes a change to the foreign-key update action.
ChangeUpdateAction
// ChangeDeleteAction describes a change to the foreign-key delete action.
ChangeDeleteAction
)
// Is reports whether c is match the given change kind.
func (k ChangeKind) Is(c ChangeKind) bool {
return k == c || k&c != 0
}
// Differ is the interface implemented by the different
// drivers for comparing and diffing schema top elements.
type Differ interface {
// RealmDiff returns a diff report for migrating a realm
// (or a database) from state "from" to state "to". An error
// is returned if such step is not possible.
RealmDiff(from, to *Realm) ([]Change, error)
// SchemaDiff returns a diff report for migrating a schema
// from state "from" to state "to". An error is returned
// if such step is not possible.
SchemaDiff(from, to *Schema) ([]Change, error)
// TableDiff returns a diff report for migrating a table
// from state "from" to state "to". An error is returned
// if such step is not possible.
TableDiff(from, to *Table) ([]Change, error)
}
// changes.
func (*AddAttr) change() {}
func (*DropAttr) change() {}
func (*ModifyAttr) change() {}
func (*AddSchema) change() {}
func (*DropSchema) change() {}
func (*ModifySchema) change() {}
func (*AddTable) change() {}
func (*DropTable) change() {}
func (*ModifyTable) change() {}
func (*AddIndex) change() {}
func (*DropIndex) change() {}
func (*ModifyIndex) change() {}
func (*AddCheck) change() {}
func (*DropCheck) change() {}
func (*ModifyCheck) change() {}
func (*AddColumn) change() {}
func (*DropColumn) change() {}
func (*ModifyColumn) change() {}
func (*AddForeignKey) change() {}
func (*DropForeignKey) change() {}
func (*ModifyForeignKey) change() {}
// clauses.
func (*IfExists) clause() {}
func (*IfNotExists) clause() {}