Skip to content

Commit 304defa

Browse files
authored
DbProxy - Clarify internal table filtering in schema export (#2856)
refactor(db-proxy): clarify internal table filtering in schema export Replace opaque SQL LIKE patterns with named TypeScript predicates that explicitly document which tables are excluded (sqlite_*, _cf_* DO KV backing tables, __drizzle_migrations).
1 parent 5ce5b66 commit 304defa

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

services/db-proxy/src/app-db-do.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,28 +136,35 @@ export class AppDbDO extends DurableObject<Env> {
136136
});
137137
}
138138

139+
/**
140+
* Internal tables excluded from schema exports:
141+
* - `sqlite_*` : SQLite-managed tables (sqlite_sequence, etc.)
142+
* - `_cf_*` : Cloudflare DO KV-API backing tables (e.g. `_cf_KV`)
143+
* - `__drizzle_migrations` : drizzle migration bookkeeping
144+
*/
145+
private static isInternalTableName(name: string): boolean {
146+
return name.startsWith('sqlite_') || name.startsWith('_cf_') || name === '__drizzle_migrations';
147+
}
148+
149+
private static isInternalIndexName(name: string): boolean {
150+
return name.startsWith('sqlite_');
151+
}
152+
139153
/**
140154
* Get database schema (tables and indexes, excluding internal tables)
141155
*/
142156
getSchema(): SchemaResponse {
143-
// Get all tables excluding internal ones
144157
const tables = this.sql<TableInfo>`
145158
SELECT name, type, sql FROM sqlite_master
146-
WHERE type = 'table'
147-
AND name NOT LIKE '__\_%' ESCAPE '\\'
148-
AND name NOT LIKE 'sqlite_%'
149-
AND sql IS NOT NULL
159+
WHERE type = 'table' AND sql IS NOT NULL
150160
ORDER BY name
151-
`;
161+
`.filter(t => !AppDbDO.isInternalTableName(t.name));
152162

153-
// Get all indexes excluding internal ones
154163
const indexes = this.sql<IndexInfo>`
155164
SELECT name, sql FROM sqlite_master
156-
WHERE type = 'index'
157-
AND name NOT LIKE 'sqlite_%'
158-
AND sql IS NOT NULL
165+
WHERE type = 'index' AND sql IS NOT NULL
159166
ORDER BY name
160-
`;
167+
`.filter(i => !AppDbDO.isInternalIndexName(i.name));
161168

162169
return {
163170
tables: tables.map(t => ({ name: t.name, sql: t.sql })),

0 commit comments

Comments
 (0)