Add SQLite support alongside PostgreSQL for flexible deployment#9
Merged
Conversation
Let users run the service without deploying PostgreSQL to cut deployment cost. The target database is selected from DATABASE_URL at deploy time and is fixed at runtime (no switching) to avoid data being split across stores: postgresql:// / postgres:// -> PostgreSQL (unchanged default) sqlite: / file: -> SQLite (embedded, bun:sqlite) - config: getDbDriver()/isSqliteUrl()/getSqliteFilePath() infer the driver from the connection string (defaults to postgres). - schema: split into schema.pg.ts (pg-core) and schema.sqlite.ts (sqlite-core) with identical columns/defaults/indexes; schema.ts exports the active driver's tables at runtime so the store layer is unchanged. - client: SQLite uses the built-in bun:sqlite + drizzle-orm/bun-sqlite (WAL, busy_timeout, auto-created parent dir), one shared connection. - migrate: per-driver migration folder (drizzle/ vs drizzle/sqlite/) and migrator; SQLite skips the pg advisory lock and readiness probe. Adds the generated SQLite baseline migration; drizzle.config.ts is driver-aware. - console-store: API-key quota charge uses portable stepwise atomic writes on SQLite instead of the pg-only UPDATE...FROM RETURNING/FOR UPDATE CTE; the console-clear guard is bypassed for SQLite (local embedded file). - server: database reset enumerates tables via sqlite_master on SQLite. - docs/ops: docker-compose.sqlite.yml, .env.example, READMEs, db/README; ignore local *.db/*.sqlite files. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U5dS4D3toAwqRG63xF97p1
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
This PR adds SQLite as an alternative database backend, allowing the application to run without requiring a separate PostgreSQL instance. The target database is automatically selected at startup based on the
DATABASE_URLscheme and is fixed for the lifetime of the deployment to prevent data fragmentation.Key Changes
Dual-dialect schema structure: Split database schema into
schema.pg.ts(PostgreSQL) andschema.sqlite.ts(SQLite) with identical column names, defaults, and indexes. The mainschema.tsexports the appropriate dialect at runtime based onDATABASE_URL.Database driver detection: Added
getDbDriver(),isSqliteUrl(), andgetSqliteFilePath()utilities insrc/db/config.tsto determine the target database from the connection string (sqlite:/file:schemes → SQLite;postgresql://→ PostgreSQL).SQLite client initialization: Updated
src/db/client.tsto create and manage a shared bun:sqlite connection with WAL mode and busy timeout for concurrent access, mirroring the PostgreSQL connection pooling pattern.Migration support for both dialects: Extended
src/db/migrate.tsto run SQLite migrations fromdrizzle/sqlite/folder alongside PostgreSQL migrations fromdrizzle/. Added initial SQLite migration (drizzle/sqlite/0000_many_the_hand.sql) and metadata snapshot.Drizzle configuration: Updated
drizzle.config.tsto select schema and output folder based on the detected driver, enablingbun run db:generateto work with either database.Docker Compose for SQLite: Added
docker-compose.sqlite.ymlfor zero-dependency SQLite deployments using a named volume for persistence.Documentation and examples: Updated README files (English and Chinese) and
.env.exampleto document SQLite as a deployment option. Addedsrc/db/README.mdexplaining the dual-dialect architecture and migration workflow.Gitignore updates: Added patterns for SQLite database files (
.db,.db-shm,.db-wal,.sqlite,.sqlite3).Implementation Details
bigint({mode:'number'})→ SQLiteinteger({mode:'number'})for 64-bit integers;serial→integer(primaryKey autoIncrement).console-store.ts, etc.) remains unchanged; it imports from the unifiedschema.tswhich provides the correct dialect's tables.https://claude.ai/code/session_01U5dS4D3toAwqRG63xF97p1