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.
This commit is contained in:
Isaias Tavares
2026-11-05 00:13:04 -03:00
parent 293cdbc353
commit ae005fc582
2 changed files with 44 additions and 7 deletions
@@ -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;',