From 016d30d49f3dd11c8e0aadd80f67fcba133f41a2 Mon Sep 17 00:00:00 2001 From: Zengyu Date: Wed, 28 Feb 2024 14:18:21 +0800 Subject: [PATCH] Add pgx/v5 and set isolation to serializable level Following this comment: https://github.com/golang-migrate/migrate/pull/862#issuecomment-1864041631 And reference PR: https://github.com/golang-migrate/migrate/pull/656/files --- database/pgx/pgx.go | 2 +- database/pgx/v5/pgx.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/database/pgx/pgx.go b/database/pgx/pgx.go index 623ac16c3..833c69db4 100644 --- a/database/pgx/pgx.go +++ b/database/pgx/pgx.go @@ -448,7 +448,7 @@ func runesLastIndex(input []rune, target rune) int { } func (p *Postgres) SetVersion(version int, dirty bool) error { - tx, err := p.conn.BeginTx(context.Background(), &sql.TxOptions{}) + tx, err := p.conn.BeginTx(context.Background(), &sql.TxOptions{Isolation: sql.LevelSerializable}) if err != nil { return &database.Error{OrigErr: err, Err: "transaction start failed"} } diff --git a/database/pgx/v5/pgx.go b/database/pgx/v5/pgx.go index 1b5a6ea7a..a487d9a2b 100644 --- a/database/pgx/v5/pgx.go +++ b/database/pgx/v5/pgx.go @@ -339,12 +339,12 @@ func runesLastIndex(input []rune, target rune) int { } func (p *Postgres) SetVersion(version int, dirty bool) error { - tx, err := p.conn.BeginTx(context.Background(), &sql.TxOptions{}) + tx, err := p.conn.BeginTx(context.Background(), &sql.TxOptions{Isolation: sql.LevelSerializable}) if err != nil { return &database.Error{OrigErr: err, Err: "transaction start failed"} } - query := `TRUNCATE ` + quoteIdentifier(p.config.migrationsSchemaName) + `.` + quoteIdentifier(p.config.migrationsTableName) + query := `DELETE FROM ` + quoteIdentifier(p.config.migrationsSchemaName) + `.` + quoteIdentifier(p.config.migrationsTableName) if _, err := tx.Exec(query); err != nil { if errRollback := tx.Rollback(); errRollback != nil { err = multierror.Append(err, errRollback)