openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
130 lines (129 loc) • 5.55 kB
JavaScript
import { i as withEngineStoreAdmission } from "./admission-scope-DmxaPNhm.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { a as getOpenClawDatabaseMaintenanceScope } from "./openclaw-state-maintenance-context-D6i7Zdhn.mjs";
import { n as openNodeSqliteDatabase } from "./node-sqlite-CU4puofF.mjs";
import { l as resolveSqliteInspectionSignal } from "./sqlite-readonly-worker-D3pRJU7r.mjs";
import { l as runSqliteIntegrityOperationSync } from "./sqlite-integrity-CCmEgO_m.mjs";
import { c as runUpdateSqliteMigration, o as hasUpdateDoctorMigrationCapture } from "./update-doctor-migration-attribution-Dp_N9vmd.mjs";
import { a as isStatePostgres } from "./openclaw-state-db-engine-sM0tpxNy.mjs";
import { c as repairOpenClawStateDatabaseSchema, i as prepareOpenClawStateDatabaseSchema, r as openOpenClawStateDatabase } from "./openclaw-state-db-ARhwdPJg.mjs";
import { f as createUpdateDoctorDatabaseWriteCapture } from "./update-doctor-result-DwaI51On.mjs";
import { i as openAgentEngineStore, n as agentEngineInput } from "./openclaw-agent-db-engine-CFD02117.mjs";
import { i as ensureOpenClawAgentDatabaseSchemaSteps, n as ensureAgentSchema } from "./openclaw-agent-db-schema-D-iWGl31.mjs";
import { t as withAgentDatabaseMaintenanceLease } from "./openclaw-agent-db-maintenance-lease-Jvh6K3Fz.mjs";
import { t as completeDoctorMigrationBackups } from "./doctor-migration-backup-artifacts-DjxmWbF6.mjs";
import { t as backupDoctorMigrationDatabases } from "./doctor-migration-backup-DwSvhlqi.mjs";
//#region src/commands/doctor-schema-migrations.ts
/** Backups precede both callers; agent lease acquisition must not do shared repair. */
async function prepareSharedStateForAgentMaintenance(e, t) {
if (isStatePostgres()) return;
const n = await prepareOpenClawStateDatabaseSchema({
env: e.env,
integrityCheck: e.check ?? "quick"
}, "doctor-preparation");
if (n.warnings.length > 0) throw new Error(n.warnings.join("\n"));
t.push(...n.changes);
}
async function prepareUpdateAgentAdditiveSchemas(e, t) {
const n = e.agentDatabaseTargets;
if (!hasUpdateDoctorMigrationCapture() || agentEngineInput() || !n?.length) return;
await prepareSharedStateForAgentMaintenance(e, t);
await withAgentDatabaseMaintenanceLease({
env: e.env,
processBound: true
}, async (e) => {
for (const t of n) {
e.assertOwned();
const n = openNodeSqliteDatabase(t.path);
try {
runUpdateSqliteMigration(n, t.path, () => ensureAgentSchema(n, t.agentId, t.path));
} finally {
n.close();
}
}
});
}
/** Commit the declared version phase before ordinary Doctor maintenance begins. */
async function prepareDoctorDatabaseMigrations(e) {
const t = await backupDoctorMigrationDatabases({
...e,
versionMigrationsOnly: true
});
const n = t.versionMigrations;
if (!n?.length) {
await prepareUpdateAgentAdditiveSchemas(e, t.changes);
return t;
}
const r = t.postgresGenerations ? createUpdateDoctorDatabaseWriteCapture(t.postgresGenerations, {
env: e.env,
signal: resolveSqliteInspectionSignal() ?? new AbortController().signal,
assertCurrent() {
const e = getOpenClawDatabaseMaintenanceScope();
if (!e?.ownsSchemaMaintenance) throw new Error("PostgreSQL version migration lost schema maintenance custody.");
e.assertAdmission();
},
warn: (e) => t.warnings.push(e)
}) : void 0;
const runMigration = (e) => r ? r.run(e) : e();
for (const r of n.filter((e) => e.kind === "state")) {
if (isStatePostgres()) {
runMigration(() => withEngineStoreAdmission(() => openOpenClawStateDatabase({
path: r.path,
env: e.env
})));
t.changes.push(`Upgraded PostgreSQL state schema: v${r.foundVersion} -> v${r.supportedVersion}.`);
continue;
}
const n = repairOpenClawStateDatabaseSchema({
path: r.path,
env: e.env,
versionMigrationOnly: true,
integrityCheck: e.check ?? "quick"
});
if (n.warnings.length > 0) throw new Error(n.warnings.join("\n"));
t.changes.push(...n.changes);
}
const i = n.filter((e) => e.kind === "agent");
if (i.length > 0) {
await prepareSharedStateForAgentMaintenance(e, t.changes);
await withAgentDatabaseMaintenanceLease({
env: e.env,
processBound: true,
schemaPolicy: isStatePostgres() ? "existing" : void 0
}, async (n) => {
for (const r of i) {
const i = e.agentDatabaseTargets?.find(({ path: e }) => e === r.path);
if (!i) throw new Error(`Missing discovered agent migration target: ${r.path}`);
const a = openNodeSqliteDatabase(i.path);
try {
if (agentEngineInput()) {
n.assertOwned();
runMigration(() => withEngineStoreAdmission(() => {
const e = openAgentEngineStore(a, i.agentId, i.path, true);
if (e !== a) e.close();
}));
} else runUpdateSqliteMigration(a, i.path, () => runSqliteIntegrityOperationSync(ensureOpenClawAgentDatabaseSchemaSteps(a, {
agentId: i.agentId,
path: i.path,
env: e.env,
versionMigrationOnly: true,
integrityCheck: e.check ?? "quick",
onMigrationWarning: (e) => t.warnings.push(e)
})));
} finally {
a.close();
}
t.changes.push(`Upgraded agent database schema in ${i.path}: v${r.foundVersion} -> v${r.supportedVersion}.`);
}
});
}
try {
completeDoctorMigrationBackups(e.env, e.databasePaths, true);
} catch (e) {
t.warnings.push(`Could not mark committed schema migration backups complete: ${formatErrorMessage(e)}`);
}
await prepareUpdateAgentAdditiveSchemas(e, t.changes);
return t;
}
//#endregion
export { prepareDoctorDatabaseMigrations };