Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: retain truncated extension SQL for replay validation (#9887)

Adam Eivy59e74df64ea0
diff --git a/server/services/backupDatabaseDump.js b/server/services/backupDatabaseDump.js
index 349c7d5562..b5c664fe21 100644
--- a/server/services/backupDatabaseDump.js
+++ b/server/services/backupDatabaseDump.js
@@ -62,6 +62,7 @@ export function inspectDatabaseDump(path, { spoolTo } = {}) {
let dollarQuote = null;
let blockDepth = 0;
let statement = '';
+ let statementTruncated = false;
let unscannable = false;
// Keep only the statement prefix: COPY rows and SQL values may be huge.
// Count completed top-level statements, never SQL-looking stored content.
@@ -98,13 +99,14 @@ export function inspectDatabaseDump(path, { spoolTo } = {}) {
if (c === '-' && next === '-') break;
if (c === '/' && next === '*') { blockDepth = 1; i += 1; continue; }
if (c === ';') {
- if (spoolTo && (EXTENSION_COMMENT.test(statement) || EXTENSION_DROP.test(statement))) {
+ if (spoolTo && !statementTruncated && (EXTENSION_COMMENT.test(statement) || EXTENSION_DROP.test(statement))) {
extensionMetadata.push({ start: statementStart, end: lineStart + i + 1 });
}
const match = statement.match(CREATE_TABLE_STATEMENT);
if (match) { tableCount += 1; tables.add(match[1]); }
copyData = /^COPY\s/.test(statement) && / FROM stdin$/.test(statement);
statement = '';
+ statementTruncated = false;
statementStart = null;
if (copyData) break;
continue;
@@ -122,13 +124,17 @@ export function inspectDatabaseDump(path, { spoolTo } = {}) {
if (c === '"') {
const identifier = line.slice(i).match(/^"(?:[^"]|"")*"/);
if (identifier) {
- if (statement.length < MAX_SCANNED_LINE) statement += identifier[0];
+ if (statement.length + identifier[0].length <= MAX_SCANNED_LINE) statement += identifier[0];
+ else statementTruncated = true;
i += identifier[0].length - 1;
quote = null;
continue;
}
}
- if (statement.length < MAX_SCANNED_LINE && (statement || !/\s/.test(c))) statement += c;
+ if (statement || !/\s/.test(c)) {
+ if (statement.length < MAX_SCANNED_LINE) statement += c;
+ else statementTruncated = true;
+ }
}
};
const scan = (text) => {
diff --git a/server/services/backupDatabaseDump.test.js b/server/services/backupDatabaseDump.test.js
index 567bca6c9f..9c668d050a 100644
--- a/server/services/backupDatabaseDump.test.js
+++ b/server/services/backupDatabaseDump.test.js
@@ -91,6 +91,17 @@ describe('inspectDatabaseDump', () => {
if (process.platform !== 'win32') expect(statSync(replayPath).mode & 0o777).toBe(0o600);
});
+ it('retains an oversized statement prefix so invalid SQL still reaches transactional replay', async () => {
+ const sql = "CREATE TABLE public.memories (\n);\nCREATE TABLE public.memory_links (\n);\n"
+ + `COMMENT ON EXTENSION vector IS NULL${' \n'.repeat(CHUNK)}INVALID SQL;\n` + TRAILER;
+ const spoolTo = join(dir, 'invalid-comment-spool.sql');
+ const dump = await inspectDatabaseDump(write('invalid-comment.sql', sql), { spoolTo });
+ expect(dump.complete).toBe(true);
+ expect(dump.extensionMetadata).toEqual([]);
+ const replayPath = await prepareDatabaseReplay(spoolTo, dump.extensionMetadata);
+ expect(readFileSync(replayPath, 'utf8')).toBe(sql);
+ });
+
it('rejects a read failure rather than reporting an empty dump', async () => {
await expect(inspectDatabaseDump(join(dir, 'missing.sql'))).rejects.toMatchObject({ code: 'ENOENT' });
});