Repository navigation
Add locking reads, relation path ordering and native upserts - #6
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds what the dilio backend still wrote by hand before
v1.0.0-RC5:LockedReadand... FOR UPDATESQL;JoinedOrderhelper;Changes
ReadQuery::forUpdate()returns the same read as a locking one.find(),findOneBy(),findBy(),rows(),exists()and every batch ofiterateById()runSELECT ... FOR UPDATEand lock the rows they read until the transaction ends; outside a transaction they throwLogicException. A locking read never answers from the snapshot of a REPEATABLE READ transaction. MySQL reads the latest committed row. PostgreSQL, and MariaDB withinnodb_snapshot_isolation, reject a row changed since the snapshot as a serialization failure.FOR UPDATE OF). WithoutOF, PostgreSQL cannot lock across the translation LEFT JOINs at all. MariaDB has noOFand also locks the rows the statement joins.queryBuilder()of a locking read throwLogicException. SQLite runs the plain SELECT.orderByaccepts dotted paths throughBelongsTorelations to a field of the target, such ascategory.nameorcategory.parent.name. This works infindBy(),findOneBy(),rows(), default relation orders andLoadorders.Visibility::All, so a list that shows soft-deleted categories also orders by their names.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 CONFLICTon PostgreSQL and SQLite,ON DUPLICATE KEY UPDATEon MySQL and MariaDB.$onInsertvalues only go into a new row, for example an explicit ID.Database::upsertClause().Database::executeLocked()is the one place that renders row locks, for bothforUpdate()andlockForUpdate().Relation::joinCondition()replaceskeyCondition()and builds the key condition for related filters, string-key loads and order joins.Identifier::isPath()is shared byLoadnames and order paths.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 failedinsert()does, instead of rolling back to a savepoint the caller could continue from.Loadand default relation orders. They were rejected when theLoador relation was constructed. Now they are resolved when the read prepares its loads; an unknown relation still fails there, before any owner SQL.Verification
composer validate --strict; the examples run. The lowest supported DBAL, 4.3.5, also passes.LockingReadTest(transaction checks and lock clauses per platform),RelationOrderTest(paths, shared joins, translations, soft-deleted targets, visibility from loads, collection orders) andUpsertTest(all three dialects, validation, keyless mappings).NOWAITchecks which rows a locking read holds, including with translation and order joins;upsert()is checked by unique key and by primary key, including a soft-deleted row.composer update solophp/repositoryand its Unit and Feature suites.🤖 Generated with Claude Code