fix(bug): database migration does not rollback schema version on hook failure (#5908)

* add tests

* correct expect schema version

* use conventional named return

* add rollback test and more description in godoc

* refactor to use commit at the end and defer rollback

* add comment

* lint
This commit is contained in:
Sorawis Nilparuk (Bo) authored and GitHub committed 2025-07-09 10:11:30 -04:00
1 parent 8b8fb28228
commit d32245a33e
5 files changed
+67 -38

No files matched your search

+8 -8
View File
@@ -360,8 +360,8 @@ func TestApplyMigrationWithHooks(t *testing.T) {
Editions: []schema.EditionState{
{
Name: "hooks",
BinarySchemaVersion: 1001,
DatabaseSchemaVersion: 1001,
BinarySchemaVersion: 2001,
DatabaseSchemaVersion: 2001,
DatabaseSchemaState: schema.Equal,
},
},
@@ -379,7 +379,7 @@ func TestApplyMigrationWithHooks(t *testing.T) {
0,
edition.WithPreHooks(
map[int]*migration.Hook{
1001: {
2001: {
CheckFunc: func(ctx context.Context, tx *sql.Tx) (migration.Problems, error) {
return migration.Problems{"failed"}, nil
},
@@ -393,7 +393,7 @@ func TestApplyMigrationWithHooks(t *testing.T) {
},
nil,
schema.MigrationCheckError{
Version: 1001,
Version: 2001,
Edition: "hooks",
Problems: migration.Problems{"failed"},
RepairDescription: "repair all the things",
@@ -403,7 +403,7 @@ func TestApplyMigrationWithHooks(t *testing.T) {
Editions: []schema.EditionState{
{
Name: "hooks",
BinarySchemaVersion: 1001,
BinarySchemaVersion: 2001,
DatabaseSchemaVersion: 1,
DatabaseSchemaState: schema.Behind,
},
@@ -447,8 +447,8 @@ func TestApplyMigrationWithHooks(t *testing.T) {
Editions: []schema.EditionState{
{
Name: "hooks",
BinarySchemaVersion: 1001,
DatabaseSchemaVersion: 1001,
BinarySchemaVersion: 2001,
DatabaseSchemaVersion: 2001,
DatabaseSchemaState: schema.Equal,
},
},
@@ -494,7 +494,7 @@ func TestApplyMigrationWithHooks(t *testing.T) {
Editions: []schema.EditionState{
{
Name: "hooks",
BinarySchemaVersion: 1001,
BinarySchemaVersion: 2001,
DatabaseSchemaVersion: 1,
DatabaseSchemaState: schema.Behind,
},