forked from DavidHuie/gomigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomigrate.go
336 lines (292 loc) · 8.15 KB
/
gomigrate.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// A simple database migrator for PostgreSQL.
package gomigrate
import (
"database/sql"
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
)
type migrationType string
const (
migrationTableName = "gomigrate"
upMigration = migrationType("up")
downMigration = migrationType("down")
)
var (
InvalidMigrationFile = errors.New("Invalid migration file")
InvalidMigrationPair = errors.New("Invalid pair of migration files")
InvalidMigrationsPath = errors.New("Invalid migrations path")
InvalidMigrationType = errors.New("Invalid migration type")
NoActiveMigrations = errors.New("No active migrations to rollback")
)
type Migrator struct {
DB *sql.DB
MigrationsPath string
dbAdapter Migratable
migrations map[uint64]*Migration
}
// Logger is the instance of a StdLogger interface to log events to.
// By default it is set to send all log messages to stdout,
// but you can set it to redirect wherever you want by instantiating
// your own logger.
var Logger StdLogger = log.New(os.Stdout, "[gomigrate] ", log.LstdFlags)
// StdLogger is used to log error messages.
type StdLogger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
Fatalf(format string, v ...interface{})
}
// Returns true if the migration table already exists.
func (m *Migrator) MigrationTableExists() (bool, error) {
row := m.DB.QueryRow(m.dbAdapter.SelectMigrationTableSql(), migrationTableName)
var tableName string
err := row.Scan(&tableName)
if err == sql.ErrNoRows {
Logger.Print("Migrations table not found")
return false, nil
}
if err != nil {
Logger.Printf("Error checking for migration table: %v", err)
return false, err
}
Logger.Print("Migrations table found")
return true, nil
}
// Creates the migrations table if it doesn't exist.
func (m *Migrator) CreateMigrationsTable() error {
_, err := m.DB.Exec(m.dbAdapter.CreateMigrationTableSql())
if err != nil {
Logger.Fatalf("Error creating migrations table: %v", err)
}
Logger.Printf("Created migrations table: %s", migrationTableName)
return nil
}
// Returns a new migrator.
func NewMigrator(db *sql.DB, adapter Migratable, migrationsPath string) (*Migrator, error) {
// Normalize the migrations path.
path := []byte(migrationsPath)
pathLength := len(path)
if path[pathLength-1] != '/' {
path = append(path, '/')
}
Logger.Printf("Migrations path: %s", path)
migrator := Migrator{
db,
string(path),
adapter,
make(map[uint64]*Migration),
}
// Create the migrations table if it doesn't exist.
tableExists, err := migrator.MigrationTableExists()
if err != nil {
return nil, err
}
if !tableExists {
if err := migrator.CreateMigrationsTable(); err != nil {
return nil, err
}
}
// Get all metadata from the database.
if err := migrator.fetchMigrations(); err != nil {
return nil, err
}
if err := migrator.getMigrationStatuses(); err != nil {
return nil, err
}
return &migrator, nil
}
// Populates a migrator with a sorted list of migrations from the file system.
func (m *Migrator) fetchMigrations() error {
pathGlob := append([]byte(m.MigrationsPath), []byte("*")...)
matches, err := filepath.Glob(string(pathGlob))
if err != nil {
Logger.Fatalf("Error while globbing migrations: %v", err)
}
for _, match := range matches {
num, migrationType, name, err := parseMigrationPath(match)
if err != nil {
Logger.Printf("Invalid migration file found: %s", match)
continue
}
Logger.Printf("Migration file found: %s", match)
migration, ok := m.migrations[num]
if !ok {
migration = &Migration{Id: num, Name: name, Status: Inactive}
m.migrations[num] = migration
}
if migrationType == upMigration {
migration.UpPath = match
} else {
migration.DownPath = match
}
}
// Validate each migration.
for _, migration := range m.migrations {
if !migration.valid() {
path := migration.UpPath
if path == "" {
path = migration.DownPath
}
Logger.Printf("Invalid migration pair for path: %s", path)
return InvalidMigrationPair
}
}
Logger.Printf("Migrations file pairs found: %v", len(m.migrations))
return nil
}
// Queries the migration table to determine the status of each
// migration.
func (m *Migrator) getMigrationStatuses() error {
for _, migration := range m.migrations {
row := m.DB.QueryRow(m.dbAdapter.GetMigrationSql(), migration.Id)
var mid uint64
err := row.Scan(&mid)
if err == sql.ErrNoRows {
continue
}
if err != nil {
Logger.Printf(
"Error getting migration status for %s: %v",
migration.Name,
err,
)
return err
}
migration.Status = Active
}
return nil
}
// Returns a sorted list of migration ids for a given status. -1 returns
// all migrations.
func (m *Migrator) Migrations(status int) []*Migration {
// Sort all migration ids.
ids := make([]uint64, 0)
for id, _ := range m.migrations {
ids = append(ids, id)
}
sort.Sort(uint64slice(ids))
// Find ids for the given status.
migrations := make([]*Migration, 0)
for _, id := range ids {
migration := m.migrations[id]
if status == -1 || migration.Status == status {
migrations = append(migrations, migration)
}
}
return migrations
}
// Applies a single migration.
func (m *Migrator) ApplyMigration(migration *Migration, mType migrationType) error {
var path string
if mType == upMigration {
path = migration.UpPath
} else if mType == downMigration {
path = migration.DownPath
} else {
return InvalidMigrationType
}
Logger.Printf("Applying migration: %s", path)
sql, err := ioutil.ReadFile(path)
if err != nil {
Logger.Printf("Error reading migration: %s", path)
return err
}
transaction, err := m.DB.Begin()
if err != nil {
Logger.Printf("Error opening transaction: %v", err)
return err
}
// Certain adapters can not handle multiple sql commands in one file so we need the adapter to split up the command
commands := m.dbAdapter.GetMigrationCommands(string(sql))
// Perform the migration.
for _, cmd := range commands {
result, err := transaction.Exec(cmd)
if err != nil {
Logger.Printf("Error executing migration: %v", err)
if rollbackErr := transaction.Rollback(); rollbackErr != nil {
Logger.Printf("Error rolling back transaction: %v", rollbackErr)
return rollbackErr
}
return err
}
if result != nil {
if rowsAffected, err := result.RowsAffected(); err != nil {
Logger.Printf("Error getting rows affected: %v", err)
if rollbackErr := transaction.Rollback(); rollbackErr != nil {
Logger.Printf("Error rolling back transaction: %v", rollbackErr)
return rollbackErr
}
return err
} else {
Logger.Printf("Rows affected: %v", rowsAffected)
}
}
}
// Log the event.
if mType == upMigration {
_, err = transaction.Exec(
m.dbAdapter.MigrationLogInsertSql(),
migration.Id,
)
} else {
_, err = transaction.Exec(
m.dbAdapter.MigrationLogDeleteSql(),
migration.Id,
)
}
if err != nil {
Logger.Printf("Error logging migration: %v", err)
if rollbackErr := transaction.Rollback(); rollbackErr != nil {
Logger.Printf("Error rolling back transaction: %v", rollbackErr)
return rollbackErr
}
return err
}
// Commit and update the struct status.
if err := transaction.Commit(); err != nil {
Logger.Printf("Error commiting transaction: %v", err)
return err
}
if mType == upMigration {
migration.Status = Active
} else {
migration.Status = Inactive
}
return nil
}
// Applies all inactive migrations.
func (m *Migrator) Migrate() error {
for _, migration := range m.Migrations(Inactive) {
if err := m.ApplyMigration(migration, upMigration); err != nil {
return err
}
}
return nil
}
// Rolls back the last migration.
func (m *Migrator) Rollback() error {
return m.RollbackN(1)
}
// Rolls back N migrations.
func (m *Migrator) RollbackN(n int) error {
migrations := m.Migrations(Active)
if len(migrations) == 0 {
return nil
}
last_migration := len(migrations) - 1 - n
for i := len(migrations) - 1; i != last_migration; i-- {
if err := m.ApplyMigration(migrations[i], downMigration); err != nil {
return err
}
}
return nil
}
// Rolls back all migrations.
func (m *Migrator) RollbackAll() error {
migrations := m.Migrations(Active)
return m.RollbackN(len(migrations))
}