Skip to content

Stage2 transactions - #10

Merged
Al-Muhandis merged 16 commits into
mainfrom
stage2-transactions
Nov 22, 2026
Merged

Al-Muhandis merged 16 commits into
mainfrom
stage2-transactions

Conversation

@Al-Muhandis

@Al-Muhandis Al-Muhandis commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Stage 2 of the roadmap (Phase 0): define the transactional contract — who owns the transaction, and how explicit units of work are made reliable.

Model. All queries of a connector share one transaction; with FPC SQLdb it is started implicitly by the first statement (a plain SELECT included), Add/Modify/Remove/Execute run immediately inside it, and Apply flushes cached updates and commits. That implicit unit of work has two traps dOPF now addresses explicitly:

  • a forgotten commit rolls the writes back when the connection closes;
  • StartTransaction after any statement found the transaction already active, so explicit transactions were unusable mid-session.

Core (core/dopf.pas):

  • StartTransaction adopts the implicitly started transaction; it raises EdConnection only for a second explicit start. An explicit unit of work can begin anywhere in the session.
  • RunInTransaction(AProc): scoped atomicity — own StartTransaction/Commit with rollback + re-raise on error; if a transaction is already active it joins it (error rolls back the whole transaction, success leaves it to the owner) and marks it explicit.
  • ExplicitTransaction tracking on the connection; StrictTransactions := True requires an explicit transaction for every write — Add, Modify, Remove, Query.Execute, and the cached-update flushes Query.Apply/Query.ApplyRetaining/Query.ApplyUpdates raise EdQuery without one (Codex review addressed in 05f007f). Query-side commit methods without an active transaction are skipped with a logged warning (compatibility mode).
  • brokers/dsqldbbroker.pas unchanged; the contract lives at the TdGQuery/TdGConnection level where it is broker-agnostic.

Compatibility (soft transition). Default behavior is unchanged for existing code: dependent projects calling Apply without opening transactions keep building and running. The strict default is planned to flip in dOPF 4.0.

README: honest Apply semantics, the example wrapped in a transaction, and a Transactions section documenting the model, the helper and the flag.

Tests

  • test_dopf: stub-broker tests for adoption, join/rollback, explicit-transaction lifecycle, compat-mode skip and strict-mode raises (21 tests).
  • test_transactions (new unit, SQLite :memory:): real-backend tests pinning the auto-start semantics, adoption, joins, strict write/flush guards, commit and rollback paths (14 tests).

46/46 console tests pass; demos and gui runner build; a dependent project (LRMirBot, 45 tests) builds and passes against this branch.

A Codex review loop (9 findings, all resolved with tests) hardened the strict-mode write guards: cached-update flushes (Apply/ApplyUpdates), sqoAutoApplyUpdates paths (Post/Delete/Refresh), write statements routed through Open (leading-verb check, comment-aware), and a nil-connection crash. The Open check is best-effort lexically — CTE-wrapped writes are documented as undetectable and belong to Query.Execute.

dOPF never opens or commits transactions on its own; the caller owns the
transaction shared by all queries of a connector. To make that contract
explicit:

- TdGQuery transaction methods (Apply, ApplyRetaining, Undo, UndoRetaining,
  Commit, Rollback) now go through CheckTransaction: they require an active
  transaction. In compatibility mode (default) a missing transaction is
  ignored with a logged warning; with Connection.StrictTransactions it
  raises EdQuery with an actionable message.
- New TdGConnection.RunInTransaction(AProc) helper: StartTransaction,
  AProc, Commit, with Rollback and re-raise on error. Rejects a missing
  callback and nesting with EdConnection.

Behavior of existing code is unchanged unless StrictTransactions is set.
- test_dopf: stub brokers now record StartTransaction/Commit/Rollback and
  Apply calls; new tests cover the compatibility-mode skip, the strict-mode
  EdQuery, forwarding with an active transaction, and RunInTransaction
  commit/rollback/nesting/nil-callback paths.
- test_transactions: SQLite in-memory integration tests (new unit, wired
  into the console and gui runners) pinning real SQLdb semantics: the
  transaction is auto-started by the first statement, Apply commits it,
  strict mode raises without one, RunInTransaction persists or rolls back.
Rewrite the misleading Apply section: statements run immediately, Apply
flushes cached updates and commits the active transaction. Wrap the README
example mutations in StartTransaction/Apply/except Rollback, and add a
Transactions section with the contract, the RunInTransaction helper and the
StrictTransactions flag (compatibility default, to flip in dOPF 4.0).
Testing against a real SQLite connection showed SQLdb starts the shared
transaction implicitly on the first statement (a plain SELECT included),
which made the first iteration of the contract wrong in two ways:

- StartTransaction/RunInTransaction after any prior statement failed with
  'Transaction already active', so explicit transactions only worked as
  the very first action of a session. StartTransaction now adopts the
  implicitly started transaction (raising EdConnection only when the user
  already started one), and RunInTransaction joins an active transaction:
  the callback runs inside it, an error rolls back the whole transaction,
  on success it stays open for its owner to commit.
- StrictTransactions only guarded the commit-side methods, a state that
  is nearly unreachable while SQLdb auto-starts transactions. The
  connection now tracks ExplicitTransaction, and in strict mode every
  write (Add/Modify/Remove/Query.Execute) raises EdQuery unless an
  explicit transaction is active.
Stub tests: StartTransaction adopting an ambient transaction (and the
clear error on a double start), the ExplicitTransaction lifecycle
including lazy reset, and the strict Execute guard. SQLite in-memory
tests: a write blocked in strict mode until StartTransaction, and
RunInTransaction joining (and on error rolling back) the ambient
auto-started transaction.
The implicit unit of work (first statement starts the shared
transaction, Apply commits it, a forgotten commit loses the writes) and
how dOPF makes it explicit: StartTransaction adopts the ambient
transaction, RunInTransaction joins or scopes one, StrictTransactions
requires an explicit transaction for every write.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T17:21:49.409902Z 9a9b2fd Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Copy link
Copy Markdown

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: 6f3b433912

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved
Codex review (PR #10): a SQLdb read leaves the implicit transaction
active, so Query.Apply passed the active-transaction check in strict
mode and could flush and commit cached updates without an explicit
unit; Query.ApplyUpdates forwarded to the broker without any check.

- CheckTransaction gains ARequireExplicit, used by Apply/ApplyRetaining:
  with StrictTransactions they now demand ExplicitTransaction, so an
  implicit (auto-started) transaction is rejected the same way writes
  are; the shared CheckExplicitTransaction helper also guards
  Query.ApplyUpdates and Query.Execute.
- RunInTransaction joining an ambient transaction now marks it explicit,
  like StartTransaction adopting it, so a write inside the joined scope
  passes the strict guard.

Tests: strict Apply after a read (raises until StartTransaction adopts),
strict ApplyUpdates, strict RunInTransaction after a read (joins, adds,
commits). 41 tests pass; LRMirBot (45) unaffected in compatibility mode.
@Al-Muhandis

Copy link
Copy Markdown
Collaborator Author

@codex review

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

Copy link
Copy Markdown

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: 05f007f5ae

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved
Codex re-review (PR #10): with SQLdb's sqoAutoApplyUpdates enabled,
TSQLQuery applies changes during Post or Delete, so Query.Post and
Query.Delete could write inside an implicit transaction even with
StrictTransactions on. Both now go through CheckExplicitTransaction
like the other write paths.

Tests: strict Post/Delete raise at the stub level, and a SQLite test
enables sqoAutoApplyUpdates for real and verifies Post is rejected
outside an explicit transaction. 43 tests pass.
@Al-Muhandis

Copy link
Copy Markdown
Collaborator Author

@codex review

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

Copy link
Copy Markdown

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: 4a3778f310

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved
Codex re-review (PR #10): SQLdb can open statements that return rows,
and some of them write (PostgreSQL 'insert ... returning'), so
Query.Open and Active := True could execute a write inside an implicit
transaction with StrictTransactions enabled.

Both paths now call CheckReadonlyStatement, which inspects the leading
keyword of the SQL text (insert/update/delete/create/begin/...) and
raises EdQuery for write verbs without an explicit transaction. The
statement is not parsed, so CTE-wrapped writes are not detected — that
is documented and such statements belong to Query.Execute.

Test: strict mode opens a plain select, rejects an insert through both
Open and Active := True. 44 tests pass.
@Al-Muhandis

Copy link
Copy Markdown
Collaborator Author

@codex review

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

Copy link
Copy Markdown

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: 3c9c5df19a

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved
Codex re-review (PR #10): the leading verb was cut at the first space
only, so 'insert' followed by a newline or tab slipped past the strict
Open guard. The verb is now taken up to any SQL whitespace or an
opening parenthesis; the test covers a newline-separated statement.
@Al-Muhandis

Copy link
Copy Markdown
Collaborator Author

@codex review

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

Copy link
Copy Markdown

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: e1a494af9c

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved
Codex re-review (PR #10): TDataSet.Refresh posts pending edits before
refetching, and with SQLdb's sqoAutoApplyUpdates that post writes, so
Query.Refresh could write outside an explicit transaction.

The guard is conditional on Modified: a refresh with pending edits
requires an explicit transaction in strict mode, while a plain re-read
stays allowed. Test: with sqoAutoApplyUpdates enabled, strict mode
allows an untouched refresh, rejects one with a pending edit, and
accepts it inside StartTransaction ... Apply. 45 tests pass.
@Al-Muhandis

Copy link
Copy Markdown
Collaborator Author

@codex review

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

Copy link
Copy Markdown

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: d70bd48066

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved

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

Copy link
Copy Markdown

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: d70bd48066

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved
…ection

Two Codex re-review findings (PR #10):

- A leading SQL comment ('-- line', '#' line, '/* block */') made the
  write-verb check read the comment marker as the verb, so
  '-- comment' + newline + 'insert ... returning' slipped through
  Open/Active in strict mode. Leading comments are now skipped before
  the verb is taken.
- CheckExplicitTransaction dereferenced Connection unconditionally, so
  Post/Delete/Refresh on a query created without an owner raised an
  access violation even with strict mode disabled. The guard is a no-op
  without a connection now, preserving the broker-level behavior.

Tests: comment-prefixed insert/update rejected through Open and
Active := True; Post/Delete without a connection keep raising
EdNotImplemented instead of crashing. 46 tests pass.
@Al-Muhandis

Copy link
Copy Markdown
Collaborator Author

@codex review

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

Copy link
Copy Markdown

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: d20b55caed

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved
Codex re-review (PR #10): PostgreSQL nests block comments, so
'/* outer /* inner */ tail */ delete ... returning' stopped the comment
skip at the inner '*/' and treated 'tail' as the leading verb, letting
the write through Open/Active in strict mode. The skip now tracks the
nesting depth; the test rejects a nested-comment insert.
The previous commit did not compile: inside TdGQuery methods an
unqualified Delete(S, 1, 2) resolves to the class's own parameterless
Delete, so the nested-comment skip broke the build (caught while
rebuilding a dependent project; the dopf test run before that had
linked a stale package unit and hid it). The calls are qualified as
System.Delete now; the package, tests, demos and the dependent project
build again, 46 tests pass.
@Al-Muhandis

Copy link
Copy Markdown
Collaborator Author

@codex review

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

Copy link
Copy Markdown

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: 9a9b2fd2fc

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/dopf.pas Resolved
Codex re-review (PR #10): PostgreSQL counts block comments as
whitespace, so 'insert/* note */ into ... returning' made the verb
token 'insert/*' and slipped past the strict Open guard. The verb scan
now also stops at '-', '#' and '/', the inline comment markers; the
test rejects an inline-comment insert.
The transactional contract adds backward-compatible public API
(RunInTransaction, StrictTransactions, ExplicitTransaction) without
changing default behavior, so the minor version rises; the major bump
to 4.0 is reserved for flipping the StrictTransactions default.
@Al-Muhandis
Al-Muhandis merged commit f4b70a8 into main Nov 22, 2026
2 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.

1 participant