fix(backup): restore Postgres backups into PostgreSQL 14, 15 and 16

pg_restore 17 and later write SET transaction_timeout at the top of every script, which older servers refuse under ON_ERROR_STOP. The restore now leaves settings the target server does not know out of the script while it streams.
This commit is contained in:
Maurice
2026-11-23 14:56:55 +01:00
parent 626e19c534
commit bb2b4a1f5a
6 changed files with 192 additions and 8 deletions
@@ -378,6 +378,32 @@ describe('pg-tools', () => {
expect(readdirSync(out)).toEqual([]);
});
it.each([
{ id: 'PGTOOLS-021a', versionNum: '160004', kept: false },
{ id: 'PGTOOLS-021b', versionNum: '170002', kept: true },
])(
'$id: the script psql runs sets transaction_timeout only on a server that knows it',
async ({ versionNum, kept }) => {
const scriptCopy = path.join(bin, 'restore.sql');
installFakeTool(bin, 'pg_restore', {
snippets: [
writesFile(
'\\restrict k1\nSET lock_timeout = 0;\nSET transaction_timeout = 0;\n' +
'CREATE TABLE public.mikro_orm_migrations (\n);\n',
),
],
});
installFakeTool(bin, 'psql', {
snippets: [answersSchema('public', 'public', { versionNum }), copiesFileEndingWith('restore.sql', scriptCopy)],
});
await pgRestore(URL_WITH_SECRET, path.join(out, 'in.pgdump'), bin, LIMIT);
const script = readFileSync(scriptCopy, 'utf8');
expect(script).toContain('\nCREATE TABLE public.mikro_orm_migrations (\n');
expect(script).toContain('SET lock_timeout = 0;\n');
expect(script.includes('SET transaction_timeout = 0;')).toBe(kept);
},
);
it("PGTOOLS-020: a plugin's own table named mikro_orm_migrations never passes for TREK's history", async () => {
const forged =
'COPY trek_plugin_notes.mikro_orm_migrations (id, name) FROM stdin;\n1\tMigration29990101000000_forged\n\\.\n';
@@ -0,0 +1,81 @@
/**
* The settings a newer pg_restore writes at the start of a restore script
* (src/db/engine/postgres/script-settings.ts): a server that does not know one
* refuses the whole script under ON_ERROR_STOP, so the restore leaves it out
* for that server and nowhere else.
*/
import { withoutUnknownSettings } from '../../../../../src/db/engine/postgres/script-settings';
import { Readable } from 'node:stream';
import { describe, expect, it } from 'vitest';
/** The settings block pg_restore 18 writes, followed by the script's first objects and some COPY data. */
const SCRIPT = [
'--',
'-- PostgreSQL database dump',
'--',
'',
'\\restrict k1',
'',
'-- Dumped from database version 16.4',
'-- Dumped by pg_dump version 18.1',
'',
'SET statement_timeout = 0;',
'SET lock_timeout = 0;',
'SET idle_in_transaction_session_timeout = 0;',
'SET transaction_timeout = 0;',
"SET client_encoding = 'UTF8';",
'SET standard_conforming_strings = on;',
"SELECT pg_catalog.set_config('search_path', '', false);",
'SET row_security = off;',
'',
'CREATE TABLE public.mikro_orm_migrations (',
' id integer NOT NULL',
');',
'COPY public.notes (body) FROM stdin;',
'SET transaction_timeout = 0;',
'Grüße aus Köln',
'\\.',
'',
].join('\n');
/** What the filter makes of `text` for a server of major `major`, fed in chunks of `size` bytes. */
async function filtered(text: string, major: number, size: number): Promise<string> {
const bytes = Buffer.from(text, 'utf8');
const chunks: Buffer[] = [];
for (let at = 0; at < bytes.length; at += size) chunks.push(bytes.subarray(at, at + size));
const out: Buffer[] = [];
for await (const chunk of Readable.from(chunks).pipe(withoutUnknownSettings(major))) out.push(chunk as Buffer);
return Buffer.concat(out).toString('utf8');
}
describe('script-settings', () => {
it.each([14, 15, 16])(
'SCRIPTSET-001: a PostgreSQL %i server gets the script without SET transaction_timeout, and nothing else changes',
async (major) => {
const expected = SCRIPT.replace('SET transaction_timeout = 0;\n', '');
for (const size of [1, 3, 7, 64, 1 << 20]) {
expect(await filtered(SCRIPT, major, size), `chunks of ${size}`).toBe(expected);
}
},
);
it.each([17, 18])('SCRIPTSET-002: a PostgreSQL %i server gets the script as pg_restore wrote it', async (major) => {
expect(await filtered(SCRIPT, major, 5)).toBe(SCRIPT);
});
it('SCRIPTSET-003: only the settings block is read, so a COPY row that looks like the setting stays', async () => {
const out = await filtered(SCRIPT, 16, 1 << 20);
expect(out.match(/^SET transaction_timeout = 0;$/gm)).toHaveLength(1);
expect(out.indexOf('SET transaction_timeout')).toBeGreaterThan(out.indexOf('COPY public.notes'));
});
it('SCRIPTSET-004: a script that ends inside its settings block keeps its last line unless it is the setting', async () => {
expect(await filtered('SET lock_timeout = 0;\nSET transaction_timeout = 0;', 16, 4)).toBe(
'SET lock_timeout = 0;\n',
);
expect(await filtered('SET lock_timeout = 0;\nSET row_security = off;', 16, 4)).toBe(
'SET lock_timeout = 0;\nSET row_security = off;',
);
});
});