/
migrations.go
322 lines (274 loc) · 7.92 KB
/
migrations.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package golembic
import (
"fmt"
"sync"
)
// NOTE: Ensure that
// * `Migrations.Since` satisfies `migrationsFilter`.
// * `Migrations.Until` satisfies `migrationsFilter`.
var (
_ migrationsFilter = (*Migrations)(nil).Since
_ migrationsFilter = (*Migrations)(nil).Until
)
// Migrations represents a sequence of migrations to be applied.
type Migrations struct {
sequence map[string]Migration
lock sync.Mutex
}
// NewSequence creates a new sequence of migrations rooted in a single
// base / root migration.
func NewSequence(root Migration) (*Migrations, error) {
if root.Previous != "" {
err := fmt.Errorf(
"%w; previous: %q, revision: %q",
ErrNotRoot, root.Previous, root.Revision,
)
return nil, err
}
if root.Revision == "" {
return nil, ErrMissingRevision
}
m := &Migrations{
sequence: map[string]Migration{
root.Revision: root,
},
lock: sync.Mutex{},
}
return m, nil
}
// Register adds a new migration to an existing sequence of migrations, if
// possible. The new migration must have a previous migration and have a valid
// revision that is not already registered.
func (m *Migrations) Register(migration Migration) error {
m.lock.Lock()
defer m.lock.Unlock()
if migration.Previous == "" {
return fmt.Errorf("%w; revision: %q", ErrNoPrevious, migration.Revision)
}
if _, ok := m.sequence[migration.Previous]; !ok {
return fmt.Errorf(
"%w; revision: %q, previous: %q",
ErrPreviousNotRegistered, migration.Revision, migration.Previous,
)
}
if migration.Revision == "" {
return fmt.Errorf("%w; previous: %q", ErrMissingRevision, migration.Previous)
}
if _, ok := m.sequence[migration.Revision]; ok {
return fmt.Errorf("%w; revision: %q", ErrAlreadyRegistered, migration.Revision)
}
// NOTE: This crucially relies on `m.sequence` being locked.
migration.serialID = uint32(len(m.sequence))
m.sequence[migration.Revision] = migration
return nil
}
// RegisterMany attempts to register multiple migrations (in order) with an
// existing sequence.
func (m *Migrations) RegisterMany(ms ...Migration) error {
for _, migration := range ms {
err := m.Register(migration)
if err != nil {
return err
}
}
return nil
}
// RegisterManyOpt attempts to register multiple migrations (in order) with an
// existing sequence. It differs from `RegisterMany()` in that the construction
// of `Migration` objects is handled directly here by taking a slice of
// option slices.
func (m *Migrations) RegisterManyOpt(manyOpts ...[]MigrationOption) error {
for _, opts := range manyOpts {
migration, err := NewMigration(opts...)
if err != nil {
return err
}
err = m.Register(*migration)
if err != nil {
return err
}
}
return nil
}
// Root does a linear scan of every migration in the sequence and returns
// the root migration. In the "general" case such a scan would be expensive, but
// the number of migrations should always be a small number.
//
// NOTE: This does not verify or enforce the invariant that there must be
// exactly one migration without a previous migration. This invariant is enforced
// by the exported methods such as `Register()` and `RegisterMany()` and the
// constructor `NewSequence()`.
func (m *Migrations) Root() Migration {
m.lock.Lock()
defer m.lock.Unlock()
for _, migration := range m.sequence {
if migration.Previous == "" {
return migration
}
}
return Migration{}
}
// All produces the migrations in the sequence, in order.
//
// NOTE: This does not verify or enforce the invariant that there must be
// exactly one migration without a previous migration. This invariant is
// enforced by the exported methods such as `Register()` and
// `RegisterMany()` and the constructor `NewSequence()`.
func (m *Migrations) All() []Migration {
root := m.Root()
m.lock.Lock()
defer m.lock.Unlock()
result := []Migration{root}
// Find the unique revision (without validation) that points at the
// current `previous`.
previous := root.Revision
for i := 0; i < len(m.sequence)-1; i++ {
for _, migration := range m.sequence {
if migration.Previous != previous {
continue
}
result = append(result, migration)
previous = migration.Revision
break
}
}
return result
}
// Since returns the migrations that occur **after** `revision`.
//
// This utilizes `All()` and returns all migrations after the one that
// matches `revision`. If none match, an error will be returned. If
// `revision` is the **last** migration, the migrations returned will be an
// empty slice.
func (m *Migrations) Since(revision string) (int, []Migration, error) {
all := m.All()
found := false
result := []Migration{}
pastMigrationCount := 0
for _, migration := range all {
if found {
result = append(result, migration)
continue
}
pastMigrationCount++
if migration.Revision == revision {
found = true
}
}
if !found {
err := fmt.Errorf("%w; revision: %q", ErrMigrationNotRegistered, revision)
return 0, nil, err
}
return pastMigrationCount, result, nil
}
// Until returns the migrations that occur **before** `revision`.
//
// This utilizes `All()` and returns all migrations up to and including the one
// that matches `revision`. If none match, an error will be returned.
func (m *Migrations) Until(revision string) (int, []Migration, error) {
all := m.All()
found := false
result := []Migration{}
for _, migration := range all {
result = append(result, migration)
if migration.Revision == revision {
found = true
break
}
}
if !found {
err := fmt.Errorf("%w; revision: %q", ErrMigrationNotRegistered, revision)
return 0, nil, err
}
// I.e. we are not filtering any migrations from the beginning of the
// sequence.
pastMigrationCount := 0
return pastMigrationCount, result, nil
}
// Between returns the migrations that occur between two revisions.
//
// This can be seen as a combination of `Since()` and `Until()`.
func (m *Migrations) Between(since, until string) (int, []Migration, error) {
all := m.All()
foundSince := false
foundUntil := false
result := []Migration{}
pastMigrationCount := 0
for _, migration := range all {
if foundSince {
if foundUntil {
break
}
result = append(result, migration)
}
pastMigrationCount++
if migration.Revision == since {
foundSince = true
}
if migration.Revision == until {
foundUntil = true
}
}
if !foundSince {
err := fmt.Errorf("%w; revision: %q", ErrMigrationNotRegistered, since)
return 0, nil, err
}
if !foundUntil {
err := fmt.Errorf("%w; revision: %q", ErrMigrationNotRegistered, until)
return 0, nil, err
}
return pastMigrationCount, result, nil
}
// Revisions produces the revisions in the sequence, in order.
//
// This utilizes `All()` and just extracts the revisions.
func (m *Migrations) Revisions() []string {
result := []string{}
for _, migration := range m.All() {
result = append(result, migration.Revision)
}
return result
}
type describeMetadata struct {
Revision string
Description string
}
// Describe displays all of the registered migrations (with descriptions).
func (m *Migrations) Describe(log PrintfReceiver) {
revisions := m.Revisions()
m.lock.Lock()
defer m.lock.Unlock()
dms := []describeMetadata{}
revisionWidth := 0
for _, revision := range revisions {
migration := m.sequence[revision]
dms = append(
dms,
describeMetadata{
Revision: revision,
Description: migration.ExtendedDescription(),
},
)
if len(revision) > revisionWidth {
revisionWidth = len(revision)
}
}
indexWidth := len(fmt.Sprintf("%d", len(dms)-1))
format := ("%" + fmt.Sprintf("%d", indexWidth) + "d " +
"| %" + fmt.Sprintf("%d", revisionWidth) + "s " +
"| %s")
for i, dm := range dms {
log.Printf(format, i, dm.Revision, dm.Description)
}
}
// Get retrieves a revision from the sequence, if present. If not, returns
// `nil`.
func (m *Migrations) Get(revision string) *Migration {
m.lock.Lock()
defer m.lock.Unlock()
migration, ok := m.sequence[revision]
if ok {
return &migration
}
return nil
}