Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollback function not called automatically #76

Open
oxyno-zeta opened this issue Aug 24, 2021 · 1 comment
Open

Rollback function not called automatically #76

oxyno-zeta opened this issue Aug 24, 2021 · 1 comment

Comments

@oxyno-zeta
Copy link

oxyno-zeta commented Aug 24, 2021

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 !

@oxyno-zeta
Copy link
Author

According to this test:

func TestMigration_WithUseTransactionsShouldRollback(t *testing.T) {

The flag UseTransaction should be set to true to have a rollback but the rollback method isn't called by itself.

The rollback function isn't used at all if not called by RollbackTo or else functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant