openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
77 lines (76 loc) • 4.38 kB
JavaScript
import { a as readSqliteWriterAppVersion, i as readSqliteUserVersion, n as createNewerSqliteSchemaVersionError } from "./sqlite-user-version-D-rYjp-A.mjs";
import "./openclaw-state-db-contract-DpFGI8ZZ.mjs";
import { n as isPostgresSqlConnection } from "./sql-connection-VmV9pnJI.mjs";
import { S as getNodeSqliteKysely, i as tableExists, m as SqliteSchemaMismatchError, p as rememberStateSchemaVersionAdmission, u as getStateSchemaVersionAdmission, w as prepareSqliteQuerySync, y as createSqliteQueryCache } from "./openclaw-state-db-schema-helpers-Bn-sHIhd.mjs";
import { g as getPostgresPhysicalFacts, r as getAdmittedSqliteSchemaFacts, t as admitSqliteSchema } from "./sqlite-schema-facts-BFHoBJFZ.mjs";
import { n as normalizeOpenClawStateSchemaReadError } from "./openclaw-state-db-schema-migration-required-wTocmCbq.mjs";
//#region src/state/openclaw-state-db-schema-version.ts
const CONTENT_VERSION_KEY = "state.schema.contentVersion";
var PostgresSchemaAdmissionUnavailableError = class extends Error {};
const contentVersionQuery = createSqliteQueryCache((e) => prepareSqliteQuerySync(e, () => getNodeSqliteKysely(e).selectFrom("config_machine_state").select("value_json").where("state_key", "=", CONTENT_VERSION_KEY)));
/** Content and its marker commit together, even while older readers retain their version floor. */
function readStateSchemaContentVersion(e, t, n = readStateSchemaContentVersionRow, r) {
if (isPostgresSqlConnection(e)) {
const n = getStateSchemaVersionAdmission(e);
if (!n) throw new PostgresSchemaAdmissionUnavailableError("Missing PostgreSQL state schema admission; use the matching OpenClaw version or restore the matching PostgreSQL schema and anchor.");
return Math.max(t ?? n.userVersion, n.contentVersion);
}
const i = getStateSchemaVersionAdmission(e);
if (i) return Math.max(t ?? i.userVersion, i.contentVersion);
const a = r ?? getAdmittedSqliteSchemaFacts(e)?.userVersion ?? readSqliteUserVersion(e);
const o = parseStateSchemaContentVersion(tableExists(e, "config_machine_state") ? n(e)?.value_json : void 0);
rememberStateSchemaVersionAdmission(e, {
userVersion: a,
contentVersion: o
});
return Math.max(t ?? a, o);
}
function readStateSchemaContentVersionRow(e) {
return contentVersionQuery(e)().rows[0];
}
function parseStateSchemaContentVersion(e) {
if (e === void 0) return 0;
let t;
try {
t = e === null ? null : JSON.parse(e);
} catch (e) {
throw new SqliteSchemaMismatchError(`Invalid shared state schema content version in ${CONTENT_VERSION_KEY}.`, { cause: e });
}
if (typeof t !== "number" || !Number.isSafeInteger(t) || t < 0) throw new SqliteSchemaMismatchError(`Invalid shared state schema content version in ${CONTENT_VERSION_KEY}.`);
return t;
}
/** Refuse newer headers before catalog inspection, then share one physical schema capture. */
function admitStateSchemaFacts(e, t, n = "runtime") {
if (isPostgresSqlConnection(e)) {
if (!getPostgresPhysicalFacts(e)) throw new PostgresSchemaAdmissionUnavailableError("Missing PostgreSQL physical schema admission");
return;
}
try {
admitSqliteSchema(e, (r) => assertSupportedStateSchemaVersion(e, t, {
userVersion: r,
contentVersion: r
}, void 0, n));
} catch (r) {
let i;
try {
i = readSqliteUserVersion(e);
} catch {
throw normalizeOpenClawStateSchemaReadError(r, t);
}
if (i > 36) throw createNewerSqliteSchemaVersionError("OpenClaw state database", t, i, 36, readSqliteWriterAppVersion(e), n);
throw normalizeOpenClawStateSchemaReadError(r, t);
}
}
function assertSupportedStateSchemaVersion(e, t, n, r, i = "runtime") {
if (isPostgresSqlConnection(e)) return readStateSchemaContentVersion(e);
try {
const a = n?.userVersion ?? getStateSchemaVersionAdmission(e)?.userVersion ?? getAdmittedSqliteSchemaFacts(e)?.userVersion ?? readSqliteUserVersion(e);
const o = n?.contentVersion ?? (a > 36 ? a : readStateSchemaContentVersion(e, a, r, a));
if (o > 36) throw createNewerSqliteSchemaVersionError("OpenClaw state database", t, o, 36, readSqliteWriterAppVersion(e), i);
return a;
} catch (e) {
throw normalizeOpenClawStateSchemaReadError(e, t);
}
}
//#endregion
export { parseStateSchemaContentVersion as a, assertSupportedStateSchemaVersion as i, PostgresSchemaAdmissionUnavailableError as n, readStateSchemaContentVersion as o, admitStateSchemaFacts as r, CONTENT_VERSION_KEY as t };