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
|
package services
import (
"fmt"
"strconv"
"time"
"git.sr.ht/~eugenetriguba/bolt/internal/configloader"
"git.sr.ht/~eugenetriguba/bolt/internal/models"
"git.sr.ht/~eugenetriguba/bolt/internal/output"
"git.sr.ht/~eugenetriguba/bolt/internal/repositories"
)
// MigrationService provides the primary interface for migration operations.
// It coordinates with the lower-level components to apply, revert, create,
// and list out migrations while also managing presentation concerns like
// output and timing.
type MigrationService struct {
executor *MigrationExecutor
queryService *MigrationQueryService
depGraph *MigrationDependencyGraph
fsRepo repositories.MigrationFsRepo
cfg configloader.Config
outputter output.Outputter
}
func NewMigrationService(
dbRepo repositories.MigrationDBRepo,
fsRepo repositories.MigrationFsRepo,
cfg configloader.Config,
outputter output.Outputter,
) MigrationService {
depGraph := NewMigrationDependencyGraph(cfg)
executor := NewMigrationExecutor(dbRepo, fsRepo)
queryService := NewMigrationQueryService(dbRepo, fsRepo, depGraph)
return MigrationService{
executor: executor,
queryService: queryService,
depGraph: depGraph,
fsRepo: fsRepo,
cfg: cfg,
outputter: outputter,
}
}
func (ms MigrationService) ApplyAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
}
migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if !migration.Applied {
if err := ms.depGraph.CheckDependenciesApplied(migration, migrationsByVersion); err != nil {
return err
}
err := ms.ApplyMigration(migration)
if err != nil {
return fmt.Errorf(
"unable to apply migration %s: %w",
migration.Name(),
err,
)
}
}
}
return nil
}
func getMigrationsByVersion(migrations []*models.Migration) map[string]*models.Migration {
result := make(map[string]*models.Migration)
for _, migration := range migrations {
result[migration.Version] = migration
}
return result
}
func (ms MigrationService) ApplyUpToVersion(version string) error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
}
var targetMigration *models.Migration
for _, migration := range migrations {
if migration.Version == version {
targetMigration = migration
break
}
}
if targetMigration == nil {
return fmt.Errorf("migration with version %s does not exist", version)
}
if targetMigration.Applied {
// TODO: Check not only that the target migration is applied, but also
// all prior migrations.
return fmt.Errorf(
"migration with version %s is already applied, nothing to apply",
version,
)
}
migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if !migration.Applied {
if err := ms.depGraph.CheckDependenciesApplied(migration, migrationsByVersion); err != nil {
return err
}
err := ms.ApplyMigration(migration)
if err != nil {
return fmt.Errorf(
"unable to apply migration %s: %w",
migration.Name(),
err,
)
}
}
if migration.Version == version {
break
}
}
return nil
}
func (ms MigrationService) ApplyMigration(migration *models.Migration) error {
ms.outputter.Output(fmt.Sprintf("Applying migration %s..", migration.Name()))
startTime := time.Now()
err := ms.executor.Apply(migration)
if err != nil {
return err
}
ms.outputter.Output(
fmt.Sprintf(
"Successfully applied migration %s in %s!",
migration.Name(),
time.Since(startTime),
),
)
return nil
}
func (ms MigrationService) RevertAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderDesc)
if err != nil {
return err
}
migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if migration.Applied {
if err := ms.depGraph.CheckNoDependentsApplied(migration, migrationsByVersion); err != nil {
return err
}
err := ms.RevertMigration(migration)
if err != nil {
return fmt.Errorf(
"unable to revert migration %s: %w",
migration.Name(),
err,
)
}
}
}
return nil
}
func (ms MigrationService) RevertDownToVersion(version string) error {
migrations, err := ms.ListMigrations(SortOrderDesc)
if err != nil {
return err
}
var targetMigration *models.Migration
for _, migration := range migrations {
if migration.Version == version {
targetMigration = migration
break
}
}
if targetMigration == nil {
return fmt.Errorf("migration with version %s does not exist", version)
}
if !targetMigration.Applied {
// TODO: Check not only that the target migration is reverted, but also
// all prior migrations.
return fmt.Errorf(
"migration with version %s isn't applied, nothing to revert",
version,
)
}
migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if migration.Applied {
if err := ms.depGraph.CheckNoDependentsApplied(migration, migrationsByVersion); err != nil {
return err
}
err := ms.RevertMigration(migration)
if err != nil {
return fmt.Errorf(
"unable to revert migration %s: %w",
migration.Name(),
err,
)
}
}
if migration.Version == version {
break
}
}
return nil
}
func (ms MigrationService) RevertMigration(migration *models.Migration) error {
ms.outputter.Output(fmt.Sprintf("Reverting migration %s..", migration.Name()))
startTime := time.Now()
err := ms.executor.Revert(migration)
if err != nil {
return err
}
ms.outputter.Output(
fmt.Sprintf(
"Successfully reverted migration %s in %s!",
migration.Name(),
time.Since(startTime),
),
)
return nil
}
func (ms MigrationService) CreateMigration(message string) (*models.Migration, error) {
var migration *models.Migration
if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleTimestamp {
migration = models.NewTimestampMigration(time.Now(), message)
} else {
currentVersion, err := ms.getCurrentSequentialMigrationVersion()
if err != nil {
return nil, err
}
migration = models.NewSequentialMigration(currentVersion+1, message)
}
leafNodes, err := ms.getMigrationLeafNodes()
if err != nil {
return nil, err
}
migration.Dependencies = leafNodes
err = ms.fsRepo.Create(migration)
if err != nil {
return nil, err
}
ms.outputter.Output(
fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
)
return migration, nil
}
func (ms MigrationService) getMigrationLeafNodes() ([]string, error) {
localMigrations, err := ms.fsRepo.List()
if err != nil {
return nil, fmt.Errorf("unable to list migrations for finding leaf nodes: %w", err)
}
leafNodes, err := ms.depGraph.FindLeafNodes(localMigrations)
if err != nil {
return nil, fmt.Errorf("unable to find leaf nodes for dependencies: %w", err)
}
return leafNodes, nil
}
func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
var maxVersion uint64 = 0
localMigrations, err := ms.fsRepo.List()
if err != nil {
return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
}
for _, migration := range localMigrations {
version, err := strconv.ParseUint(migration.Version, 10, 64)
if err != nil {
return 0, fmt.Errorf(
"migration %s has invalid version for sequential style: %w",
migration.Name(),
err,
)
}
if version > maxVersion {
maxVersion = version
}
}
return maxVersion, nil
}
func (ms MigrationService) ListMigrations(order SortOrder) ([]*models.Migration, error) {
return ms.queryService.ListMigrations(order)
}
|