--- name: database-libsql version: 2 summary: SQLite through the libsql client and Drizzle, migrated at startup, with no native build step. requires: [start-app] conflicts: [database-sqlite, database-postgres] dependencies: ['@libsql/client', drizzle-orm] devDependencies: [drizzle-kit] wires: [ package.json, .gitignore, .oxfmtrc.json, src/server/app.ts, src/server/app.test.ts, src/routes/api/health.ts, Dockerfile, src/server/session.ts, src/routes/api/auth.$.ts, ] starters: [src/db/schema.ts, drizzle/] --- # database-libsql ## Use it when The application wants a local SQLite file but `database-sqlite` does not fit: the image is built for another architecture with `pnpm install --ignore-scripts` (libsql ships prebuilt platform packages and runs no install script), or the database may later move to a remote libsql server such as Turso by changing the URL. Praetorium uses it for these reasons. Otherwise prefer `database-sqlite`: its synchronous driver is simpler and it has the backup and restore script. Use `database-postgres` for several replicas or a managed database. ## Decisions - **The client is a pool, so settings go where every connection gets them.** `@libsql/client` opens up to 20 connections to a local file, each with libsql's defaults, so a pragma sent through `execute` reaches only one. The busy timeout goes through the client's `timeout` option, which every connection receives; WAL is set once and persists in the file; synchronous FULL and foreign keys on are libsql's defaults. `src/server/database.test.ts` reads all four from the startup client so a libsql default changing fails a test. - **`app().database` is a promise.** Setting WAL and migrating are asynchronous and `createApp` stays synchronous, so server code awaits it: `const database = await app().database`. A failed migration rejects that promise, the health route reports it, and the deploy never turns healthy. `createApp` attaches a handler so the rejection does not crash the process before anything awaits it, and closes the client only if it opened. - **Migrations run at startup with foreign keys off, then are checked.** libsql's `migrate` turns foreign keys off for the migration batch, so a drizzle-kit table rebuild cannot cascade-delete referencing rows (the test proves this stays true). Violations the migration leaves behind are found by `PRAGMA foreign_key_check` after it, and stop startup on every start until repaired. - **A backup before every schema change.** When migrations are pending and the database has tables, `VACUUM INTO` writes `$DATA_DIR/backups/pre-migration-