Skip to content

orm: add transaction API with savepoint-backed nesting#26707

Merged
medvednikov merged 3 commits intovlang:masterfrom
watzon:feat/add-transaction-support-to-orm
Mar 9, 2026
Merged

orm: add transaction API with savepoint-backed nesting#26707
medvednikov merged 3 commits intovlang:masterfrom
watzon:feat/add-transaction-support-to-orm

Conversation

@watzon
Copy link
Contributor

@watzon watzon commented Mar 9, 2026

Summary

This adds an ORM-level transaction API in vlib/orm. Checks off another task in #24931.

New public API:

  • orm.begin(mut conn) !orm.Tx
  • orm.transaction(mut conn, fn (mut tx orm.Tx) !T) !T

orm.Tx implements orm.Connection, so existing ORM surfaces work inside transactions:

  • sql tx { ... }!
  • orm.new_query[T](tx)

Nested transactions are implemented with savepoints rather than a second top-level transaction.

What changed

  • added orm.TransactionalConnection
  • added orm.Tx and orm.Savepoint
  • added automatic commit/rollback helper flow via orm.transaction(...)
  • added manual transaction control via orm.begin(...)
  • added manual savepoint creation, rollback, and release
  • added driver ORM transaction hooks for SQLite, MySQL, and PostgreSQL
  • added release_savepoint(...) support in the SQLite/MySQL/PostgreSQL backends
  • documented the new transaction API in the ORM README

Behavior

  • root Tx.commit() commits the DB transaction
  • root Tx.rollback() rolls back the DB transaction
  • nested Tx.commit() releases the savepoint
  • nested Tx.rollback() rolls back to the savepoint and releases it
  • transaction/savepoint handles become inactive after close and return clear errors on reuse
  • v1 uses each driver's default transaction mode and does not add ORM transaction syntax inside sql db {} blocks

Tests

Ran:

  • ./vnew -silent test vlib/orm/
  • ./vnew vlib/db/sqlite/sqlite_test.v
  • ./vnew check-md vlib/orm/README.md

Also compiled:

  • ./vnew vlib/db/mysql/mysql_test.v
  • ./vnew vlib/db/pg/pg_test.v

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 41f89349ef

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@medvednikov medvednikov merged commit c3cd33c into vlang:master Mar 9, 2026
78 checks passed
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