Skip to content

Add Database Transaction Handling Utilities for pgx and sqlc#30

Open
dytsou wants to merge 1 commit into
mainfrom
feat/database-with-transaction
Open

Add Database Transaction Handling Utilities for pgx and sqlc#30
dytsou wants to merge 1 commit into
mainfrom
feat/database-with-transaction

Conversation

@dytsou

@dytsou dytsou commented Jul 5, 2026

Copy link
Copy Markdown
Member

Type of changes

  • Feature

Purpose

  • Provide reusable transaction lifecycle helpers for pgx and sqlc call sites
  • Reduce boilerplate around begin, commit, rollback, and WithTx rebinding

Additional Information

  • Adds WithTransaction / TxRunner for raw pgx.Tx callbacks; reuses an existing tx when db is already pgx.Tx
  • Adds WithTransactionQueries / QueriesTx[T] to bind sqlc-generated queries to the active transaction via Run and RunWithDBTx
  • Includes unit tests in pkg/database/tx_test.go and pkg/database/tx_queries_test.go
  • Bumps github.com/jackc/pgx/v5 from v5.7.4 to v5.9.2

Usage examples:

// sqlc-only callback
err := databaseutil.WithTransactionQueries(ctx, pool, logger, queries).
    Run(func(qtx *db.Queries) error {
        return qtx.UpdateUser(ctx, params)
    })

// callback needs both pgx.Tx and sqlc queries
err := databaseutil.WithTransactionQueries(ctx, pool, logger, queries).
    RunWithDBTx(func(tx pgx.Tx, qtx *db.Queries) error {
        return otherService.DoWork(ctx, tx, qtx)
    })

…eriesTx for transaction-scoped queries and implemented test cases for transaction management.
@dytsou dytsou changed the title feat: add database transaction handling utilities Add database transaction handling utilities for pgx and sqlc Jul 5, 2026
@dytsou dytsou changed the title Add database transaction handling utilities for pgx and sqlc Add Database Transaction Handling Utilities for pgx and sqlc Jul 5, 2026
@dytsou dytsou self-assigned this Jul 5, 2026

@yukicoder0509 yukicoder0509 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this pull request introduces new transaction-helper APIs. I think we should document the detailed usage in README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants