From 44477a6dbd1398657c892d649b364439739c473c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 1 Jun 2024 01:25:16 +0200 Subject: [golang] fixed sql migrations --- golang/pkg/database/migrations.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'golang/pkg/database/migrations.go') diff --git a/golang/pkg/database/migrations.go b/golang/pkg/database/migrations.go index 94207a5..81cebaa 100644 --- a/golang/pkg/database/migrations.go +++ b/golang/pkg/database/migrations.go @@ -61,15 +61,22 @@ func DBInit(ctx context.Context, url string) (myDB *DB, err error) { return nil, err } + tx, err := db.Begin() + if err != nil { + return nil, err + } for version < len(statements) { - if _, err = db.ExecContext(ctx, statements[version]); err != nil { + if _, err = tx.ExecContext(ctx, statements[version]); err != nil { + tx.Rollback() return nil, err } version++ } - if _, err = db.ExecContext(ctx, `DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (?);`, version); err != nil { + if _, err = tx.ExecContext(ctx, `DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (?);`, version); err != nil { + tx.Rollback() return nil, err } + tx.Commit() return &DB{ctx: ctx, db: db}, nil } -- cgit v1.2.3