Repository navigation
fix: reject PK-changing UPDATEs onto an existing key and enforce UNIQUE constraints - #517
Merged
CastleSoft merged 1 commit intoNov 16, 2026
Conversation
…UE constraints (directory storage) Known issue 8: an UPDATE that moved a primary key onto an existing key was accepted; the PK index was re-pointed at the moved row and the original row became unreachable (silent data loss). Every UPDATE path - UpdateAffectedCount (SQL), UpdateMultipleCore (ExecuteBatchSQL, structured batch API) and UpdateByPrimaryKey - now validates new key values before any row is written, tracking what earlier operations of a batch claim and release; a SET of the key matching several rows is rejected too. Known issue 9: a UNIQUE column constraint was stored but never checked (the INSERT-time check was a stub). A single-column UNIQUE now gets an automatic unique hash index (as SQLite's sqlite_autoindex_*), registered at CREATE TABLE and on every open. One validator covers single-row INSERT, both batch critical sections and every UPDATE path; NULLs never conflict. On the way: a UNIQUE index was enforced only by a single-row INSERT into a columnar table (not page-based, not multi-row, not UPDATE), all three HashIndex add paths rejected a second NULL, and a table with a unique index gave up the atomic batched multi-row INSERT for a per-row loop. The batch core enforces unique indexes now, so CanUseBatchedInsert no longer excludes them; the test that pinned the old fallback is replaced by one asserting the batch path is used and still rejects a duplicate. Every check starts with an allocation-free test that returns at once when a table or statement has no key to protect. Not covered: single-file storage (its persisted Unique flag is also written for every inline PRIMARY KEY column, so it cannot tell a declared UNIQUE from an EF Core composite key's column - tried and backed out when the EF CompositeKey tests failed; known issues 10/11) and composite UNIQUE (a, b) on INSERT. Tests: PrimaryKeyUpdateTests, UniqueConstraintTests (columnar, page-based), red before. Suites: core 2392/0 (6 skipped), VectorSearch 255/0, EF Core 116/0, linq2DB 96/0, canaries 51/0. Write-path gate: PASSED, every metric ok.
|
CastleSoft
merged commit Nov 16, 2026
8c43fe6
into
fix/linq2db-adapter-and-scalar-functions
2 checks passed
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.



Summary
Fixes known issues §8 and §9 from
docs/sharpcoredb-known-issues.mdon directory storage, plus three defects found on the way. Every fix has a test that was run red before it and green after.§8 — an UPDATE that moved a PRIMARY KEY onto an existing key lost a row
UPDATE t SET id = 1 WHERE id = 3with a row 1 present was accepted: the primary-key index was re-pointed at the moved row and the original row 1 became unreachable (silent data loss).Every UPDATE path now validates new key values before any row is written:
Table.UpdateAffectedCount(SQL UPDATE)UpdateMultipleCore(ExecuteBatchSQL, structured batch API), tracking what earlier operations in the batch claim and releaseUpdateByPrimaryKeyA SET of the key that matches more than one row is rejected too. Pinned by
PrimaryKeyUpdateTests(columnar and page-based, in session and after reopen).§9 — a UNIQUE constraint was never enforced
email TEXT UNIQUE(orUNIQUE (email)) accepted duplicates; the INSERT-time check was a stub. A single-column UNIQUE now gets an automatic unique hash index, as SQLite'ssqlite_autoindex_…, registered at CREATE TABLE and on every open (existing tables are covered; registration builds nothing, so rows stored earlier never block an open). One validator covers single-row INSERT, both batch critical sections and every UPDATE path. NULLs never conflict. Pinned byUniqueConstraintTests(columnar and page-based).Found on the way
HashIndexadd paths rejected a second NULL as a duplicateDBNullkeys skip the uniqueness checkCanUseBatchedInsertno longer excludes themChanged test:
MultiRowInsertBatchingTests.CanUseBatchedInsert_IsFalse_WithUniqueSecondaryIndexpinned the old fallback, which existed only because the batch core did not enforce unique indexes. It is replaced byCanUseBatchedInsert_IsTrue_WithUniqueSecondaryIndex_BecauseTheBatchCoreEnforcesIt, which asserts the batch path is used and still rejects a duplicate keeping no row of the statement.Performance
Every check starts with an allocation-free test that returns at once when a table or statement has no key to protect (the capturing lambdas live in separate
*Corehelpers so no closure is allocated on the fast path).Write-path gate (
--gate, Release, alone on the box): GATE PASSED, every metricok, nothing slower than baseline × 1.50. The box wasNOISY—WSearchand the Defender exclusion need elevation; build servers were shut down withquiet-machine.ps1 -Apply.(ratio = baseline ÷ current; > 1 is slower.) The added work for an UPDATE that touches no key is one dictionary miss plus a scan of a one- or two-entry SET list per operation; the untouched DELETE path moved by a similar amount, so the spread reads as machine noise.
Not covered (recorded in the known-issues document)
ColumnFlags.Uniqueis also written for every inlinePRIMARY KEYcolumn (DatabaseExtensions: "PRIMARY KEY implies UNIQUE"), so a reopened file cannot tell a declared UNIQUE from an EF Core composite key's column. Enforcing it was implemented and backed out whenCompleteExampleIntegrationTests.CompositeKey_*failed. Same column-entry format decision as §10/§11; §8's single-file half (a duplicated key, not a lost row) is tracked in §11.UNIQUE (a, b)on INSERT.Tests
AGENTS.mdlist) 51 / 0SharpCoreDB.CI.slnfbuilds with 0 errors