From ae005fc58290d8688e9fcb48e37c1804fe241e01 Mon Sep 17 00:00:00 2001 From: Isaias Tavares Date: Thu, 5 Nov 2026 00:13:04 -0300 Subject: [PATCH] fix(db): check the restore script for TREK's tables line by line pgRestore read the whole pg_restore --file script into one string to find TREK's migration table before clearing the schema. The script carries every COPY row of the dump, so a large database made the read exceed V8's maximum string length before the check ran, and the restore failed with an unhelpful error instead of loading or refusing. The check now streams the script with readline and stops at the first matching line. The match itself is unchanged: the same CREATE TABLE line for the same quoted schema, so a dump without TREK's history is still refused before anything is cleared. The regex's escaping is a named helper. PGTOOLS-005b pins that the script is never read whole. --- server/src/db/engine/postgres/pg-tools.ts | 32 ++++++++++++++++--- .../unit/db/engine/postgres/pg-tools.test.ts | 19 +++++++++-- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/server/src/db/engine/postgres/pg-tools.ts b/server/src/db/engine/postgres/pg-tools.ts index b547c8075..9d130ac45 100644 --- a/server/src/db/engine/postgres/pg-tools.ts +++ b/server/src/db/engine/postgres/pg-tools.ts @@ -1,6 +1,7 @@ import { execFile } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; +import readline from 'node:readline'; import { promisify } from 'node:util'; const execFileAsync = promisify(execFile); @@ -266,10 +267,31 @@ async function refuseToolsWithoutRestrict(binDir: string | undefined): Promise { + const createsHistory = new RegExp(`^CREATE TABLE ${escapeRegExp(`${schema.quoted}.mikro_orm_migrations`)} \\(`); + const input = fs.createReadStream(script, { encoding: 'utf8' }); + const lines = readline.createInterface({ input, crlfDelay: Infinity }); + try { + for await (const line of lines) { + if (createsHistory.test(line)) return true; + } + return false; + } finally { + lines.close(); + input.destroy(); + } } /** @@ -295,7 +317,7 @@ export async function pgRestore(url: string, source: string, binDir: string | un ['--no-owner', '--no-privileges', `--schema=${schema.name}`, `--file=${script}`, '--', source], {}, ); - if (!scriptHoldsTrekSchema(script, schema)) { + if (!(await scriptHoldsTrekSchema(script, schema))) { throw new Error(`The backup holds no TREK tables in the schema "${schema.name}"; nothing was replaced.`); } await run( diff --git a/server/tests/unit/db/engine/postgres/pg-tools.test.ts b/server/tests/unit/db/engine/postgres/pg-tools.test.ts index 72783ec65..568d54085 100644 --- a/server/tests/unit/db/engine/postgres/pg-tools.test.ts +++ b/server/tests/unit/db/engine/postgres/pg-tools.test.ts @@ -16,7 +16,7 @@ import { resolvePgTool, } from '../../../../../src/db/engine/postgres/pg-tools'; -import { +import fs, { chmodSync, existsSync, mkdtempSync, @@ -28,7 +28,7 @@ import { } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; const URL_WITH_SECRET = 'postgresql://trek:s3cr%40t@db.example:6543/trek_db?sslmode=require'; @@ -176,6 +176,21 @@ describe('pg-tools', () => { expect(readdirSync(out)).toEqual([]); }); + it('PGTOOLS-005b: the restore script is checked line by line, so its COPY data is never read whole', async () => { + const source = path.join(out, 'in.pgdump'); + fakeTool('pg_restore', { + body: `printf '%s\\n' '\\restrict k1' 'CREATE TABLE public.mikro_orm_migrations (' > "\${4#--file=}"; seq 1 200000 >> "\${4#--file=}"`, + }); + fakeTool('psql', { stdout: PUBLIC_SCHEMA }); + const readWhole = vi.spyOn(fs, 'readFileSync'); + try { + await pgRestore(URL_WITH_SECRET, source, bin); + expect(readWhole.mock.calls.filter(([file]) => String(file).endsWith('restore.sql'))).toEqual([]); + } finally { + readWhole.mockRestore(); + } + }); + it("PGTOOLS-006: reads the dump's recorded migrations from its COPY block, and none from a dump without them", async () => { const copy = [ 'SET statement_timeout = 0;',