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
|
package services
import (
"fmt"
"sort"
"github.com/eugenetriguba/bolt/internal/models"
"github.com/eugenetriguba/bolt/internal/output"
"github.com/eugenetriguba/bolt/internal/repositories"
)
type MigrationService struct {
dbRepo *repositories.MigrationDBRepo
fsRepo *repositories.MigrationFsRepo
outputter output.Outputter
}
func NewMigrationService(
dbRepo *repositories.MigrationDBRepo,
fsRepo *repositories.MigrationFsRepo,
outputter output.Outputter,
) *MigrationService {
return &MigrationService{dbRepo: dbRepo, fsRepo: fsRepo, outputter: outputter}
}
type sortOrder int
const (
SortOrderDesc sortOrder = iota
SortOrderAsc
)
func (ms *MigrationService) ApplyAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
}
for _, migration := range migrations {
if !migration.Applied {
ms.ApplyMigration(migration)
}
}
return nil
}
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 {
return fmt.Errorf(
"migration with version %s is already applied, nothing to apply",
version,
)
}
for _, migration := range migrations {
if !migration.Applied {
err := ms.ApplyMigration(migration)
if err != nil {
return err
}
}
if migration.Version == version {
break
}
}
return nil
}
func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
ms.outputter.Output(
fmt.Sprintf(
"Applying migration %s_%s..",
migration.Version,
migration.Message,
),
)
scriptContents, err := ms.fsRepo.ReadUpgradeScript(migration)
if err != nil {
return err
}
err = ms.dbRepo.Apply(scriptContents, migration)
if err != nil {
return err
}
ms.outputter.Output(
fmt.Sprintf(
"Successfully applied migration %s_%s!",
migration.Version,
migration.Message,
),
)
return nil
}
func (ms *MigrationService) RevertAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderDesc)
if err != nil {
return err
}
for _, migration := range migrations {
if migration.Applied {
ms.RevertMigration(migration)
}
}
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 {
return fmt.Errorf(
"migration with version %s isn't applied, nothing to revert",
version,
)
}
for _, migration := range migrations {
if migration.Applied {
err := ms.RevertMigration(migration)
if err != nil {
return err
}
}
if migration.Version == version {
break
}
}
return nil
}
func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
ms.outputter.Output(
fmt.Sprintf(
"Reverting migration %s_%s..",
migration.Version,
migration.Message,
),
)
scriptContents, err := ms.fsRepo.ReadDowngradeScript(migration)
if err != nil {
return err
}
err = ms.dbRepo.Revert(scriptContents, migration)
if err != nil {
return err
}
ms.outputter.Output(
fmt.Sprintf(
"Successfully reverted migration %s_%s!",
migration.Version,
migration.Message,
),
)
return nil
}
func (ms *MigrationService) CreateMigration(migration *models.Migration) error {
err := ms.fsRepo.Create(migration)
if err != nil {
return err
}
ms.outputter.Output(
fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
)
return nil
}
func (ms *MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
// Assumption: All migration versions that have been applied
// to the database exist locally in the list of filesystem migrations.
// This would hold true unless someone decided to apply a migration and,
// for whatever reason, delete it later from their filesystem.
migrations, err := ms.fsRepo.List()
if err != nil {
return nil, err
}
appliedMigrations, err := ms.dbRepo.List()
if err != nil {
return nil, err
}
var flatMigrations []*models.Migration
for _, migration := range migrations {
_, ok := appliedMigrations[migration.Version]
if ok {
migration.Applied = true
}
flatMigrations = append(flatMigrations, migration)
}
sort.Slice(flatMigrations, func(i, j int) bool {
if order == SortOrderAsc {
return flatMigrations[i].Version < flatMigrations[j].Version
} else {
return flatMigrations[i].Version > flatMigrations[j].Version
}
})
return flatMigrations, nil
}
|