-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplemigrate.go
357 lines (287 loc) · 9.57 KB
/
simplemigrate.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package simplemigrate
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"io/fs"
"os"
"sort"
"strings"
"time"
"github.com/gosom/simplemigrate/internal/filesystem"
)
var (
// ErrMigrationTableNameMissing is returned when the migrations table name is empty
ErrMigrationTableNameMissing = errors.New("migrations table name cannot be empty")
// ErrUnknownDriver is returned when the driver is unknown
ErrUnknownDriver = errors.New("unknown driver")
// ErrInvalidMigrationFile is returned when the migration file is invalid
ErrInvalidMigrationFile = errors.New("invalid migration file")
// ErrMigrationFolder is returned when the migration folder is invalid
ErrMigrationFolder = errors.New("invalid migration folder")
// ErrInvalidQuery is returned when the query is invalid
ErrInvalidQuery = errors.New("invalid query")
)
const (
// defaultMigrationsTable is the default name of the migrations table
defaultMigrationsTable = "schema_migrations"
)
// Migration represents a single migration
type Migration struct {
Version int
Fname string
AppliedAt *time.Time
Statements []string
Hash string
}
// DBDriver represents a database driver
//
//go:generate mockgen -destination=internal/mocks/mock_dbdriver.go -package=mocks . DBDriver
type DBDriver interface {
// Dialect returns the database dialect
Dialect() string
// Close closes the connection to the database
Close(ctx context.Context) error
// CreateMigrationsTable creates the migrations table
// migrationsTable is the name of the migrations table
// If the table already exists, it does nothing
// It returns an error if something goes wrong
CreateMigrationsTable(ctx context.Context, migrationsTable string) error
// SelectMigrations selects all migrations from the migrations table
// migrationsTable is the name of the migrations table
// It returns a sorted slice (by Version ascending) of migrations or an error
SelectMigrations(ctx context.Context, migrationsTable string) ([]Migration, error)
// ApplyMigrations applies migrations to the database
// migrationsTable is the name of the migrations table
// inTx is a flag that indicates if the migrations should be applied in a transaction
// migrations is the slice of migrations to apply
// It returns an error if something goes wrong
ApplyMigrations(ctx context.Context, migrationsTable string, inTx bool, migrations []Migration) error
}
// QueryValidator represents a query validator
//
//go:generate mockgen -destination=internal/mocks/mock_queryvalidator.go -package=mocks . QueryValidator
type QueryValidator interface {
ValidateQuery(ctx context.Context, dialect, query string) error
}
// Option represents a migrator option
type Option func(*Migrator) error
// Migrator is a struct that represents a migrator
// It is used to migrate a database
type Migrator struct {
driver DBDriver
migrationsTable string
printer func(string, ...any)
folder fs.FS
qvalidator QueryValidator
inTransaction bool
}
// New is a constructor for Migrator
// Use this to create a new migrator
// and apply migrations to a database
// The driver is the database driver
// The options are used to configure the migrator
func New(driver DBDriver, opts ...Option) *Migrator {
ans := Migrator{
driver: driver,
migrationsTable: defaultMigrationsTable,
}
for _, opt := range opts {
if err := opt(&ans); err != nil {
panic(err)
}
}
if ans.folder == nil {
ans.folder = filesystem.NewSystemFS("migrations")
}
return &ans
}
// WithInTransaction is an option to apply all migrations in a transaction
// If an error occurs, the transaction is rolled back
// It is disabled by default
func WithInTransaction() Option {
return func(m *Migrator) error {
m.inTransaction = true
return nil
}
}
// WithQueryValidator is an option to enable query validation
// It is disabled by default
// Its purpose is to validate queries before applying them
func WithQueryValidator(validator QueryValidator) Option {
return func(m *Migrator) error {
m.qvalidator = validator
return nil
}
}
// WithSystemFS is an option to use the system filesystem
// The root is the root folder of the migrations
// It is "migrations" by default
func WithSystemFS(root string) Option {
return func(m *Migrator) error {
exists, err := isDir(root)
if err != nil {
return fmt.Errorf("%w: %s", ErrMigrationFolder, err.Error())
}
if !exists {
return fmt.Errorf("%w: %s", ErrMigrationFolder, root+" does not exist")
}
m.folder = filesystem.NewSystemFS(root)
return nil
}
}
// WithEmbedFS is an option to use the embed filesystem
// The fs is the embed filesystem
// It is nil by default
func WithEmbedFS(f fs.FS) Option {
return func(m *Migrator) error {
m.folder = f
return nil
}
}
// WithMigrationTable is an option to set the migrations table name
// It is "schema_migrations" by default
func WithMigrationTable(migrationsTable string) Option {
return func(m *Migrator) error {
if migrationsTable == "" {
return ErrMigrationTableNameMissing
}
m.migrationsTable = migrationsTable
return nil
}
}
// Migrate is used to apply migrations to a database
// It returns an error if something goes wrong
func (m *Migrator) Migrate(ctx context.Context) error {
fmt.Println("Migrating...")
if err := m.driver.CreateMigrationsTable(ctx, m.migrationsTable); err != nil {
return err
}
fmt.Println("Migrations table:", m.migrationsTable)
localMigrations, err := m.readMigrations(ctx)
if err != nil {
return err
}
appliedMigrations, err := m.driver.SelectMigrations(ctx, m.migrationsTable)
if err != nil {
return err
}
if len(localMigrations) < len(appliedMigrations) {
return fmt.Errorf("%w: %s", ErrInvalidMigrationFile, "local migrations are less than applied migrations")
}
for i := range appliedMigrations {
if appliedMigrations[i].Version != localMigrations[i].Version {
return fmt.Errorf("%w: %s", ErrInvalidMigrationFile, "local migrations are not in sync with applied migrations")
}
if appliedMigrations[i].Hash != localMigrations[i].Hash {
return fmt.Errorf("%w: %s", ErrInvalidMigrationFile, "local migrations are not in sync with applied migrations")
}
}
toApply := localMigrations[len(appliedMigrations):]
if len(toApply) == 0 {
fmt.Println("No migrations to apply")
return nil
}
for _, migration := range toApply {
if err := m.validate(ctx, migration); err != nil {
return err
}
}
fmt.Printf("Applying %d migrations [start_version=%d end_version=%d]\n",
len(toApply), toApply[0].Version, toApply[len(toApply)-1].Version)
return m.driver.ApplyMigrations(ctx, m.migrationsTable, m.inTransaction, toApply)
}
// readMigrations is used to read migrations from the filesystem
func (m *Migrator) readMigrations(_ context.Context) ([]Migration, error) {
files, err := listFiles(m.folder, ".")
if err != nil {
return nil, err
}
items := make([]Migration, 0, len(files))
for _, file := range files {
migration := Migration{
Fname: file,
}
idx := strings.Index(file, "_")
if idx == -1 {
return nil, fmt.Errorf("%w: %s", ErrInvalidMigrationFile, file+" must have a version")
}
if _, err := fmt.Sscanf(file[:idx], "%d", &migration.Version); err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidMigrationFile, file+" must have an integer version")
}
if migration.Version == 0 {
return nil, fmt.Errorf("%w: %s", ErrInvalidMigrationFile, file+" must have a non-zero version")
}
data, err := fs.ReadFile(m.folder, file)
if err != nil {
return nil, err
}
data = bytes.TrimSpace(data)
migration.Hash = computeHash(data)
statements := strings.Split(string(data), "-- migrate:next")
migration.Statements = statements
items = append(items, migration)
}
sort.Slice(items, func(i, j int) bool {
return items[i].Version < items[j].Version
})
if len(items) > 0 {
if items[0].Version != 1 {
return nil, fmt.Errorf("%w: %s", ErrInvalidMigrationFile, "first migration must have version 1")
}
for i := 1; i < len(items); i++ {
if items[i].Version-items[i-1].Version != 1 {
return nil, fmt.Errorf("%w: %s (%s - %s)", ErrInvalidMigrationFile, "migrations must have sequential versions", items[i-1].Fname, items[i].Fname)
}
}
}
return items, nil
}
// validate is used to validate a migration
func (m *Migrator) validate(ctx context.Context, migration Migration) error {
if len(migration.Statements) == 0 {
return fmt.Errorf("%w: %s", ErrInvalidMigrationFile, migration.Fname+" is empty")
}
if m.qvalidator != nil {
for _, statement := range migration.Statements {
if err := m.qvalidator.ValidateQuery(ctx, m.driver.Dialect(), statement); err != nil {
return fmt.Errorf("%s: %w %s", migration.Fname, ErrInvalidQuery, err)
}
}
}
return nil
}
// listFiles is used to list files from the filesystem
func listFiles(fsys fs.FS, dir string) ([]string, error) {
var files []string //nolint:prealloc // I don't know how many files are in the folder
entries, err := fs.ReadDir(fsys, dir)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() {
return nil, fmt.Errorf("%w: %s", ErrMigrationFolder, "cannot contain a folder")
}
if !strings.HasSuffix(entry.Name(), ".sql") {
return nil, fmt.Errorf("%w: %s", ErrInvalidMigrationFile, entry.Name()+" must have .sql extension")
}
files = append(files, entry.Name())
}
return files, nil
}
func isDir(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return fileInfo.IsDir(), nil
}
func computeHash(b []byte) string {
hash := sha256.Sum256(b)
return fmt.Sprintf("%x", hash)
}