-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrator.go
121 lines (99 loc) · 2.67 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
package migrator
import (
"context"
"database/sql"
"fmt"
"io/fs"
"sort"
)
type Migrator struct {
db *sql.DB
fs fs.FS
logFunc LogFunc
idFunc IDFunc
}
func New(db *sql.DB, fs fs.ReadDirFS) *Migrator {
return &Migrator{
db: db,
fs: fs,
logFunc: func(_ string, _ ...any) {},
idFunc: DefaultIDFunc,
}
}
func (m *Migrator) WithLogFunc(logFunc LogFunc) *Migrator {
m.logFunc = logFunc
return m
}
func (m *Migrator) WithIDFunc(idFunc IDFunc) *Migrator {
m.idFunc = idFunc
return m
}
func (m *Migrator) Migrate(ctx context.Context) error {
tx, err := m.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("BEGIN: %w", err)
}
defer func() {
_ = tx.Rollback()
}()
var currentID uint32
if err := tx.QueryRowContext(ctx, "PRAGMA user_version").Scan(¤tID); err != nil {
return fmt.Errorf("PRAGMA user_version: %w", err)
}
entries, err := fs.ReadDir(m.fs, ".")
if err != nil {
return fmt.Errorf("readdir: %w", err)
}
ids := make(map[uint32]string, len(entries))
unappliedMigrations := make([]*migrationFile, 0, len(entries))
for _, entry := range entries {
if entry.IsDir() {
continue
}
baseName := entry.Name()
content, err := fs.ReadFile(m.fs, baseName)
if err != nil {
return fmt.Errorf("readfile: %w", err)
}
id, err := m.idFunc(baseName)
if err != nil {
return fmt.Errorf("invalid migration filename: %w", err)
}
if duplicateFilename, ok := ids[id]; ok {
return fmt.Errorf("duplicate migration id (%d): %s, %s", id, duplicateFilename, baseName)
}
ids[id] = baseName
// Migration has already been applied
if id <= currentID {
continue
}
unappliedMigrations = append(unappliedMigrations, &migrationFile{
ID: id,
Filename: baseName,
SQL: string(content),
})
}
if len(unappliedMigrations) == 0 {
return nil
}
sort.Slice(unappliedMigrations, func(i, j int) bool {
return unappliedMigrations[i].ID < unappliedMigrations[j].ID
})
for _, migration := range unappliedMigrations {
if _, err := tx.ExecContext(ctx, migration.SQL); err != nil {
return fmt.Errorf("%s: %w", migration.Filename, err)
}
m.logFunc("migration applied", "id", migration.ID, "filename", migration.Filename)
}
finalID := unappliedMigrations[len(unappliedMigrations)-1].ID
if _, err := tx.ExecContext(ctx, fmt.Sprintf("PRAGMA user_version = %d", finalID)); err != nil {
return fmt.Errorf("PRAGMA user_version: %w", err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("COMMIT: %w", err)
}
return nil
}
// LogFunc defines a function used to log the migration process.
// attrs is a list of key-value pairs, where the key is a string.
type LogFunc func(msg string, attrs ...any)