Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(backup): name the client tool when pg_restore cannot read a Postgres dump

Mauricebf8677a28c14
diff --git a/server/src/nest/backup/postgres-database-backup.ts b/server/src/nest/backup/postgres-database-backup.ts
index c6ac0e46c..73c5a8fd1 100644
--- a/server/src/nest/backup/postgres-database-backup.ts
+++ b/server/src/nest/backup/postgres-database-backup.ts
@@ -1,5 +1,6 @@
import type { DbSettings } from '../../app-config';
import { postgresTarget, type DatabaseEngine } from '../../db/engine';
+import { PgToolMissingError } from '../../db/engine/postgres/pg-process';
import { pgDump, pgDumpMigrations, pgRestore, pgRestoreTarget } from '../../db/engine/postgres/pg-tools';
import { unknownMigrations } from '../../db/known-migrations';
import { DataPathsService } from '../app-config/data-paths.service';
@@ -35,6 +36,23 @@ export async function checkPostgresDump(extractDir: string, binDir: string | und
return checkPostgresDumpFile(dump, binDir);
}
+/** pg_restore's words for an archive a newer pg_dump wrote. */
+const NEWER_ARCHIVE_FORMAT = /unsupported version \([\d.]+\) in file header/;
+
+/**
+ * The reason when pg_restore, not the file, kept the dump from being read: the
+ * tool is missing, or older than the pg_dump that wrote the archive. Null for
+ * anything else.
+ */
+function toolCannotRead(err: unknown): string | null {
+ if (err instanceof PgToolMissingError) return err.message;
+ if (!NEWER_ARCHIVE_FORMAT.test(describeError(err))) return null;
+ return (
+ 'This backup was written by a newer pg_dump than the pg_restore TREK runs can read. ' +
+ 'Point TREK_DB_PG_BIN_DIR at newer client tools (the Docker image has the newest supported ones).'
+ );
+}
+
/** Why the dump at `dump` cannot be restored into a Postgres database, or null: the checks of {@link checkPostgresDump}. */
async function checkPostgresDumpFile(dump: string, binDir: string | undefined): Promise<string | null> {
let executed: string[];
@@ -42,7 +60,7 @@ async function checkPostgresDumpFile(dump: string, binDir: string | undefined):
executed = await pgDumpMigrations(dump, binDir);
} catch (err) {
logWarn(`Restore: the archive's database could not be read (${describeError(err)})`);
- return 'Uploaded file is not a valid Postgres dump';
+ return toolCannotRead(err) ?? 'Uploaded file is not a valid Postgres dump';
}
if (executed.length === 0) {
return 'The uploaded dump holds no TREK migration history. This does not appear to be a TREK backup.';
diff --git a/server/tests/unit/nest/postgres-database-backup.test.ts b/server/tests/unit/nest/postgres-database-backup.test.ts
index ba8fbcb7a..41e44d380 100644
--- a/server/tests/unit/nest/postgres-database-backup.test.ts
+++ b/server/tests/unit/nest/postgres-database-backup.test.ts
@@ -203,4 +203,16 @@ describe('PostgresDatabaseBackup', () => {
installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations(['Migration20990101000000_from_the_future'])] });
await expect(backup.checkCopy(copy)).resolves.toMatch(/made by a newer TREK version/);
});
+
+ it('PGBACKUP-008: a dump pg_restore cannot read for its own reasons names the tool, not the file', async () => {
+ const copy = path.join(root, 'travel.pgdump');
+ writeFileSync(copy, 'dump');
+ vi.spyOn(console, 'warn').mockImplementation(() => undefined);
+ await expect(backup.checkCopy(copy)).resolves.toMatch(/^pg_restore was not found/);
+
+ installFakeTool(bin, 'pg_restore', {
+ snippets: [failsWith('pg_restore: error: unsupported version (1.16) in file header')],
+ });
+ await expect(backup.checkCopy(copy)).resolves.toMatch(/written by a newer pg_dump than the pg_restore/);
+ });
});