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.
This commit is contained in:
Isaias Tavares
2026-11-05 00:21:55 -03:00
parent 1c73ac4c35
commit 0e93df378f
3 changed files with 13 additions and 9 deletions
+9
View File
@@ -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';
+2 -6
View File
@@ -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 !== '');
@@ -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<string[]> {
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);
}