@@ -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.
3638type 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.
97113func (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 }
0 commit comments