forked from hbarnardt/hb_migrations
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrations.go
943 lines (827 loc) · 22.7 KB
/
migrations.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
package migrations
import (
"bytes"
"context"
"html/template"
"log"
"os"
"path"
"sort"
"time"
"github.com/go-pg/pg/v10"
"github.com/pkg/errors"
)
var (
// ErrInvalidVerbosity indicates that verbosity has already been
// specified when attempting to specify quiet, or that quiet has
// already been specified when attempting to specify verbosity.
ErrInvalidVerbosity = errors.New("verbosity already set in opposite direction")
// ErrMigrationNotKnown indicates that a migration has been found
// in the DB, but with no corresponding known migration.
ErrMigrationNotKnown = errors.New("no migration by name")
// ErrInitialMigrationNotKnown indicates that no migration was
// found with the name of the initial migration.
ErrInitialMigrationNotKnown = errors.New("initial migration not known")
// ErrNoMigrationName indicates that an attempt was made to
// create a migration, without specifying a name.
ErrNoMigrationName = errors.New("no migration name specified")
// ErrFileAlreadyExists indicates that an attempt was made to
// create a migration, without specifying a name.
ErrFileAlreadyExists = errors.New("migration file already exists")
// ErrInvalidMigrationFuncRun indicates that a migration is being
// run with a function with invalid function signature.
ErrInvalidMigrationFuncRun = errors.New("invalid migration function run")
)
type migration struct {
Name string
Up interface{}
Down interface{}
}
const (
// DefaultMigrationTableName is the table in which migrations will be
// noted if not overridden in the Migrator.
DefaultMigrationTableName = "public.hb_migrations"
// DefaultInitialMigrationName is the name of the migration which will
// be run by Init, if not overridden in the Migrator.
DefaultInitialMigrationName = "000000000000_init"
// DefaultMigrationNameConvention is the convention with which the names
// for migration files and functions will be generated, if not overridden
// in the Migrator.
DefaultMigrationNameConvention = SnakeCase
// DefaultMigrationTemplate is the template which will be used for Create,
// when using Create without a template.
//
// Expects a file similar to the following to exist in the same package:
// package main
//
// import (
// migrations "github.com/getkalido/hb_migrations/v2"
// )
//
// var (
// registry migrations.Registry
// )
//
// func main() {
// dbFactory := GetDB // GetDB should return a *pg.DB.
// migrator, err := migrations.NewMigrator(dbFactory, migrations.WithMigrations(®istry))
// // Do things.
// }
DefaultMigrationTemplate = `package main
import (
"github.com/go-pg/pg/v10"
migrations "github.com/getkalido/hb_migrations/v2"
)
func init() {
err := registry.Register(
"{{.Filename}}",
up{{.FuncName}},
down{{.FuncName}},
)
if err != nil {
panic(err)
}
}
func up{{.FuncName}}(tx *pg.Tx, cont *migrations.Context) error {
var err error
_, err = tx.Exec(` + "`" + "`" + `)
if err != nil {
return err
}
return nil
}
func down{{.FuncName}}(tx *pg.Tx, cont *migrations.Context) error {
var err error
_, err = tx.Exec(` + "`" + "`" + `)
if err != nil {
return err
}
return nil
}
`
)
// DBFactory returns a DB instance which will house both the migration table
// (to track completed migrations) and the tables which will be affected by
// the migrations.
type DBFactory func() *pg.DB
// Migrator can create or manage migrations as indicated by
// options during construction.
//
// Should not be considered thread-safe.
type Migrator struct {
dbFactory func() *pg.DB
ctx context.Context
logger *log.Logger
registry Registry
migrationTableName string
initialMigration string
migrationDir string
templateDir string
migrationNameConvention MigrationNameConvention
explicitLock bool
verbosity int
context Context
}
// DefaultMigrator returns a migrator with the default options.
func DefaultMigrator() *Migrator {
return &Migrator{
migrationTableName: "public.hb_migrations",
initialMigration: "000000000000_init",
migrationNameConvention: SnakeCase,
explicitLock: true,
}
}
// NewMigrator creates a Migrator with the specified options.
//
// DefaultMigrator is used to get a default migrator, then
// options are applied on top of the defaults.
func NewMigrator(dbFactory DBFactory, opts ...MigratorOpt) (*Migrator, error) {
var err error
migrator := DefaultMigrator()
for _, opt := range opts {
err = opt(migrator)
if err != nil {
return nil, err
}
}
if migrator.logger == nil {
migrator.logger = log.Default()
}
if migrator.ctx == nil {
migrator.logWithMinVerbosity(1, "Using TODO context")
migrator.ctx = context.TODO()
}
if migrator.migrationDir == "" {
workingDir, err := os.Getwd()
if err != nil {
return nil, err
}
migrator.logWithMinVerbosity(1, "Setting migration directory: %s", workingDir)
migrator.migrationDir = workingDir
}
migrator.dbFactory = dbFactory
return migrator, nil
}
// MigratorOpt represents an option which can be applied to
// a migrator during creation. See With*() functions in this
// package.
type MigratorOpt func(*Migrator) error
// WithMigrationTableName sets the name of the table which will
// store completed migrations for a Migrator.
//
// Intended for use with NewMigrator.
func WithMigrationTableName(name string) MigratorOpt {
return func(m *Migrator) error {
m.migrationTableName = name
return nil
}
}
// WithInitialName sets the name of the initial migration which
// will be run by a Migrator when running the init command.
//
// Intended for use with NewMigrator.
func WithInitialName(name string) MigratorOpt {
return func(m *Migrator) error {
m.initialMigration = name
return nil
}
}
// WithNameConvention sets the name naming convention which will
// be used by a Migrator when generating new migrations.
//
// Intended for use with NewMigrator.
func WithNameConvention(convention MigrationNameConvention) MigratorOpt {
return func(m *Migrator) error {
m.migrationNameConvention = convention
return nil
}
}
// WithCapacity initialises a Migrator with enough capacity for
// a given number of migrations. Not necessary, but can limit
// allocations when building the list of migrations.
//
// Intended for use with NewMigrator.
func WithCapacity(capacity uint) MigratorOpt {
return func(m *Migrator) error {
m.registry.EnsureCapacity(int(capacity))
return nil
}
}
// WithMigrations loads migrations from an existing registry.
// Pre-emptively ensures that the Migrator has capacity for
// the migrations being copied.
//
// Intended for use with NewMigrator.
func WithMigrations(registry *Registry) MigratorOpt {
return func(m *Migrator) error {
m.registry.From(registry)
return nil
}
}
// WithoutExplicitLock initialises a Migrator which will
// try to explicitly lock the migrations table for each
// transaction. Currently the default behaviour.
//
// Intended for use with NewMigrator.
func WithExplicitLock() MigratorOpt {
return func(m *Migrator) error {
m.explicitLock = true
return nil
}
}
// WithoutExplicitLock initialises a Migrator which will not
// try to explicitly lock the migrations table for each
// transaction.
//
// Intended for use with NewMigrator.
func WithoutExplicitLock() MigratorOpt {
return func(m *Migrator) error {
m.explicitLock = false
return nil
}
}
// WithLogger initialises a Migrator with a logger to use
// when logging output. If no logger is specified, the
// standard logger from the log package is used.
//
// Intended for use with NewMigrator.
func WithLogger(logger *log.Logger) MigratorOpt {
return func(m *Migrator) error {
m.logger = logger
return nil
}
}
// WithVerbosity initialises a Migrator with verbosity level
// (default: 0). Non-zero values will increase the amount
// of logging.
//
// It is an error to set both verbosity and quiet to a
// non-zero value.
//
// Intended for use with NewMigrator.
func WithVerbosity(verbosity uint) MigratorOpt {
return func(m *Migrator) error {
if m.verbosity < 0 {
return errors.Wrapf(
ErrInvalidVerbosity,
"current verbosity %d",
m.verbosity,
)
}
m.verbosity = int(verbosity)
return nil
}
}
// WithQuiet initialises a Migrator with quiet level
// (default: 0). Non-zero values will decrease the amount
// of logging.
//
// It is an error to set both verbosity and quiet to a
// non-zero value.
//
// Intended for use with NewMigrator.
func WithQuiet(quiet uint) MigratorOpt {
return func(m *Migrator) error {
if m.verbosity > 0 {
return errors.Wrapf(
ErrInvalidVerbosity,
"current verbosity %d",
m.verbosity,
)
}
m.verbosity = int(quiet)
return nil
}
}
// WithContext initialises a Migrator with a context object.
// This is intended to allow the migrations to be easily
// stopped in a CLI tool.
//
// Intended for use with NewMigrator.
func WithContext(ctx context.Context) MigratorOpt {
return func(m *Migrator) error {
m.ctx = ctx
return nil
}
}
// WithTemplateDir initialises a Migrator with a given
// template directory. When searching for named templates,
// this directory will be used.
//
// Intended for use with NewMigrator.
func WithTemplateDir(path string) MigratorOpt {
return func(m *Migrator) error {
m.templateDir = path
return nil
}
}
// WithMigrationDir initialises a Migrator with a given
// migration directory. When generating new migrations,
// they will be created in this directory.
//
// Intended for use with NewMigrator.
func WithMigrationDir(path string) MigratorOpt {
return func(m *Migrator) error {
m.migrationDir = path
return nil
}
}
// WithPostgresFlavour initialises a Migrator with a given
// Postgres flavour. This is not directly used by Migrator
// and is merely a helper to allow migrations to act
// differently depending on which DB they are connected to.
//
// Intended for use with NewMigrator.
func WithPostgresFlavour(flavour PostgresFlavour) MigratorOpt {
return func(m *Migrator) error {
m.context.Flavour = flavour
return nil
}
}
// Register adds a migration to the list of known migrations.
//
// If a migration by the given name is already known, this will
// return ErrMigrationAlreadyExists.
//
// Valid function signatures for migration functions are:
//
// func(*pg.Tx) error
// func(*pg.Tx, *Context) error
func (m *Migrator) Register(
name string,
up interface{},
down interface{},
) error {
return m.registry.Register(name, up, down)
}
// logWithMinVerbosity will log the provided format string if
// a verbosity threshold is met.
//
// Quiet level is considered negative verbosity.
func (m *Migrator) logWithMinVerbosity(requiredVerbosity int, format string, v ...any) {
currentVerbosity := m.verbosity
if currentVerbosity >= requiredVerbosity {
m.logger.Printf(format, v...)
}
}
func (m *Migrator) ensureMigrationTable(db pg.DBI) error {
_, err := db.Exec(
`
CREATE TABLE IF NOT EXISTS ? (
id serial,
name varchar,
batch integer,
migration_time timestamptz
)
`,
pg.Ident(m.migrationTableName),
)
return err
}
func (m *Migrator) insertCompletedMigration(db pg.DBI, name string, batch int) error {
_, err := db.Exec(
"insert into ? (name, batch, migration_time) values (?, ?, now())",
pg.Ident(m.migrationTableName),
name,
batch,
)
if err != nil {
return err
}
return nil
}
func (m *Migrator) getCompletedMigrations(db pg.DBI) ([]string, error) {
var results []string
_, err := db.Query(&results, "select name from ?", pg.Ident(m.migrationTableName))
if err != nil {
return nil, err
}
return results, nil
}
func (m *Migrator) getMigrationsToRun(db pg.DBI) ([]string, error) {
var completedMigrations []string
completedMigrations, err := m.getCompletedMigrations(db)
if err != nil {
return nil, err
}
missingMigrations, _, migrationsToRun := difference(completedMigrations, m.registry.List())
if len(missingMigrations) > 0 {
return nil, errors.Wrapf(ErrMigrationNotKnown, "unknown migrations: %+v", missingMigrations)
}
if len(migrationsToRun) > 0 {
sort.Strings(migrationsToRun)
}
return migrationsToRun, nil
}
// maybeLockTable will try to lock the table if explicit locking is
// enabled. If not, this does nothing.
func (m *Migrator) maybeLockTable(tx *pg.Tx) error {
if !m.explicitLock {
return nil
}
// https://www.postgresql.org/docs/current/explicit-locking.html
// This mode protects a table against concurrent data changes, and is self-exclusive so that only one session can hold it at a time.
// This means only one migration can run at a time, but pg_dump can still COPY from the table (since it acquires a ACCESS SHARE lock, or so I am told)
_, err := tx.Exec(
"LOCK ? in SHARE ROW EXCLUSIVE MODE",
pg.Ident(m.migrationTableName),
)
return err
}
func (m *Migrator) getBatchNumber(db pg.DBI) (int, error) {
var result int
_, err := db.Query(
pg.Scan(&result),
"select max(batch) from ?",
pg.Ident(m.migrationTableName),
)
if err != nil {
return 0, err
}
return result, nil
}
// Init runs the initial migration against the configured DB. Attempting to
// run this without registering the initial migration is an error.
func (m *Migrator) Init() error {
db := m.dbFactory()
return db.RunInTransaction(
m.ctx,
func(tx *pg.Tx) (err error) {
err = m.ensureMigrationTable(tx)
if err != nil {
return
}
err = m.maybeLockTable(tx)
if err != nil {
return
}
batch, err := m.getBatchNumber(tx)
if err != nil {
return err
}
batch++
m.logWithMinVerbosity(0, "Batch %d run: %d migrations\n", batch, 1)
migrationName := m.initialMigration
migration, ok := m.registry.Get(migrationName)
if !ok {
err = errors.Wrap(ErrInitialMigrationNotKnown, "not found")
return err
}
switch migrationFunc := migration.Up.(type) {
case func(*pg.Tx) error:
err = migrationFunc(tx)
case func(*pg.Tx, *Context) error:
err = migrationFunc(tx, &m.context)
default:
err = errors.Wrapf(
ErrInvalidMigrationFuncRun,
"invalid migration function %T",
migrationFunc,
)
}
if err != nil {
err = errors.Wrapf(err, "%s failed to migrate", migrationName)
return err
}
err = m.insertCompletedMigration(tx, migrationName, batch)
if err != nil {
return err
}
return nil
},
)
}
// MigrateStepByStep runs any migrations against the DB which have not been
// run yet. Each migration is run in its own transaction and marked as
// belonging to a separate batch.
func (m *Migrator) MigrateStepByStep() error {
db := m.dbFactory()
var migrationsToRun []string
err := db.RunInTransaction(
m.ctx,
func(tx *pg.Tx) (err error) {
err = m.ensureMigrationTable(tx)
if err != nil {
return
}
err = m.maybeLockTable(tx)
if err != nil {
return err
}
migrationsToRun, err = m.getMigrationsToRun(tx)
return err
},
)
if err != nil {
return err
}
if len(migrationsToRun) == 0 {
return nil
}
for _, migrationName := range migrationsToRun {
err := db.RunInTransaction(
m.ctx,
func(tx *pg.Tx) (err error) {
err = m.maybeLockTable(tx)
if err != nil {
return err
}
batch, err := m.getBatchNumber(tx)
if err != nil {
return err
}
batch++
m.logWithMinVerbosity(0, "Batch %d run: 1 migration - %s\n", batch, migrationName)
migration, exists := m.registry.Get(migrationName)
if !exists {
return errors.Wrapf(ErrMigrationNotKnown, "migration %s", migrationName)
}
switch migrationFunc := migration.Up.(type) {
case func(*pg.Tx) error:
err = migrationFunc(tx)
case func(*pg.Tx, *Context) error:
err = migrationFunc(tx, &m.context)
default:
err = errors.Wrapf(
ErrInvalidMigrationFuncRun,
"invalid migration function %T",
migrationFunc,
)
}
if err != nil {
err = errors.Wrapf(err, "%s failed to migrate", migrationName)
return err
}
err = m.insertCompletedMigration(tx, migrationName, batch)
return err
},
)
if err != nil {
return err
}
}
return nil
}
// MigrateStepByStep runs any migrations against the DB which have not been
// run yet. All migrations are run in a single migration and marked as
// belonging to the same batch.
func (m *Migrator) MigrateBatch() error {
db := m.dbFactory()
return db.RunInTransaction(
m.ctx,
func(tx *pg.Tx) (err error) {
err = m.ensureMigrationTable(tx)
if err != nil {
return
}
err = m.maybeLockTable(tx)
if err != nil {
return err
}
migrationsToRun, err := m.getMigrationsToRun(tx)
if err != nil {
return err
}
if len(migrationsToRun) == 0 {
return nil
}
batch, err := m.getBatchNumber(tx)
if err != nil {
return err
}
batch++
m.logWithMinVerbosity(0, "Batch %d run: %d migrations\n", batch, len(migrationsToRun))
for _, migrationName := range migrationsToRun {
migration, exists := m.registry.Get(migrationName)
if !exists {
return errors.Wrapf(ErrMigrationNotKnown, "migration %s", migrationName)
}
switch migrationFunc := migration.Up.(type) {
case func(*pg.Tx) error:
err = migrationFunc(tx)
case func(*pg.Tx, *Context) error:
err = migrationFunc(tx, &m.context)
default:
err = errors.Wrapf(
ErrInvalidMigrationFuncRun,
"invalid migration function %T",
migrationFunc,
)
}
if err != nil {
err = errors.Wrapf(err, "%s failed to migrate", migrationName)
return err
}
err = m.insertCompletedMigration(tx, migrationName, batch)
if err != nil {
return err
}
}
return err
},
)
}
func (m *Migrator) removeRolledbackMigration(db pg.DBI, name string) error {
m.logWithMinVerbosity(0, "Rolled back %s\n", name)
_, err := db.Exec("delete from ? where name = ?", pg.Ident(m.migrationTableName), name)
if err != nil {
return err
}
return nil
}
func (m *Migrator) getMigrationsInBatch(db pg.DBI, batch int) ([]string, error) {
var results []string
_, err := db.Query(
&results,
"select name from ? where batch = ? order by id desc",
pg.Ident(m.migrationTableName),
batch,
)
if err != nil {
return nil, err
}
return results, nil
}
// Rollback rolls back all migrations in the most recent batch.
// If the most recent group of migrations was run with MigrateStepByStep,
// this will only roll back the most recent migration.
func (m *Migrator) Rollback() error {
db := m.dbFactory()
return db.RunInTransaction(
m.ctx,
func(tx *pg.Tx) (err error) {
err = m.ensureMigrationTable(tx)
if err != nil {
return
}
err = m.maybeLockTable(tx)
if err != nil {
return err
}
completedMigrations, err := m.getCompletedMigrations(tx)
if err != nil {
return err
}
missingMigrations, _, _ := difference(completedMigrations, m.registry.List())
if len(missingMigrations) > 0 {
return errors.Wrapf(ErrMigrationNotKnown, "unknown migrations: %+v", missingMigrations)
}
batch, err := m.getBatchNumber(tx)
if err != nil {
return err
}
migrationsToRun, err := m.getMigrationsInBatch(tx, batch)
if err != nil {
return err
}
if len(migrationsToRun) == 0 {
return nil
}
sort.Strings(migrationsToRun)
m.logWithMinVerbosity(0, "Batch %d rollback: %d migrations\n", batch, len(migrationsToRun))
for _, migrationName := range migrationsToRun {
migration, exists := m.registry.Get(migrationName)
if !exists {
return errors.Wrapf(ErrMigrationNotKnown, "migration %s", migrationName)
}
switch migrationFunc := migration.Down.(type) {
case func(*pg.Tx) error:
err = migrationFunc(tx)
case func(*pg.Tx, *Context) error:
err = migrationFunc(tx, &m.context)
default:
err = errors.Wrapf(
ErrInvalidMigrationFuncRun,
"invalid migration function %T",
migrationFunc,
)
}
if err != nil {
err = errors.Wrapf(err, "%s failed to rollback", migrationName)
return err
}
err = m.removeRolledbackMigration(tx, migrationName)
if err != nil {
return err
}
}
return nil
},
)
}
// Create renders the default migration template to the configured migration
// directory.
func (m *Migrator) Create(description string) error {
caser, err := GetCaser(m.migrationNameConvention)
if err != nil {
return err
}
now := time.Now()
filename := caser.ToFileCase(now, description)
funcName := caser.ToFuncCase(now, description)
filePath, err := m.createMigrationFile(
filename,
funcName,
DefaultMigrationTemplate,
)
if err != nil {
return err
}
m.logWithMinVerbosity(0, "Created migration %s", filePath)
return nil
}
// CreateFromTemplate renders a migration template to the configured migration
// directory.
func (m *Migrator) CreateFromTemplate(description string, template string) error {
caser, err := GetCaser(m.migrationNameConvention)
if err != nil {
return err
}
now := time.Now()
filename := caser.ToFileCase(now, description)
funcName := caser.ToFuncCase(now, description)
filePath, err := m.createMigrationFile(
filename,
funcName,
template,
)
if err != nil {
return err
}
m.logWithMinVerbosity(0, "Created migration %s", filePath)
return nil
}
func (m *Migrator) createMigrationFile(filename, funcName, templateString string) (string, error) {
var err error
filePath := path.Join(m.migrationDir, filename+".go")
_, err = os.Stat(filePath)
if !os.IsNotExist(err) {
err := errors.Wrapf(
ErrFileAlreadyExists,
"file %s (%v)",
filename,
err,
)
return "", err
}
if len(templateString) == 0 {
templateString = DefaultMigrationTemplate
}
data := map[string]interface{}{
"Filename": filename,
"FuncName": funcName,
}
t := template.Must(template.New("template").Parse(templateString))
buf := &bytes.Buffer{}
if err := t.Execute(buf, data); err != nil {
return "", errors.Wrap(err, "failed to render template")
}
templateString = buf.String()
err = os.WriteFile(filePath, []byte(templateString), 0644)
if err != nil {
return "", errors.Wrap(err, "could not write file")
}
return filePath, nil
}
// difference returns the sets of:
//
// a - b
// a union b
// b - a
//
// Elements in the first two sets will be returned in the same order as
// their appearance in a. Elements in the last set will be returned in
// the same order as their appearance in b.
func difference(
a []string,
b []string,
) (
aNotB []string,
unionAB []string,
bNotA []string,
) {
aSet := make(map[string]struct{}, len(a))
for _, name := range a {
aSet[name] = struct{}{}
}
bSet := make(map[string]struct{}, len(b))
for _, name := range b {
bSet[name] = struct{}{}
}
aNotB = make([]string, 0)
unionAB = make([]string, 0)
bNotA = make([]string, 0)
for _, name := range a {
if _, ok := bSet[name]; ok {
unionAB = append(unionAB, name)
} else {
aNotB = append(aNotB, name)
}
}
for _, name := range b {
if _, ok := aSet[name]; !ok {
bNotA = append(bNotA, name)
}
}
return aNotB, unionAB, bNotA
}