SPB Git

Repository Public License

0002-sqlite-without-dependencies.md 4.0 KB 68 lines markdown

1# ADR 0002 — Storage on the system SQLite, without a third-party library23Status: accepted (2026-11-25). Module: `Persistence`.45## Context67ORIX Navigator stores sessions (windows, tabs), history, bookmarks, downloads, permissions, notes, canvases, agent8runs, workflows, sync state and settings. The requirements that shaped the choice:910- the window/tab state must survive a crash, and saving it must be cheap enough to do continuously;11- the omnibox needs full-text search and ranked suggestions over about 100 000 history URLs on every keystroke;12- nothing may run on the main actor, and the code must be Swift 6 strict-concurrency clean on macOS and iOS;13- the project rule is **no third-party dependency** (supply chain, review surface, long-term maintenance).1415Options considered:1617| Option | Why not |18|---|---|19| SwiftData / Core Data | No FTS5 or custom ranking query, opaque schema and migrations, `ModelContext` is not `Sendable`, weak control over when a commit reaches the disk. |20| GRDB | An excellent library, but a third-party dependency (≈ 60 k lines to audit and track) for a layer whose needed subset is small. Rejected on purpose. |21| Files (JSON / property lists) | No atomic multi-entity update, no index, no search; a crash during a rewrite loses a session. |22| **System SQLite (`import SQLite3`)** | Chosen. |2324## Decision2526Use the SQLite that ships with the OS through its C API, wrapped by a thin layer written for this project27(`Sources/Persistence/SQLite`, about 650 lines):2829- `SQLiteConnection` — one handle, a prepared-statement cache, typed binding (`SQLiteConvertible`) and typed30  reading (`SQLiteRow`), `transaction { }` with savepoints for nesting, errors carrying SQLite's code, extended31  code and message (`SQLiteError`). Not `Sendable`: it never leaves its actor.32- `ConnectionActor` — an actor whose executor is a private serial `DispatchQueue`. Blocking SQLite calls run on33  that queue, not on the Swift cooperative pool and never on the main thread.34- `SQLiteDatabase` — one writer plus a pool of read-only connections (three by default) for file databases, in35  WAL mode, so readers and the writer do not block each other. An in-memory database has a single connection.36- `Migrator` + `Schema.migrations` — ordered, append-only migrations, each in its own transaction, version in37  `PRAGMA user_version`.38- Typed stores on top (`NavigatorDatabase` is the façade); no SQL leaks out of the module.3940Features used from the system SQLite: WAL, foreign keys, FTS5 (`unicode61` tokenizer with diacritic folding and41prefix indexes), JSON1 (`json_group_array`), recursive CTEs, `UPSERT`, `WITHOUT ROWID`, partial indexes. All of42them are present in the SQLite of macOS 15 and iOS 18, the minimum deployment targets.4344## Consequences4546Good:4748- No dependency; the whole storage layer is reviewable in this repository.49- Full control over durability (one committed WAL transaction per session save), over the schema, and over the50  queries that matter (frecency ranking is one indexed SQL statement).51- FTS5 and JSON1 for free; the database file can be inspected with the `sqlite3` command-line tool.5253Costs we accept:5455- We own the unsafe C boundary (pointer lifetimes, statement finalisation). It is confined to one file and56  covered by tests (round trips, rollback, cache eviction, concurrency, corruption).57- Row ⇄ model mapping is written by hand (`Stores/Records.swift`). Adding a model field means: a migration, one58  line in `init(row:)`, one in `save(_:)`.59- The SQLite version is the OS's. A feature newer than the minimum OS must not be used without a fallback.60- No encryption at rest at the SQLite level (SQLCipher would be a dependency). Protection is the platform's:61  iOS data protection and FileVault. See `docs/PERSISTENCE.md`, "Encryption at rest".6263## Revisit if6465- sync (Bridge) needs change feeds or CRDT storage that outgrow a hand-written layer;66- a requirement for per-database encryption appears (then: SQLCipher, or encrypting sensitive columns with a67  Keychain-held key).68
Files main
Menu

Theme