forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_extended.go
More file actions
337 lines (276 loc) · 9.87 KB
/
migrate_extended.go
File metadata and controls
337 lines (276 loc) · 9.87 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package migrate
import (
"errors"
"fmt"
"os"
"time"
"github.com/abramad-labs/histomigrate/database"
)
// DoMigration executes a single database migration.
// It acquires a lock, checks if the migration is already applied, queues it for processing (if not applied), runs it, and then releases the lock.
// It requires an ExtendedDriver.
func (m *Migrate) DoMigration(version uint) error {
if err := m.lock(); err != nil {
return err
}
ret := make(chan interface{}, m.PrefetchMigrations)
ed, isExtended := m.databaseDrv.(database.ExtendedDriver)
if isExtended {
isApplied, err := ed.IsMigrationApplied(version)
if err != nil {
return m.unlockErr(err)
}
if isApplied {
return m.unlockErr(ErrNoChange)
}
go m.queueUpSingleMigration(version, ret)
} else {
return m.unlockErr(errors.New("driver type is not right"))
}
return m.unlockErr(m.runMigrations(ret))
}
// UndoMigration rolls back a specific database migration.
// It acquires a lock, confirms the migration is currently applied (returning ErrNoChange if not), then queues and runs the "down" migration.
// It requires an ExtendedDriver.
func (m *Migrate) UndoMigration(version uint) error {
if err := m.lock(); err != nil {
return err
}
ret := make(chan interface{}, m.PrefetchMigrations)
ed, isExtended := m.databaseDrv.(database.ExtendedDriver)
if isExtended {
isApplied, err := ed.IsMigrationApplied(version)
if err != nil {
return m.unlockErr(err)
}
if !isApplied {
return m.unlockErr(ErrNoChange)
}
go m.queueDownSingleMigration(version, ret)
} else {
return m.unlockErr(errors.New("driver type is not right"))
}
return m.unlockErr(m.runMigrations(ret))
}
// queueUpMigrations function is responsible for identifying and preparing "up" (forward) migrations that need to be applied.
// It starts by determining the first available migration from a sourceDrv (source driver, likely a file system or similar).
// It then iterates through subsequent migrations, skipping any that have already been applied (as indicated by the appliedMigrs list).
// For each unapplied migration, it creates a Migration object, marks it as an "up" migration, and sends it to the ret channel for further processing.
// The function also asynchronously buffers the migration's content in a separate goroutine.
// It respects a limit on the number of new migrations to queue and can be stopped gracefully.
// If no new migrations are found or queued (and no background errors occur), it signals ErrNoChange.
func (m *Migrate) queueUpMigrations(appliedMigrs []int, limit int, ret chan<- interface{}) {
defer close(ret)
appliedSet := make(map[int]struct{}, len(appliedMigrs))
for _, v := range appliedMigrs {
appliedSet[v] = struct{}{}
}
targetVersion, err := m.sourceDrv.First()
if errors.Is(err, os.ErrNotExist) {
ret <- ErrNoChange
return
}
if err != nil {
ret <- err
return
}
appliedCount := 0
for limit == -1 || appliedCount < limit {
if m.stop() {
break
}
if _, ok := appliedSet[int(targetVersion)]; ok {
targetVersion, err = m.sourceDrv.Next(targetVersion)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
break
}
ret <- err
return
}
continue
}
appliedCount++
migr, err := m.newMigration(targetVersion, int(targetVersion))
if err != nil {
ret <- err
return
}
migr.UpKindMigration = true
ret <- migr
go func(migr *Migration) {
if err := migr.Buffer(); err != nil {
m.logErr(err)
}
}(migr)
targetVersion, err = m.sourceDrv.Next(targetVersion)
if errors.Is(err, os.ErrNotExist) {
break
}
if err != nil {
ret <- err
return
}
}
if appliedCount == 0 {
ret <- ErrNoChange
}
}
// queueUpSingleMigration finds and buffers the specified "up" migration.
// It assumes that applied migrations are already filtered out before calling.
// It sends either the prepared migration or an error to the provided channel.
func (m *Migrate) queueUpSingleMigration(version uint, ret chan<- interface{}) {
defer close(ret)
if err := m.versionExists(version); err != nil {
ret <- err
return
}
if m.stop() {
return
}
migr, err := m.newMigration(version, int(version))
if err != nil {
ret <- err
return
}
migr.UpKindMigration = true
ret <- migr
go func(migr *Migration) {
if err := migr.Buffer(); err != nil {
m.logErr(err)
}
}(migr)
}
// queueDownMigrations iterates through a provided list of applied migrations (assumed to be in descending order of version),
// preparing them for "down" (rollback) operations.
// For each migration, it determines the target version (which would be the previous version in the sequence, or -1 if it's the oldest migration), creates a Migration object, sends it to a channel for processing,
// and asynchronously buffers its content. The function respects a limit on the number of migrations to process and can be stopped gracefully.
// If no migrations are found or processed (and no background errors occur), it signals ErrNoChange.
func (m *Migrate) queueDownMigrations(appliedMigrs []int, limit int, ret chan<- interface{}) {
defer close(ret)
if len(appliedMigrs) == 0 || limit == 0 {
ret <- ErrNoChange
return
}
appliedCount := 0
for i := 0; i < len(appliedMigrs); i++ {
if m.stop() {
break
}
if limit != -1 && appliedCount >= limit {
break
}
version := uint(appliedMigrs[i])
var targetVersion int
if i == len(appliedMigrs)-1 {
targetVersion = -1
} else {
targetVersion = appliedMigrs[i+1]
}
appliedCount++
migr, err := m.newMigration(version, targetVersion)
if err != nil {
ret <- err
return
}
ret <- migr
go func(migr *Migration) {
if err := migr.Buffer(); err != nil {
m.logErr(err)
}
}(migr)
}
if appliedCount == 0 {
ret <- ErrNoChange
}
}
// queueDownSingleMigration finds and buffers the specified "down" migration.
// It calculates the target version (previous version) for the rollback.
// It sends either the prepared migration or an error to the provided channel.
func (m *Migrate) queueDownSingleMigration(version uint, ret chan<- interface{}) {
defer close(ret)
if err := m.versionExists(version); err != nil {
ret <- err
return
}
if m.stop() {
return
}
targetVersion := -1
prev, err := m.sourceDrv.Prev(version)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
ret <- err
return
}
} else {
targetVersion = int(prev)
}
migr, err := m.newMigration(version, targetVersion)
if err != nil {
ret <- err
return
}
ret <- migr
go func(migr *Migration) {
if err := migr.Buffer(); err != nil {
m.logErr(err)
}
}(migr)
}
// handleSingleMigration function is a private helper responsible for executing a single database migration. It takes a `*Migration` object and performs the following steps:
// 1. Pre-Migration State Management: It first interacts with the database driver to set the migration's state to "dirty" (or "in-progress") before running its script. If the driver implements `database.ExtendedDriver`, it uses specific methods like `AddDirtyMigration` for "up" migrations or `UpdateMigrationDirtyFlag(..., true)` for "down" migrations. Otherwise, it defaults to `SetVersion(..., true)`.
// 2. Execute Migration Body: If the migration contains a script (`migr.Body` is not nil), it logs the execution and then runs the migration's `BufferedBody` (the actual SQL or code) against the database using the driver's `Run` method.
// 3. Post-Migration State Management: After successful execution of the body, it updates the migration's status to "clean" or "applied." If using an `ExtendedDriver`, it calls `UpdateMigrationDirtyFlag(..., false)` for "up" migrations or `RemoveMigration` for "down" migrations. For basic drivers, it calls `SetVersion(..., false)`.
// 4. Logging Timings: Finally, it calculates and logs the time taken for buffering and running the migration, providing insights into performance.
// The function handles errors at each step, wrapping them with contextual information to indicate exactly where the failure occurred. It relies on the `m.databaseDrv` (which can be `database.ExtendedDriver` or a simpler `BasicDriver`) to interact with the underlying database.
func (m *Migrate) handleSingleMigration(migr *Migration) error {
ed, isExtended := m.databaseDrv.(database.ExtendedDriver)
if isExtended {
if migr.UpKindMigration {
if err := ed.AddDirtyMigration(migr.Version); err != nil {
return fmt.Errorf("failed to add dirty migration for version %d: %w", migr.Version, err)
}
} else {
if err := ed.UpdateMigrationDirtyFlag(migr.Version, true); err != nil {
return fmt.Errorf("failed to set dirty flag for version %d: %w", migr.Version, err)
}
}
} else {
if err := m.databaseDrv.SetVersion(migr.TargetVersion, true); err != nil {
return fmt.Errorf("failed to set dirty version %d: %w", migr.TargetVersion, err)
}
}
if migr.Body != nil {
m.logVerbosePrintf("Read and execute %v\n", migr.LogString())
if err := m.databaseDrv.Run(migr.BufferedBody); err != nil {
return fmt.Errorf("failed to run migration %d body: %w", migr.Version, err)
}
}
if isExtended {
if migr.UpKindMigration {
if err := ed.UpdateMigrationDirtyFlag(migr.Version, false); err != nil {
return fmt.Errorf("failed to clear dirty flag for version %d: %w", migr.Version, err)
}
} else {
if err := ed.RemoveMigration(migr.Version); err != nil {
return fmt.Errorf("failed to remove migration for version %d: %w", migr.Version, err)
}
}
} else {
if err := m.databaseDrv.SetVersion(migr.TargetVersion, false); err != nil {
return fmt.Errorf("failed to set clean version %d: %w", migr.TargetVersion, err)
}
}
endTime := time.Now()
readTime := migr.FinishedReading.Sub(migr.StartedBuffering)
runTime := endTime.Sub(migr.FinishedReading)
if m.Log != nil {
if m.Log.Verbose() {
m.logPrintf("Finished %v (read %v, ran %v)\n", migr.LogString(), readTime, runTime)
} else {
m.logPrintf("%v (%v)\n", migr.LogString(), readTime+runTime)
}
}
return nil
}