Repository navigation
Description
Background
perima uses refinery 0.8 reading numbered SQL migration files (crates/db/migrations/V001..V010) for SQLite schema management via the rusqlite 0.39 driver. Pre-v1, the project has zero deployed user databases and a single solo developer; post-v1, mobile (UniFFI + Expo) and an Obsidian plugin (axum HTTP API) will share the same schema, with multi-device CRDT sync via Loro.
The question of whether to drop migrations entirely (collapsing to a single canonical idempotent schema), keep migrations as-is, or adopt a hybrid was investigated externally during Batch F design. This issue archives the findings for the post-v1 reopen point when the multi-device sync story changes the calculus.
Research recommendation: HYBRID
Keep refinery for table and index DDL; generate FTS5 trigger DDL idempotently at boot via templated codegen (no version numbers for trigger changes). Adopted by Batch F.
Key tradeoff (the deciding argument)
Multi-device CRDT sync requires a queryable schema-version oracle. When peer devices on different binary versions reconcile, they need to detect schema mismatch before exchanging mutations. refinery_schema_history's SELECT MAX(version) provides that oracle for free. A single-canonical-schema approach has no version surface — peers cannot negotiate compatibility.
This argument applies to table/index shape, which is what CRDT peers actually need to agree on. FTS5 trigger bodies are derived from table shape and do not participate in peer negotiation, so they can live outside refinery.
Evidence (with sources)
- Declarative schema migration is a documented pattern but limited to additive changes (CREATE TABLE / ADD COLUMN). Structural changes still need explicit migration steps. — https://simonwillison.net/2022/May/3/simple-declarative-schema-migration-for-sqlite/ (2022-05-03)
- Cargo (the Rust toolchain) uses an explicit
Migrationtype in its SQLite util module — production-grade Rust+SQLite projects choose versioned migrations. — https://doc.rust-lang.org/nightly/nightly-rustc/cargo/util/sqlite/type.Migration.html - SQLite natively supports idempotent trigger DDL:
DROP TRIGGER IF EXISTS+CREATE TRIGGERare documented and safe to run on every boot. — https://www.sqlite.org/lang_createtrigger.html, https://www.sqlite.org/lang_droptrigger.html - Refinery announced 2019 (HN: https://news.ycombinator.com/item?id=21752410) and remains the ecosystem default for Rust+SQLite anything requiring schema evolution. No strong counter-movement found in 2024-2025 search results.
- Lobsters + HN community consensus (2022, referenced into 2024): declarative/canonical schema preferred for rapid solo iteration; explicit migrations win when multiple deployed versions or schema divergence scenarios exist. — https://lobste.rs/s/iypstr/simple_declarative_schema_migration_for, https://news.ycombinator.com/item?id=31249823
Peer projects
- Cargo (Rust toolchain) — explicit migrations.
- Atuin (shell history, Rust+SQLite) — uses a migration system per their forum thread on history DB updates. Source: https://github.com/atuinsh/atuin
- sqlite-migrate (Simon Willison, Python) — migration-first pattern from a major SQLite OSS contributor. https://github.com/simonw/sqlite-migrate
Lighter alternatives to refinery (for future evaluation)
- rusqlite_migration — uses SQLite's
user_versionpragma instead of a migration table. Simpler API, no CLI, no SQL file embedding. — https://github.com/cljoly/rusqlite_migration- Trade-off: switching cost pre-v1 is manageable but not free; loses refinery's richer feature set.
Risks of the hybrid approach (to mitigate)
- Two schema management systems (refinery for tables/indexes, boot-time codegen for triggers). Requires inviolable rule: triggers NEVER go in refinery files; codegen is authoritative for all trigger DDL.
- Trigger drift detection: without rule Upgrade actions/checkout to v5 (Node 24 deprecation) #1, a trigger could exist in both systems with diverging definitions. Insta snapshot tests are the safeguard.
- No published prior art for this exact hybrid in Rust+SQLite — bespoke approach. Mitigated by inline documentation.
- Refinery squash window: collapsing V001..V0NN into a single canonical V001 is painless pre-v1 (zero live user DBs). Post-v1, the fingerprint must match every live DB's already-applied state — doable but fingerprint-fragile.
- Minijinja template correctness: generated SQL must be regression-tested via insta snapshots; CI must enforce snapshot freshness.
Cost of "add migrations later" for perima specifically
Zero cost today (no deployed user databases). Post-v1, a belated V001 migration must be a CREATE TABLE IF NOT EXISTS matching the schema state of every live DB. Doable but fingerprint-fragile.
When to reopen this issue
- When mobile (UniFFI + Expo) ships and multi-device sync goes live.
- When the FTS5 trigger codegen approach (Batch F) reveals limitations that the boot-time install pattern cannot handle.
- If a published Rust+SQLite project demonstrates a successful migration-less approach at scale.
Activity
arch-audit v0.6.x — checkpoint: Batches F→J landed (#136)