From 7b168eabfe0f844bcbf8dc89629d04c385b9f58c Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Mon, 30 May 2022 10:42:19 +0300 Subject: [PATCH] fix(migrate): close conn/tx on error --- migrate/migration.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/migrate/migration.go b/migrate/migration.go index 05bd6006c..39a15adf3 100644 --- a/migrate/migration.go +++ b/migrate/migration.go @@ -89,6 +89,22 @@ func NewSQLMigrationFunc(fsys fs.FS, name string) MigrationFunc { idb = conn } + var retErr error + + defer func() { + if tx, ok := idb.(bun.Tx); ok { + retErr = tx.Commit() + return + } + + if conn, ok := idb.(bun.Conn); ok { + retErr = conn.Close() + return + } + + panic("not reached") + }() + for _, q := range queries { _, err = idb.ExecContext(ctx, q) if err != nil { @@ -96,13 +112,7 @@ func NewSQLMigrationFunc(fsys fs.FS, name string) MigrationFunc { } } - if tx, ok := idb.(bun.Tx); ok { - return tx.Commit() - } else if conn, ok := idb.(bun.Conn); ok { - return conn.Close() - } - - panic("not reached") + return retErr } }