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: explain database restore refusals and uncertain results (#9753)

Adam Eivyd300f13f75e5
diff --git a/client/src/components/settings/BackupTab.jsx b/client/src/components/settings/BackupTab.jsx
index 85a29ef0fe..bb4c9025b6 100644
--- a/client/src/components/settings/BackupTab.jsx
+++ b/client/src/components/settings/BackupTab.jsx
@@ -31,12 +31,28 @@ const asArray = (v) => Array.isArray(v) ? v : [];
// Backup form. Drop those on load instead: the spelling of the survivors is left
// untouched (anchoring is a read-time concern), so the form is not dirty.
const asExcludeArray = (v) => asArray(v).filter(isSafeExcludePattern);
-// restorePostgres refusals that left the database untouched (preview or execution).
-const DB_RESTORE_REFUSALS = {
+// User-facing restore failures shared by preview and confirmation. Only proven
+// refusals promise unchanged data; transport and unknown outcomes stay uncertain.
+const DB_RESTORE_FAILURE_MESSAGES = {
manifest_mismatch: 'Snapshot dump failed integrity verification',
manifest_unreadable: 'Snapshot verification metadata could not be read. Choose another snapshot or repair the backup media before retrying.',
dump_unreadable: 'The snapshot database dump could not be read or staged for restore. Nothing was changed; check the backup media and free temp space, then retry.',
dump_incomplete: 'The snapshot database dump is incomplete (truncated or damaged). Nothing was changed; choose another snapshot.',
+ restore_preflight: 'Database restore was refused because the database contains unexpected objects, ownership, or dependencies. Nothing was changed. Check the server logs and resolve the reported database conflict before trying again.',
+ restore_journal: 'The restore recovery journal could not be written. Nothing was changed. Check the server logs and available disk space before trying again.',
+ no_dump: 'No DB dump in this snapshot',
+ not_configured: 'The database is unavailable for restore. Check Database status and the server logs for connection details.',
+ restore_error: 'Database restore failed. Check Backup status, any recovery warning, and the server logs for details.',
+ timeout: 'Database restore timed out. Check Backup status, any recovery warning, and the server logs before taking further action.',
+ request_error: 'Could not confirm the database restore result. Check Backup status and any recovery warning before starting another restore.',
+};
+const dbRestoreFailureMessage = (result) => {
+ const message = DB_RESTORE_FAILURE_MESSAGES[result?.reason]
+ || 'Database restore could not be confirmed. Check Backup status and the server logs for details.';
+ // Producer explanations can name the specific missing table or database
+ // conflict. Keep them in the owned toast alongside the safe next step.
+ const details = typeof result?.error === 'string' ? result.error.trim() : '';
+ return details ? `${message} Details: ${details}` : message;
};
const snapshotIdentity = (snapshot) =>
snapshot.selectionKey || `${snapshot.source || 'current'}/${snapshot.id}`;
@@ -311,6 +327,16 @@ export function BackupTab() {
);
};
+ const reportRestoreFailure = (result) => {
+ if (result?.recovery) {
+ // Committed (or not yet provably rolled back): resume, never replay.
+ setRestoreRecovery({ pending: true, ...result.recovery });
+ toast.error(result.error || 'The database restore needs recovery before the database can be used.', { duration: Infinity });
+ return;
+ }
+ toast.error(dbRestoreFailureMessage(result));
+ };
+
const handleRestoreDb = async (snapshot) => {
const generation = restorePreviewGenerationRef.current + 1;
restorePreviewGenerationRef.current = generation;
@@ -322,14 +348,10 @@ export function BackupTab() {
};
// Dry-run first to show what would restore, then open the confirm modal.
const preview = await restoreDatabase({ ...request, dryRun: true }, { silent: true })
- .catch(() => null);
+ .catch(err => ({ status: 'failed', reason: 'request_error', error: err?.message }));
if (restorePreviewGenerationRef.current !== generation) return;
- if (!preview || preview.status === 'skipped') {
- toast.error(preview?.reason === 'no_dump' ? 'No DB dump in this snapshot' : 'DB restore unavailable');
- return;
- }
- if (preview.status !== 'ok') {
- toast.error(DB_RESTORE_REFUSALS[preview.reason] || `DB restore unavailable: ${preview.reason || 'unknown'}`);
+ if (preview?.status !== 'ok') {
+ reportRestoreFailure(preview);
return;
}
setRestorePreview(preview);
@@ -340,18 +362,11 @@ export function BackupTab() {
const target = restoreTarget;
setRestoreTarget(null);
const result = await restoreDatabase({ ...target.request, dryRun: false }, { silent: true })
- .catch(() => ({ status: 'failed', reason: 'request_error' }));
- if (result.status === 'ok') {
+ .catch(err => ({ status: 'failed', reason: 'request_error', error: err?.message }));
+ if (result?.status === 'ok') {
toast.success(`Database restored from ${target.request.snapshotId}`, { icon: '💾' });
- } else if (DB_RESTORE_REFUSALS[result.reason]) {
- toast.error(DB_RESTORE_REFUSALS[result.reason]);
- } else if (result.recovery) {
- // Committed (or not yet provably rolled back): the server keeps the
- // database fenced. Resume — never repeat — the restore.
- setRestoreRecovery({ pending: true, ...result.recovery });
- toast.error(result.error || 'The database restore needs recovery before the database can be used.', { duration: Infinity });
} else {
- toast.error(`DB restore failed: ${result.reason || 'unknown'}`);
+ reportRestoreFailure(result);
}
setRestorePreview(null);
};
diff --git a/client/src/components/settings/BackupTab.test.jsx b/client/src/components/settings/BackupTab.test.jsx
index 3091b1ce44..c5e27a2897 100644
--- a/client/src/components/settings/BackupTab.test.jsx
+++ b/client/src/components/settings/BackupTab.test.jsx
@@ -377,22 +377,96 @@ describe('BackupTab', () => {
}, { silent: true });
});
- it('toasts an error when the confirmed restore fails', async () => {
+ it.each(['preview', 'confirmation'])('explains a preflight refusal at %s without admitting another restore', async (phase) => {
withSnapshot();
- restoreDatabase
- .mockResolvedValueOnce({ status: 'ok', sizeBytes: 2048, tableCount: 12 }) // dry-run
- .mockResolvedValueOnce({ status: 'failed', reason: 'pg_restore_error' }); // real restore fails
+ const refusal = {
+ status: 'failed',
+ reason: 'restore_preflight',
+ error: 'Database contains unexpected objects, ownership, or dependencies. Restore was refused without changing data.',
+ };
+ if (phase === 'confirmation') {
+ restoreDatabase.mockResolvedValueOnce({ status: 'ok', sizeBytes: 2048, tableCount: 12 });
+ }
+ restoreDatabase.mockResolvedValueOnce(refusal);
await renderTab();
await act(async () => {
fireEvent.click(await screen.findByRole('button', { name: /Restore DB/i }));
});
- await act(async () => {
- fireEvent.click(screen.getByRole('button', { name: /^Restore$/i }));
- });
+ if (phase === 'confirmation') {
+ await act(async () => {
+ fireEvent.click(screen.getByRole('button', { name: /^Restore$/i }));
+ });
+ }
+
+ expect(toast.error).toHaveBeenCalledWith(
+ 'Database restore was refused because the database contains unexpected objects, ownership, or dependencies. Nothing was changed. Check the server logs and resolve the reported database conflict before trying again. Details: ' + refusal.error
+ );
+ expect(toast.success).not.toHaveBeenCalled();
+ expect(restoreDatabase).toHaveBeenCalledTimes(phase === 'preview' ? 1 : 2);
+ expect(screen.queryByText(/Restore database\?/i)).toBeNull();
+ });
+
+ it.each(['preview', 'confirmation'])('keeps a lost %s response uncertain and preserves the request explanation', async (phase) => {
+ withSnapshot();
+ if (phase === 'confirmation') {
+ restoreDatabase.mockResolvedValueOnce({ status: 'ok', sizeBytes: 2048, tableCount: 12 });
+ }
+ restoreDatabase.mockRejectedValueOnce(new Error('Connection closed before the response arrived.'));
+ await renderTab();
+ await act(async () => { fireEvent.click(await screen.findByRole('button', { name: /Restore DB/i })); });
+ if (phase === 'confirmation') {
+ await act(async () => { fireEvent.click(screen.getByRole('button', { name: /^Restore$/i })); });
+ }
+
+ expect(toast.error).toHaveBeenCalledWith(
+ 'Could not confirm the database restore result. Check Backup status and any recovery warning before starting another restore. Details: Connection closed before the response arrived.'
+ );
+ expect(toast.success).not.toHaveBeenCalled();
+ expect(restoreDatabase).toHaveBeenCalledTimes(phase === 'preview' ? 1 : 2);
+ expect(screen.queryByText(/Restore database\?/i)).toBeNull();
+ });
- expect(toast.error).toHaveBeenCalledWith('DB restore failed: pg_restore_error');
+ it.each([
+ ['preview', { status: 'failed', error: 'Additional restore details.' }],
+ ['confirmation', null],
+ ])('keeps an unknown %s result uncertain', async (phase, result) => {
+ withSnapshot();
+ if (phase === 'confirmation') {
+ restoreDatabase.mockResolvedValueOnce({ status: 'ok', sizeBytes: 2048, tableCount: 12 });
+ }
+ restoreDatabase.mockResolvedValueOnce(result);
+ await renderTab();
+ await act(async () => { fireEvent.click(await screen.findByRole('button', { name: /Restore DB/i })); });
+ if (phase === 'confirmation') {
+ await act(async () => { fireEvent.click(screen.getByRole('button', { name: /^Restore$/i })); });
+ }
+
+ expect(toast.error).toHaveBeenCalledWith(
+ 'Database restore could not be confirmed. Check Backup status and the server logs for details.'
+ + (result?.error ? ` Details: ${result.error}` : '')
+ );
expect(toast.success).not.toHaveBeenCalled();
+ expect(screen.queryByText(/Restore database\?/i)).toBeNull();
+ });
+
+ it('offers recovery when preview reports a pending uncertain restore', async () => {
+ withSnapshot();
+ const recovery = { id: '00000000-0000-4000-8000-000000000002', stage: 'replaying', snapshotId: 'snap-1' };
+ restoreDatabase.mockResolvedValueOnce({
+ status: 'failed', reason: 'restore_recovery_pending',
+ error: 'The previous restore outcome needs recovery.', recovery,
+ });
+ recoverDatabaseRestore.mockResolvedValueOnce({ status: 'ok', outcome: 'rolled_back' });
+ await renderTab();
+ await act(async () => { fireEvent.click(await screen.findByRole('button', { name: /Restore DB/i })); });
+
+ expect(toast.error).toHaveBeenCalledWith('The previous restore outcome needs recovery.', { duration: Infinity });
+ expect(screen.queryByText(/Restore database\?/i)).toBeNull();
+ await act(async () => { fireEvent.click(screen.getByRole('button', { name: /Resume recovery/i })); });
+ expect(recoverDatabaseRestore).toHaveBeenCalledWith(recovery.id, { silent: true });
+ expect(restoreDatabase).toHaveBeenCalledTimes(1);
+ expect(screen.queryByText('Database restore needs recovery')).toBeNull();
});
it('aborts and toasts when the dry-run reports no dump', async () => {
@@ -444,7 +518,10 @@ describe('BackupTab', () => {
// learns nothing changed instead of seeing an opaque reason code.
it('explains an incomplete dump and does not open confirmation', async () => {
withSnapshot();
- restoreDatabase.mockResolvedValue({ status: 'failed', reason: 'dump_incomplete' });
+ restoreDatabase.mockResolvedValue({
+ status: 'failed', reason: 'dump_incomplete',
+ error: 'The snapshot database dump is incomplete (missing table example_records). Restore was refused without changing data.',
+ });
await renderTab();
await act(async () => {
@@ -452,7 +529,7 @@ describe('BackupTab', () => {
});
expect(restoreDatabase).toHaveBeenCalledTimes(1);
- expect(toast.error).toHaveBeenCalledWith(expect.stringMatching(/incomplete.*Nothing was changed/));
+ expect(toast.error).toHaveBeenCalledWith(expect.stringMatching(/incomplete.*Nothing was changed.*missing table example_records/));
expect(screen.queryByText(/Restore database\?/i)).toBeNull();
});
});