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

Add CommitQuery to transaction options #2195

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type TxOptions struct {
// BeginQuery is the SQL query that will be executed to begin the transaction. This allows using non-standard syntax
// such as BEGIN PRIORITY HIGH with CockroachDB. If set this will override the other settings.
BeginQuery string
// CommitQuery is the SQL query that will be executed to commit the transaction.
CommitQuery string
}

var emptyTxOptions TxOptions
Expand Down Expand Up @@ -105,7 +107,10 @@ func (c *Conn) BeginTx(ctx context.Context, txOptions TxOptions) (Tx, error) {
return nil, err
}

return &dbTx{conn: c}, nil
return &dbTx{
conn: c,
commitQuery: txOptions.CommitQuery,
}, nil
}

// Tx represents a database transaction.
Expand Down Expand Up @@ -154,6 +159,7 @@ type dbTx struct {
conn *Conn
savepointNum int64
closed bool
commitQuery string
}

// Begin starts a pseudo nested transaction implemented with a savepoint.
Expand All @@ -177,7 +183,12 @@ func (tx *dbTx) Commit(ctx context.Context) error {
return ErrTxClosed
}

commandTag, err := tx.conn.Exec(ctx, "commit")
commandSQL := "commit"
if tx.commitQuery != "" {
commandSQL = tx.commitQuery
}

commandTag, err := tx.conn.Exec(ctx, commandSQL)
tx.closed = true
if err != nil {
if tx.conn.PgConn().TxStatus() != 'I' {
Expand Down