{{ message }}
Repository navigation
feat(core): add a database table prefix for shared SQLite databases - #53746
Open
Brendonovich wants to merge 3 commits into
Open
Brendonovich wants to merge 3 commits into
Brendonovich wants to merge 3 commits into
Conversation
This branch has not been deployed
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.

Issue for this PR
Closes #53577
Type of change
What does this PR do?
Adds a
database.prefixoption, so OpenCode can share a SQLite database with tables it doesn't own:With a prefix set, every table and index OpenCode creates, including the
migrationjournal, is stored as<prefix><name>. Bootstrap only looks at tables under the prefix, so the host's tables (cf_agents_*,alchemy_*, or its ownsession/kv) no longer tripDatabase is not empty and has no session table, and names can't collide. With no prefix, behavior is unchanged, so existing deployments keep their data.How it works:
PrefixedSQLiteDialectin our Drizzle fork (database/drizzle/sqlite-core/dialect.ts). Table definitions stay shared and unprefixed, because they're module-level and shared by every Durable Object in an isolate. The prefix is applied when a query is rendered. Drizzle renders nested SQL through the root statement'scollectSQL, so wrapping the root lets the dialect rename everyTablereference andtable.columnqualifier. That covers builders, subqueries, andsqltemplates that interpolate tables or columns. Joins are the one place Drizzle bakes a table name into a plain identifier while building the query, so the dialect passes join tables through as table references instead. Aliased from-tables aren't used, so they fail loudly when a prefix is set. The dialect is only used when a prefix is set.schema.gen.tsreferences the table declarations. It writesCREATE TABLE ${KVTable},REFERENCES ${ProjectTable}, and partial-index conditions likeWHERE ${SessionPendingTable.type} = ...as Drizzlesqltemplates, so the dialect prefixes DDL as well. Index names and the migration journal have no declaration, so they useprefixedIdentifier("..."), a chunk the prefixed dialect renders under the prefix and any other dialect renders likesql.identifier. The migration generator emits these interpolations from drizzle-kit's output and imports the declarations.prefixedIdentifier("kv")rather than declarations, because migrations are snapshots and must stay correct if a declaration is later renamed or removed.Migration.up(tx)keeps its signature. Existing migrations are untouched. They never run on a prefixed database, because a fresh prefixed database bootstraps from the current schema and records them all as applied.DatabaseMigration.apply(db, prefix)discovers tables under the prefix and uses<prefix>migrationas the journal.Database.Options.prefixis validated as a plain identifier not starting withsqlite_.Database.layerFromClientandconfiguredClienttake the prefix, andServerWorkerd.Options.database.prefixpasses it through.sessiontable there belongs to the host, not a v1 install.The dialect is the only place the prefix is applied. Hand-written future migrations must name tables and indexes through table declarations or
prefixedIdentifierrather than literal text. This is documented onMigration.up.How did you verify your code works?
packages/core/test/database-table-prefix.test.ts:sqltemplate is fully prefixed, while columns named like tables (worktree) stay unprefixed.session,kv, andcf_agents_statetables plus an index boots twice under a prefix (bootstrap, then the migration path) and does a Drizzle write/read. Every OpenCode table and index ends up under the prefix, and the foreign tables and their rows are untouched.packages/server/test/workerd.test.ts: the workerd profile boots withdatabase.prefixbeside an object-ownedsessiontable.v2. The server and SDK suites show no new failures.kvtable, and confirmed the result wasopencode_kvplus"opencode_kv_scope_idx" ... WHERE "opencode_kv"."scope" is not null, with the foreign table untouched. The probe was then reverted.bun script/migration.ts --checkpasses.bun run checkpasses.Screenshots / recordings
N/A
Checklist