fix(core): harden merge rules and validation found in review

- page_tree shows orphans and parent cycles (from concurrent moves) as roots;
  descendants CTE and move_block ancestor walk are cycle-safe
- title conflicts compare titles only (not aliases), journals keep their title,
  and loser suffixes use the random id tail so they cannot collide
- aliases get an HLC and tombstones (last-writer-wins)
- remote page deletes cascade to blocks; LastChildOf validates the parent
- reject empty and date-shaped titles on create/rename; ensure_page redirects
  dates to journals; rename rewrites refs inside block properties
- single-row writes run inside savepoints; invalid remote asset hashes are
  ignored and never reach blob paths; HLC counter cannot overflow its width
- busy_timeout for multi-connection use; schema v2 migration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-10-21 14:33:22 +02:00
1 parent 3266ddda3a
commit f9137c9896
6 files changed
+362 -29

No files matched your search

+18 -2
View File
@@ -1,4 +1,4 @@
pub const VERSION: i64 = 1;
pub const VERSION: i64 = 2;
pub const SCHEMA: &str = r#"
CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
@@ -20,7 +20,8 @@ CREATE INDEX IF NOT EXISTS pages_origin ON pages(origin_journal);
CREATE TABLE IF NOT EXISTS aliases (
alias_key TEXT PRIMARY KEY,
alias TEXT NOT NULL,
page_id TEXT NOT NULL
page_id TEXT NOT NULL,
hlc TEXT NOT NULL DEFAULT ''
);
CREATE INDEX IF NOT EXISTS aliases_page ON aliases(page_id);
@@ -90,3 +91,18 @@ CREATE TABLE IF NOT EXISTS ops (
payload TEXT NOT NULL
);
"#;
/// Upgrades databases created by older versions in place.
pub fn migrate(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
let has_alias_hlc: bool = conn
.prepare("SELECT 1 FROM pragma_table_info('aliases') WHERE name='hlc'")?
.exists([])?;
if !has_alias_hlc {
conn.execute_batch("ALTER TABLE aliases ADD COLUMN hlc TEXT NOT NULL DEFAULT ''")?;
}
conn.execute(
"INSERT OR REPLACE INTO meta(key, value) VALUES ('schema', ?1)",
[VERSION.to_string()],
)?;
Ok(())
}