Skip to content

Commit 5c78bfd

Browse files
authored
Allow to skip long-running migration (#54)
1 parent 4f80912 commit 5c78bfd

File tree

8 files changed

+91
-71
lines changed

8 files changed

+91
-71
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ module src.techknowlogick.com/xormigrate
33
go 1.14
44

55
require (
6-
cloud.google.com/go v0.37.4 // indirect
76
github.com/denisenkom/go-mssqldb v0.10.0
87
github.com/go-sql-driver/mysql v1.6.0
98
github.com/goccy/go-json v0.9.11 // indirect
109
github.com/joho/godotenv v1.3.0
1110
github.com/lib/pq v1.10.2
1211
github.com/mattn/go-sqlite3 v1.14.9
1312
github.com/stretchr/testify v1.7.0
14-
google.golang.org/appengine v1.6.0 // indirect
1513
xorm.io/builder v0.3.12 // indirect
1614
xorm.io/xorm v1.3.2
1715
)

go.sum

Lines changed: 31 additions & 69 deletions
Large diffs are not rendered by default.

mysql_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build mysql
12
// +build mysql
23

34
package xormigrate

postgresql_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build postgresql
12
// +build postgresql
23

34
package xormigrate

sqlite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build sqlite
12
// +build sqlite
23

34
package xormigrate

sqlserver_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build sqlserver
12
// +build sqlserver
23

34
package xormigrate

xormigrate.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ type Migration struct {
3030
Migrate MigrateFunc `xorm:"-"`
3131
// Rollback will be executed on rollback. Can be nil.
3232
Rollback RollbackFunc `xorm:"-"`
33+
// Long marks the migration an non-required migration that will likely take a long time. Must use Xormigrate.AllowLong() to be enabled.
34+
Long bool `xorm:"-"`
3335
}
3436

3537
// Xormigrate represents a collection of all migrations of a database schema.
3638
type Xormigrate struct {
3739
db *xorm.Engine
3840
migrations []*Migration
3941
initSchema InitSchemaFunc
42+
allowLong bool
4043
}
4144

4245
// ReservedIDError is returned when a migration is using a reserved ID
@@ -82,6 +85,7 @@ func New(db *xorm.Engine, migrations []*Migration) *Xormigrate {
8285
return &Xormigrate{
8386
db: db,
8487
migrations: migrations,
88+
allowLong: false,
8589
}
8690
}
8791

@@ -93,6 +97,18 @@ func (x *Xormigrate) InitSchema(initSchema InitSchemaFunc) {
9397
x.initSchema = initSchema
9498
}
9599

100+
// InitSchema sets a function that is run if no migration is found.
101+
// The idea is preventing to run all migrations when a new clean database
102+
// is being migratinx. In this function you should create all tables and
103+
// foreign key necessary to your application.
104+
func (x *Xormigrate) AllowLong(allow ...bool) {
105+
allowLong := true
106+
if len(allow) > 0 {
107+
allowLong = allow[0]
108+
}
109+
x.allowLong = allowLong
110+
}
111+
96112
// Migrate executes all migrations that did not run yet.
97113
func (x *Xormigrate) Migrate() error {
98114
return x.migrate("")
@@ -128,6 +144,9 @@ func (x *Xormigrate) migrate(migrationID string) error {
128144
}
129145

130146
for _, migration := range x.migrations {
147+
if migration.Long && !x.allowLong {
148+
continue
149+
}
131150
if err := x.runMigration(migration); err != nil {
132151
return err
133152
}

xormigrate_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,43 @@ func TestEmptyMigrationList(t *testing.T) {
318318
})
319319
}
320320

321+
func TestAllowLong(t *testing.T) {
322+
forEachDatabase(t, func(db *xorm.Engine) {
323+
t.Run("without AllowLong", func(t *testing.T) {
324+
m := New(db, []*Migration{{
325+
ID: "201608301430",
326+
Long: true,
327+
Migrate: func(tx *xorm.Engine) error {
328+
return tx.Sync2(&Pet{})
329+
},
330+
Rollback: func(tx *xorm.Engine) error {
331+
return tx.DropTables(&Pet{})
332+
},
333+
}})
334+
err := m.Migrate()
335+
assert.Nil(t, err)
336+
assert.Equal(t, int64(0), tableCount(t, db))
337+
})
338+
339+
t.Run("with AllowLong", func(t *testing.T) {
340+
m := New(db, []*Migration{{
341+
ID: "201608301430",
342+
Long: true,
343+
Migrate: func(tx *xorm.Engine) error {
344+
return tx.Sync2(&Pet{})
345+
},
346+
Rollback: func(tx *xorm.Engine) error {
347+
return tx.DropTables(&Pet{})
348+
},
349+
}})
350+
m.AllowLong(true)
351+
err := m.Migrate()
352+
assert.Nil(t, err)
353+
assert.Equal(t, int64(1), tableCount(t, db))
354+
})
355+
})
356+
}
357+
321358
func tableCount(t *testing.T, db *xorm.Engine) (count int64) {
322359
count, err := db.Count(&Migration{})
323360
assert.NoError(t, err)

0 commit comments

Comments
 (0)