Skip to content

Add locking reads, relation path ordering and native upserts - #6

Merged
SoloPHP merged 1 commit into
mainfrom
rc5/application-gaps
Oct 27, 2026
Merged

SoloPHP merged 1 commit into
mainfrom
rc5/application-gaps

Conversation

@SoloPHP

@SoloPHP SoloPHP commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Adds what the dilio backend still wrote by hand before v1.0.0-RC5:

  • locking reads instead of its own LockedRead and ... FOR UPDATE SQL;
  • ordering by a related row's field instead of its own JoinedOrder helper;
  • a native upsert instead of check-then-insert.

Changes

  • ReadQuery::forUpdate() returns the same read as a locking one. find(), findOneBy(), findBy(), rows(), exists() and every batch of iterateById() run SELECT ... FOR UPDATE and lock the rows they read until the transaction ends; outside a transaction they throw LogicException. A locking read never answers from the snapshot of a REPEATABLE READ transaction. MySQL reads the latest committed row. PostgreSQL, and MariaDB with innodb_snapshot_isolation, reject a row changed since the snapshot as a serialization failure.
    • PostgreSQL and MySQL 8 lock only the rows of the read's table (FOR UPDATE OF). Without OF, PostgreSQL cannot lock across the translation LEFT JOINs at all. MariaDB has no OF and also locks the rows the statement joins.
    • Relation loads read without locks. Aggregates and queryBuilder() of a locking read throw LogicException. SQLite runs the plain SELECT.
  • Ordering by relation paths. orderBy accepts dotted paths through BelongsTo relations to a field of the target, such as category.name or category.parent.name. This works in findBy(), findOneBy(), rows(), default relation orders and Load orders.
    • Each target is LEFT JOINed once per statement over the rows that a load of the same relation in the read sees. That is its active rows, or every row when the read loads it with Visibility::All, so a list that shows soft-deleted categories also orders by their names.
    • Translated target fields follow the query locale. Paths through to-many relations are rejected before SQL.
  • Repository::upsert($key, $data, $onInsert = []) inserts the row of a unique key, or updates the row that already holds it, in one native statement: ON CONFLICT on PostgreSQL and SQLite, ON DUPLICATE KEY UPDATE on MySQL and MariaDB.
    • $onInsert values only go into a new row, for example an explicit ID.
    • The key must be the columns of one unique index. On MySQL and MariaDB a key that no unique index covers inserts a duplicate, which the docs state.
    • The conflict clause is shared with translation upserts through Database::upsertClause().
  • Internal reuse:
    • Database::executeLocked() is the one place that renders row locks, for both forUpdate() and lockForUpdate().
    • Relation::joinCondition() replaces keyCondition() and builds the key condition for related filters, string-key loads and order joins.
    • Identifier::isPath() is shared by Load names and order paths.
  • Docs: docs/guide/api.md, details.md, migration.md, README and the changelog section "Changes since 1.0.0-RC4".

Behavior changes for RC4 users

  • insertMany() without a savepoint. A batch that fits one statement now runs without a transaction or savepoint; only a split batch is wrapped in one. Inside a caller's transaction on PostgreSQL, a failure of such a batch now aborts that transaction, as a failed insert() does, instead of rolling back to a savepoint the caller could continue from.
  • Dotted paths in Load and default relation orders. They were rejected when the Load or relation was constructed. Now they are resolved when the read prepares its loads; an unknown relation still fails there, before any owner SQL.
  • Invalid order keys now report "Order must use field names or relation paths to fields."

Verification

  • 638 unit tests, PHPStan max and PSR-12; composer validate --strict; the examples run. The lowest supported DBAL, 4.3.5, also passes.
  • New tests: LockingReadTest (transaction checks and lock clauses per platform), RelationOrderTest (paths, shared joins, translations, soft-deleted targets, visibility from loads, collection orders) and UpsertTest (all three dialects, validation, keyless mappings).
  • Integration suite on MySQL 8.4, MariaDB 12.3 and PostgreSQL 17, 37 tests on each:
    • a second session with NOWAIT checks which rows a locking read holds, including with translation and order joins;
    • a plain read and a locking read are compared in REPEATABLE READ: MySQL returns the committed row, PostgreSQL fails with 40001 and MariaDB with 1020;
    • order paths are checked with translations and soft-deleted targets;
    • upsert() is checked by unique key and by primary key, including a soft-deleted row.
  • The dilio backend moves to RC5 right after the tag, with composer update solophp/repository and its Unit and Feature suites.

🤖 Generated with Claude Code

ReadQuery::forUpdate() turns a read into a locking one: find(),
findOneBy(), findBy(), rows(), exists() and iterateById() run
SELECT ... FOR UPDATE and lock the rows they read until the transaction
ends, and never answer from the snapshot of a REPEATABLE READ
transaction. PostgreSQL and MySQL 8 lock only the rows of the read's
table (FOR UPDATE OF). Aggregates and queryBuilder() of a locking read
are rejected; relation loads read without locks.

orderBy accepts dotted paths through BelongsTo relations to a field of
the target, such as category.name, in reads, default relation orders and
Load orders. Each target is LEFT JOINed once per statement over the rows
a load of the same relation in the read sees, its active rows without
one. Relation::joinCondition() now builds the key condition for related
filters, string-key loads and these joins.

Repository::upsert($key, $data, $onInsert) inserts the row of a unique
key, or updates the row that already holds it, in one native statement.
Its conflict clause is shared with translation upserts.

insertMany() runs a batch that fits one statement without a transaction
or savepoint; only a batch split into several statements is wrapped in
one.
@SoloPHP
SoloPHP merged commit c774271 into main Oct 27, 2026
26 checks passed
@SoloPHP
SoloPHP deleted the rc5/application-gaps branch October 27, 2026 21:46
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