Skip to content

Rollback function not called automatically #76

@oxyno-zeta

Description

@oxyno-zeta

Hi everybody,

Rollback isn't executed when an error occured in the migration script. The issue have been raised in #42 but the solution provided isn't working as expected because RollbackLast only rollback the last succeed migration, not the last that have failed :( .

Dependencies

gorm.io (so v2) version: v1.21.12
go migrate version: v2.0.0
postgresql 12
go version: 1.16

Migrations

package main

var Migrations = []*gormigrate.Migration{
   {
		ID: "2021082409555553",
		Migrate: func(tx *gorm.DB) error {
			return tx.Exec(`ALTER TABLE "todos" ALTER COLUMN "text" TYPE varchar(200)`).Error
		},
		Rollback: func(tx *gorm.DB) error {
			return tx.Exec(`ALTER TABLE "MMMMMMM" ALTER COLUMN "text" TYPE varchar(200)`).Error
		},
	},
}

Execute Migrate

package main

func Migrate(db *gorm.DB) error {
	m := gormigrate.New(db, gormigrate.DefaultOptions, Migrations)

	return m.Migrate()
}

Actual

The migration function is called, fails because the todos table doesn't exist and the rollback function isn't called.

Note: Logs have been wrapped with logrus

DEBU[0000] SELECT count(*) FROM "migrations" WHERE id = '2021082409555553'  error="<nil>" rows=1
DEBU[0000] ALTER TABLE "todos" ALTER COLUMN "text" TYPE varchar(200)  error="ERROR: relation \"todos\" does not exist (SQLSTATE 42P01)" rows=0
FATA[0000] ERROR: relation "todos" does not exist (SQLSTATE 42P01)  error="ERROR: relation \"todos\" does not exist (SQLSTATE 42P01)"

Expectations

The first ALTER TABLE will fail because the table doesn't exists and then Rollback function must be called.

Anybody have a clue ?

Thanks !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions