openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
42 lines (41 loc) • 2.66 kB
JavaScript
import { i as executeWithCachedStatement } from "./kysely-sync-cache-state-eUau8Jaa.mjs";
import { n as getAdmittedSqliteSchemaFacts } from "./sqlite-schema-facts-CG0Tk84j.mjs";
import { d as sqlitePrimaryResultCode } from "./sqlite-error-diagnostics-DCvjvszg.mjs";
import { t as SqliteSchemaMismatchError } from "./sqlite-schema-issues-Cd5MmsG2.mjs";
//#region src/state/openclaw-state-db-schema-helpers.ts
function tableHasColumn(db, tableName, columnName) {
return readTableColumns(db, tableName).has(columnName);
}
function readTableColumns(db, tableName) {
const rows = db.prepare(`PRAGMA table_info(${tableName})`).all();
return new Set(rows.flatMap((row) => typeof row.name === "string" ? [row.name] : []));
}
/** Inspect only failed queries; native unavailability must not become a schema refusal. */
function classifySqliteTableReadError(db, tableName, columnNames, error) {
if (sqlitePrimaryResultCode(error) !== 1) return error;
try {
const existing = readTableColumns(db, tableName);
if (existing.size > 0 && columnNames.some((column) => !existing.has(column))) return new SqliteSchemaMismatchError(`SQLite table ${tableName} is missing required columns; run openclaw doctor --fix to repair it.`, { cause: error });
} catch {}
return error;
}
function tablePrimaryKeyColumns(db, tableName) {
return db.prepare(`PRAGMA table_info(${tableName})`).all().filter((row) => Number(row.pk ?? 0) > 0 && typeof row.name === "string").toSorted((left, right) => Number(left.pk ?? 0) - Number(right.pk ?? 0)).map((row) => row.name);
}
function tableExists(db, tableName) {
const schema = getAdmittedSqliteSchemaFacts(db);
if (schema) return schema.tables.has(tableName);
return executeWithCachedStatement(db, "SELECT 1 AS ok FROM sqlite_master WHERE type = 'table' AND name = ?", [tableName], (statement) => statement.get(tableName))?.ok === 1;
}
function ensureColumn(db, tableName, columnSql) {
const columnName = columnSql.trim().split(/\s+/, 1)[0];
if (!columnName || !tableExists(db, tableName) || tableHasColumn(db, tableName, columnName)) return false;
db.exec(`ALTER TABLE ${tableName} ADD COLUMN ${columnSql};`);
return true;
}
/** Missing runtime tables are empty only before state grows beyond checkpoint bootstrap. */
function hasOpenClawStateTablesBeyondStartupCheckpoint(db) {
return db.prepare("SELECT 1 FROM main.sqlite_schema WHERE type = 'table' AND name NOT IN ('schema_meta', 'state_leases') LIMIT 1").get() !== void 0;
}
//#endregion
export { tableHasColumn as a, tableExists as i, ensureColumn as n, tablePrimaryKeyColumns as o, hasOpenClawStateTablesBeyondStartupCheckpoint as r, classifySqliteTableReadError as t };