fix(memory): make save_compression_guidelines version bump atomic#1810
Merged
fix(memory): make save_compression_guidelines version bump atomic#1810
Conversation
) Replace the read-then-write version increment with a single atomic INSERT ... SELECT COALESCE(MAX(version), 0) + 1 statement. SQLite's single-writer WAL guarantee makes this safe without an explicit transaction; two concurrent callers can no longer both observe version=N and both insert version=N+1. Migration 033 adds a UNIQUE(version) constraint to the compression_guidelines table. To handle databases that already contain duplicate version rows from the original bug, the migration deduplicates by keeping the row with the highest rowid per version before applying the constraint. Tests added: - unique_constraint_prevents_duplicate_version: raw INSERT of a duplicate version must fail with a UNIQUE constraint error - concurrent_saves_produce_unique_versions: 8 concurrent writers on a shared file-backed WAL database all produce distinct version numbers Closes #1799
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
save_compression_guidelineswith a single atomicINSERT ... SELECT COALESCE(MAX(version), 0) + 1statementUNIQUE(version)constraint oncompression_guidelines, with row-level deduplication for pre-existing corrupt dataunique_constraint_prevents_duplicate_versionandconcurrent_saves_produce_unique_versions(file-backed WAL DB, 8 concurrent writers)Closes #1799
Test plan
cargo nextest run --config-file .github/nextest.toml -p zeph-memory --lib -E 'test(compression_guidelines)'— 10 passedcargo nextest run --config-file .github/nextest.toml --workspace --features full --lib --bins— 5789 passedcargo clippy --workspace --features full -- -D warnings— cleancargo +nightly fmt --check— clean