-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmigrator.go
312 lines (242 loc) · 6.8 KB
/
migrator.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
// Package migrator represents MySQL database migrator
//
// MySQL database migrator designed to run migrations to your features and manage database schema update with intuitive go code.
// It is compatible with the latest MySQL v8.
package migrator
import (
"database/sql"
"errors"
"fmt"
"strings"
"time"
)
const migrationTable = "migrations"
var (
// ErrTableNotExists returns when migration table not found
ErrTableNotExists = errors.New("Migration table does not exist")
// ErrNoMigrationDefined returns when no migrations defined in the migrations pool
ErrNoMigrationDefined = errors.New("No migrations defined")
// ErrEmptyRollbackStack returns when nothing can be reverted
ErrEmptyRollbackStack = errors.New("Nothing to rollback, there are no migration executed")
// ErrMissingMigrationName returns when migration name is missing
ErrMissingMigrationName = errors.New("Missing migration name")
// ErrNoSQLCommandsToRun returns when migration is invalid and has no commands in the pool
ErrNoSQLCommandsToRun = errors.New("There are no commands to be executed")
)
type migrationEntry struct {
id uint64
name string
batch uint64
appliedAt time.Time
}
// Migrator represents a struct with migrations, that should be executed.
//
// Default migration table name is `migrations`, but it can be re-defined.
// Pool is a list of migrations that should be migrated.
type Migrator struct {
// Name of the table to track executed migrations
TableName string
// stack of migrations
Pool []Migration
executed []migrationEntry
}
// Migrate runs all migrations from pool and stores in migration table executed migration.
func (m Migrator) Migrate(db *sql.DB) (migrated []string, err error) {
if len(m.Pool) == 0 {
return migrated, ErrNoMigrationDefined
}
if err := m.checkMigrationPool(); err != nil {
return migrated, err
}
if err := m.createMigrationTable(db); err != nil {
return migrated, fmt.Errorf("Migration table failed to be created: %v", err)
}
if err := m.fetchExecuted(db); err != nil {
return migrated, err
}
batch := m.batch() + 1
table := m.table()
for _, item := range m.Pool {
if m.isExecuted(item.Name) {
continue
}
s := item.Up()
if len(s.pool) == 0 {
return migrated, ErrNoSQLCommandsToRun
}
if err := item.exec(db, s.pool...); err != nil {
return migrated, err
}
entry := migrationEntry{name: item.Name, batch: batch}
sql := fmt.Sprintf("INSERT INTO `%s` (`name`, `batch`) VALUES (\"%s\", %d)", table, entry.name, entry.batch)
if _, err := db.Exec(sql); err != nil {
return migrated, err
}
migrated = append(migrated, item.Name)
}
return migrated, nil
}
// Rollback reverts last executed batch of migrations.
func (m Migrator) Rollback(db *sql.DB) (reverted []string, err error) {
if len(m.Pool) == 0 {
return reverted, ErrNoMigrationDefined
}
if err := m.checkMigrationPool(); err != nil {
return reverted, err
}
if !m.hasTable(db) {
return reverted, ErrTableNotExists
}
if err := m.fetchExecuted(db); err != nil {
return reverted, err
}
if len(m.executed) == 0 {
return reverted, ErrEmptyRollbackStack
}
table := m.table()
revertable := m.lastBatchExecuted()
for i := len(revertable) - 1; i >= 0; i-- {
name := revertable[i].name
for j := len(m.Pool) - 1; j >= 0; j-- {
item := m.Pool[j]
if item.Name == name {
s := item.Down()
if len(s.pool) == 0 {
return reverted, ErrNoSQLCommandsToRun
}
if err := item.exec(db, s.pool...); err != nil {
return reverted, err
}
if _, err := db.Exec(fmt.Sprintf("DELETE FROM %s WHERE id = ?", table), revertable[i].id); err != nil {
return reverted, err
}
reverted = append(reverted, name)
}
}
}
return reverted, nil
}
// Revert reverts all executed migration from the pool.
func (m Migrator) Revert(db *sql.DB) (reverted []string, err error) {
if len(m.Pool) == 0 {
return reverted, ErrNoMigrationDefined
}
if err := m.checkMigrationPool(); err != nil {
return reverted, err
}
if !m.hasTable(db) {
return reverted, ErrTableNotExists
}
if err := m.fetchExecuted(db); err != nil {
return reverted, err
}
if len(m.executed) == 0 {
return reverted, ErrEmptyRollbackStack
}
table := m.table()
for i := len(m.executed) - 1; i >= 0; i-- {
name := m.executed[i].name
for j := len(m.Pool) - 1; j >= 0; j-- {
item := m.Pool[j]
if item.Name == name {
s := item.Down()
if len(s.pool) == 0 {
return reverted, ErrNoSQLCommandsToRun
}
if err := item.exec(db, s.pool...); err != nil {
return reverted, err
}
if _, err := db.Exec(fmt.Sprintf("DELETE FROM %s WHERE id = ?", table), m.executed[i].id); err != nil {
return reverted, err
}
reverted = append(reverted, name)
}
}
}
return reverted, nil
}
func (m Migrator) checkMigrationPool() error {
var names []string
for _, item := range m.Pool {
if item.Name == "" {
return ErrMissingMigrationName
}
for _, exist := range names {
if exist == item.Name {
return fmt.Errorf(`Migration "%s" is duplicated in the pool`, exist)
}
}
names = append(names, item.Name)
}
return nil
}
func (m Migrator) createMigrationTable(db *sql.DB) error {
if m.hasTable(db) {
return nil
}
sql := fmt.Sprintf(
"CREATE TABLE %s (%s) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
m.table(),
strings.Join([]string{
"id int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY",
"name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL",
"batch int(11) NOT NULL",
"applied_at timestamp(6) NULL DEFAULT CURRENT_TIMESTAMP(6)",
}, ", "),
)
_, err := db.Exec(sql)
return err
}
func (m Migrator) hasTable(db *sql.DB) bool {
_, hasTable := db.Query("SELECT * FROM " + m.table())
return hasTable == nil
}
func (m Migrator) table() string {
table := m.TableName
if table == "" {
table = migrationTable
}
return table
}
func (m Migrator) batch() uint64 {
var batch uint64
for _, item := range m.executed {
if item.batch > batch {
batch = item.batch
}
}
return batch
}
func (m *Migrator) fetchExecuted(db *sql.DB) error {
rows, err := db.Query("SELECT id, name, batch, applied_at FROM " + m.table() + " ORDER BY applied_at ASC")
if err != nil {
return err
}
m.executed = []migrationEntry{}
for rows.Next() {
var entry migrationEntry
if err := rows.Scan(&entry.id, &entry.name, &entry.batch, &entry.appliedAt); err != nil {
return err
}
m.executed = append(m.executed, entry)
}
return nil
}
func (m Migrator) isExecuted(name string) bool {
for _, item := range m.executed {
if item.name == name {
return true
}
}
return false
}
func (m Migrator) lastBatchExecuted() []migrationEntry {
batch := m.batch()
var result []migrationEntry
for _, item := range m.executed {
if item.batch == batch {
result = append(result, item)
}
}
return result
}