From 0e93df378f4a6cc2966b7dc8fc196c3feb780a0d Mon Sep 17 00:00:00 2001 From: Isaias Tavares Date: Thu, 5 Nov 2026 00:21:55 -0300 Subject: [PATCH] refactor(db): define the "TREK's tables" catalog query once The bootstrap's currentTables and the client tools' pgTableNames each wrote the same information_schema query for the current schema, so a change to which tables count as TREK's would have had to land twice. It is now CURRENT_SCHEMA_TABLES_SQL in dialect/catalog.ts, beside the other catalog statements, and both read it. pgTableNames now lists its names in name order too, as the bootstrap already did. Nothing reads that order for meaning; the boot-restore refusal names the first few tables, and that list was already sorted. --- server/src/db/dialect/catalog.ts | 9 +++++++++ server/src/db/engine/postgres/pg-tools.ts | 8 ++------ server/src/db/engine/postgres/schema-bootstrap.ts | 5 ++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/server/src/db/dialect/catalog.ts b/server/src/db/dialect/catalog.ts index affa8535a..79964b936 100644 --- a/server/src/db/dialect/catalog.ts +++ b/server/src/db/dialect/catalog.ts @@ -26,3 +26,12 @@ export function tableExistsSql(platform: Platform): string { } return unsupported(platform); } + +/** + * The tables in the current schema, one `table_name` per row in name order: + * the schema TREK's own tables live in. Postgres only, and the one definition + * of that question: the bootstrap reads it through the connection, the client + * tools' table listing hands it to psql. + */ +export const CURRENT_SCHEMA_TABLES_SQL = + 'select table_name from information_schema.tables where table_schema = current_schema() order by table_name'; diff --git a/server/src/db/engine/postgres/pg-tools.ts b/server/src/db/engine/postgres/pg-tools.ts index 3a7c4ae48..d095e1bf6 100644 --- a/server/src/db/engine/postgres/pg-tools.ts +++ b/server/src/db/engine/postgres/pg-tools.ts @@ -1,3 +1,4 @@ +import { CURRENT_SCHEMA_TABLES_SQL } from '../../dialect/catalog'; import { CLEAR_SCHEMA_SQL } from './clear-schema'; import { pgConnectionEnv, run } from './pg-process'; import { refuseToolsWithoutRestrict } from './restore-safety'; @@ -157,12 +158,7 @@ export async function pgTableNames(url: string, binDir: string | undefined): Pro const out = await run( 'psql', binDir, - [ - '--no-psqlrc', - '--no-align', - '--tuples-only', - '--command=select table_name from information_schema.tables where table_schema = current_schema()', - ], + ['--no-psqlrc', '--no-align', '--tuples-only', `--command=${CURRENT_SCHEMA_TABLES_SQL}`], pgConnectionEnv(url), ); return out.split('\n').filter((line) => line !== ''); diff --git a/server/src/db/engine/postgres/schema-bootstrap.ts b/server/src/db/engine/postgres/schema-bootstrap.ts index 6c43b30e5..4a8b655f2 100644 --- a/server/src/db/engine/postgres/schema-bootstrap.ts +++ b/server/src/db/engine/postgres/schema-bootstrap.ts @@ -1,5 +1,6 @@ import { readEnv, type AppEnv } from '../../../app-config'; import { resolveDataPaths } from '../../../app-config/data-paths'; +import { CURRENT_SCHEMA_TABLES_SQL } from '../../dialect/catalog'; import { knownMigrationNames } from '../../known-migrations'; import { migrationLabel, REFUSAL, refuseNewerDatabase, type MigrationSafetyNet } from '../../legacy-baseline'; import type { AnyOrm } from '../../orm'; @@ -32,9 +33,7 @@ export interface PostgresBootstrapDeps { * refused database is left exactly as it was. */ async function currentTables(connection: Connection): Promise { - const rows: Array<{ table_name: string }> = await connection.execute( - 'select table_name from information_schema.tables where table_schema = current_schema() order by table_name', - ); + const rows: Array<{ table_name: string }> = await connection.execute(CURRENT_SCHEMA_TABLES_SQL); return rows.map((row) => row.table_name); }