Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ module src.techknowlogick.com/xormigrate
go 1.14

require (
cloud.google.com/go v0.37.4 // indirect
github.com/denisenkom/go-mssqldb v0.10.0
github.com/go-sql-driver/mysql v1.6.0
github.com/goccy/go-json v0.9.11 // indirect
github.com/joho/godotenv v1.3.0
github.com/lib/pq v1.10.2
github.com/mattn/go-sqlite3 v1.14.9
github.com/stretchr/testify v1.7.0
google.golang.org/appengine v1.6.0 // indirect
xorm.io/builder v0.3.12 // indirect
xorm.io/xorm v1.3.2
)
100 changes: 31 additions & 69 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions mysql_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build mysql
// +build mysql

package xormigrate
Expand Down
1 change: 1 addition & 0 deletions postgresql_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build postgresql
// +build postgresql

package xormigrate
Expand Down
1 change: 1 addition & 0 deletions sqlite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build sqlite
// +build sqlite

package xormigrate
Expand Down
1 change: 1 addition & 0 deletions sqlserver_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build sqlserver
// +build sqlserver

package xormigrate
Expand Down
19 changes: 19 additions & 0 deletions xormigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ type Migration struct {
Migrate MigrateFunc `xorm:"-"`
// Rollback will be executed on rollback. Can be nil.
Rollback RollbackFunc `xorm:"-"`
// Long marks the migration an non-required migration that will likely take a long time. Must use Xormigrate.AllowLong() to be enabled.
Long bool `xorm:"-"`
}

// Xormigrate represents a collection of all migrations of a database schema.
type Xormigrate struct {
db *xorm.Engine
migrations []*Migration
initSchema InitSchemaFunc
allowLong bool
}

// ReservedIDError is returned when a migration is using a reserved ID
Expand Down Expand Up @@ -82,6 +85,7 @@ func New(db *xorm.Engine, migrations []*Migration) *Xormigrate {
return &Xormigrate{
db: db,
migrations: migrations,
allowLong: false,
}
}

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

// InitSchema sets a function that is run if no migration is found.
// The idea is preventing to run all migrations when a new clean database
// is being migratinx. In this function you should create all tables and
// foreign key necessary to your application.
func (x *Xormigrate) AllowLong(allow ...bool) {
Comment on lines +100 to +104
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from InitSchema

allowLong := true
if len(allow) > 0 {
allowLong = allow[0]
}
x.allowLong = allowLong
}

// Migrate executes all migrations that did not run yet.
func (x *Xormigrate) Migrate() error {
return x.migrate("")
Expand Down Expand Up @@ -128,6 +144,9 @@ func (x *Xormigrate) migrate(migrationID string) error {
}

for _, migration := range x.migrations {
if migration.Long && !x.allowLong {
continue
}
if err := x.runMigration(migration); err != nil {
return err
}
Expand Down
37 changes: 37 additions & 0 deletions xormigrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,43 @@ func TestEmptyMigrationList(t *testing.T) {
})
}

func TestAllowLong(t *testing.T) {
forEachDatabase(t, func(db *xorm.Engine) {
t.Run("without AllowLong", func(t *testing.T) {
m := New(db, []*Migration{{
ID: "201608301430",
Long: true,
Migrate: func(tx *xorm.Engine) error {
return tx.Sync2(&Pet{})
},
Rollback: func(tx *xorm.Engine) error {
return tx.DropTables(&Pet{})
},
}})
err := m.Migrate()
assert.Nil(t, err)
assert.Equal(t, int64(0), tableCount(t, db))
})

t.Run("with AllowLong", func(t *testing.T) {
m := New(db, []*Migration{{
ID: "201608301430",
Long: true,
Migrate: func(tx *xorm.Engine) error {
return tx.Sync2(&Pet{})
},
Rollback: func(tx *xorm.Engine) error {
return tx.DropTables(&Pet{})
},
}})
m.AllowLong(true)
err := m.Migrate()
assert.Nil(t, err)
assert.Equal(t, int64(1), tableCount(t, db))
})
})
}

func tableCount(t *testing.T, db *xorm.Engine) (count int64) {
count, err := db.Count(&Migration{})
assert.NoError(t, err)
Expand Down