@hivemagnus/magnus-server 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "1.1.3"
2
+ "version": "1.1.4"
3
3
  }
@@ -239,17 +239,40 @@ export async function restoreDatabase(options) {
239
239
  });
240
240
  if (listed.code !== 0)
241
241
  throw new Error(`${options.file} is not a readable dump: ${(listed.stderr || listed.stdout).trim().split('\n').slice(-2).join(' ')}`);
242
+ // A schema left aside by an earlier restore means that restore was interrupted (or could not drop
243
+ // it): the original data may only exist there, so never swap again on top of it.
244
+ const leftover = await options.client.query("SELECT nspname FROM pg_namespace WHERE nspname LIKE 'magnus\\_pre\\_restore\\_%' ORDER BY nspname");
245
+ if (leftover.rows.length) {
246
+ const names = leftover.rows.map(row => row.nspname).join(', ');
247
+ throw new Error(`an earlier restore left the schema ${names} behind, which may hold the original data. Check what \`public\` holds; `
248
+ + `to put the set-aside schema back run \`${restoreRevertSql(leftover.rows.at(-1).nspname)}\`, or drop it once you are sure it is not needed, then restore again.`);
249
+ }
242
250
  const aside = restoreAsideSchema(options.now ?? new Date());
243
251
  await options.client.query(restoreSwapSql(aside));
244
252
  log(` moved the current schema aside as ${aside}`);
253
+ // From here on every failure must put the original schema back before it is reported.
254
+ const putBack = async (reason) => {
255
+ try {
256
+ await options.client.query(restoreRevertSql(aside));
257
+ }
258
+ catch (error) {
259
+ throw new Error(`${reason}, and putting the original schema back failed too; it is kept as ${aside}: ${error instanceof Error ? error.message : String(error)}`);
260
+ }
261
+ throw new Error(`${reason}; the database is unchanged`);
262
+ };
245
263
  const { url, env } = splitPassword(options.databaseUrl);
246
- const result = await run(pgRestore, ['--single-transaction', '--exit-on-error', '--no-owner', '--dbname', url, options.file], { capture: true, env: { ...process.env, ...env } });
247
- if (result.code !== 0) {
248
- await options.client.query(restoreRevertSql(aside)).catch((error) => {
249
- throw new Error(`pg_restore failed and putting the original schema back failed too; it is kept as ${aside}: ${error instanceof Error ? error.message : String(error)}`);
250
- });
251
- throw new Error(`pg_restore failed (exit ${result.code}); the database is unchanged: ${result.stderr.trim().split('\n').slice(-3).join(' ')}`);
264
+ let result;
265
+ try {
266
+ result = await run(pgRestore, ['--single-transaction', '--exit-on-error', '--no-owner', '--dbname', url, options.file], { capture: true, env: { ...process.env, ...env } });
252
267
  }
253
- await options.client.query(restoreFinishSql(aside));
268
+ catch (error) {
269
+ return putBack(`${pgRestore} could not be run (${error instanceof Error ? error.message : String(error)})`);
270
+ }
271
+ if (result.code !== 0)
272
+ return putBack(`pg_restore failed (exit ${result.code}): ${result.stderr.trim().split('\n').slice(-3).join(' ')}`);
273
+ // The restore committed; failing to drop the old copy must not report the restore as failed.
274
+ await options.client.query(restoreFinishSql(aside)).catch((error) => {
275
+ log(` warning: the restore succeeded but the previous schema ${aside} could not be dropped (${error instanceof Error ? error.message : String(error)}); drop it with \`${restoreFinishSql(aside)}\` once you have checked the restored data`);
276
+ });
254
277
  }
255
278
  //# sourceMappingURL=migrations.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hivemagnus/magnus-server",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "Magnus server: the fleet control plane API (Hono, PostgreSQL, Redis) for nodes running magnus-agent, with optional Hive IAM, Hive Router, HiveMem, Hermes Agent and external security console integrations.",
5
5
  "keywords": [
6
6
  "magnus",
@@ -84,7 +84,7 @@
84
84
  "prepublishOnly": "npm run db:generate && npm run clean && npm run build"
85
85
  },
86
86
  "dependencies": {
87
- "@hivemagnus/magnus-contracts": "1.1.3",
87
+ "@hivemagnus/magnus-contracts": "1.1.4",
88
88
  "@hono/node-server": "^2.1.5",
89
89
  "@hono/swagger-ui": "^0.6.2",
90
90
  "@prisma/adapter-pg": "7.10.1",
@@ -102,7 +102,7 @@
102
102
  "ws": "^8.22.0",
103
103
  "yaml": "2.9.1",
104
104
  "zod": "^4.6.7",
105
- "@hivemagnus/magnus-skills": "1.1.3",
105
+ "@hivemagnus/magnus-skills": "1.1.4",
106
106
  "prisma": "^7.10.1"
107
107
  },
108
108
  "devDependencies": {