Skip to content

Add a shared DB transaction helper to internal/db/db.go #133

Description

@Jagadeeshftw

📌 Description

Several handlers likely hand-roll BEGIN/COMMIT/ROLLBACK transaction boilerplate around pgx calls. A small WithTx(ctx, fn) helper in internal/db/db.go would reduce duplication and the risk of a forgotten rollback-on-error path.

🧩 Requirements and context

  • Add a WithTx(ctx context.Context, fn func(pgx.Tx) error) error helper that commits on nil error and rolls back otherwise.
  • Ensure panics inside fn still trigger a rollback (recover + re-panic).
  • Migrate at least one existing multi-statement handler to use the new helper as a proof of adoption.

🛠️ Suggested execution

  • Add the helper to internal/db/db.go.
  • Add a unit test covering commit, explicit error rollback, and panic-triggers-rollback.
  • Refactor one handler (e.g. in internal/handlers/) that currently hand-rolls a transaction.

✅ Acceptance criteria

  • WithTx helper exists with commit/rollback/panic-safety tests.
  • At least one handler is migrated to use it.
  • No behavior change for the migrated handler's happy path.

🔒 Security notes

Reduces the risk of a forgotten rollback leaving a transaction open/locked under error conditions, which is a minor availability concern under load.

📋 Guidelines

  • Minimum 95% test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions