mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-06-08 23:26:11 +02:00
Merge pull request #26 from odenio/master
Add option that makes migrator only mark migrations applied when migration successful
This commit is contained in:
commit
d7b10ec656
@ -27,14 +27,23 @@ func WithLocksTableName(table string) MigratorOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithMarkAppliedOnSuccess sets the migrator to only mark migrations as applied/unapplied
|
||||||
|
// when their up/down is successful
|
||||||
|
func WithMarkAppliedOnSuccess() MigratorOption {
|
||||||
|
return func(m *Migrator) {
|
||||||
|
m.markAppliedOnSuccess = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Migrator struct {
|
type Migrator struct {
|
||||||
db *ch.DB
|
db *ch.DB
|
||||||
migrations *Migrations
|
migrations *Migrations
|
||||||
|
|
||||||
ms MigrationSlice
|
ms MigrationSlice
|
||||||
|
|
||||||
table string
|
table string
|
||||||
locksTable string
|
locksTable string
|
||||||
|
markAppliedOnSuccess bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMigrator(db *ch.DB, migrations *Migrations, opts ...MigratorOption) *Migrator {
|
func NewMigrator(db *ch.DB, migrations *Migrations, opts ...MigratorOption) *Migrator {
|
||||||
@ -149,9 +158,10 @@ func (m *Migrator) Migrate(ctx context.Context, opts ...MigrationOption) (*Migra
|
|||||||
migration := &group.Migrations[i]
|
migration := &group.Migrations[i]
|
||||||
migration.GroupID = group.ID
|
migration.GroupID = group.ID
|
||||||
|
|
||||||
// Always mark migration as applied so the rollback has a chance to fix the database.
|
if !m.markAppliedOnSuccess {
|
||||||
if err := m.MarkApplied(ctx, migration); err != nil {
|
if err := m.MarkApplied(ctx, migration); err != nil {
|
||||||
return nil, err
|
return group, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cfg.nop && migration.Up != nil {
|
if !cfg.nop && migration.Up != nil {
|
||||||
@ -159,6 +169,12 @@ func (m *Migrator) Migrate(ctx context.Context, opts ...MigrationOption) (*Migra
|
|||||||
return group, err
|
return group, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if m.markAppliedOnSuccess {
|
||||||
|
if err := m.MarkApplied(ctx, migration); err != nil {
|
||||||
|
return group, err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return group, nil
|
return group, nil
|
||||||
@ -186,14 +202,21 @@ func (m *Migrator) Rollback(ctx context.Context, opts ...MigrationOption) (*Migr
|
|||||||
for i := len(lastGroup.Migrations) - 1; i >= 0; i-- {
|
for i := len(lastGroup.Migrations) - 1; i >= 0; i-- {
|
||||||
migration := &lastGroup.Migrations[i]
|
migration := &lastGroup.Migrations[i]
|
||||||
|
|
||||||
// Always mark migration as unapplied to match migrate behavior.
|
if !m.markAppliedOnSuccess {
|
||||||
if err := m.MarkUnapplied(ctx, migration); err != nil {
|
if err := m.MarkUnapplied(ctx, migration); err != nil {
|
||||||
return nil, err
|
return lastGroup, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cfg.nop && migration.Down != nil {
|
if !cfg.nop && migration.Down != nil {
|
||||||
if err := migration.Down(ctx, m.db); err != nil {
|
if err := migration.Down(ctx, m.db); err != nil {
|
||||||
return nil, err
|
return lastGroup, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.markAppliedOnSuccess {
|
||||||
|
if err := m.MarkUnapplied(ctx, migration); err != nil {
|
||||||
|
return lastGroup, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user