openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
57 lines (56 loc) • 2.57 kB
JavaScript
import { n as isPostgresSqlConnection } from "./sql-connection-Beiyg5Dt.mjs";
import { d as invalidateSqliteSchemaFacts } from "./sqlite-schema-facts-B1bVE8IB.mjs";
import { t as LEGACY_SKILL_WORKSHOP_COLLECTION_REVIEWS_INDEX } from "./openclaw-state-db-schema-migration-required-D2P85VUd.mjs";
//#region src/state/openclaw-state-db-doctor-schema.ts
const LEGACY_SKILL_WORKSHOP_COLLECTION_REVIEWS_INDEX_SQL = "CREATE INDEX idx_skill_workshop_collection_reviews_workspace_time ON skill_workshop_collection_reviews(workspace_dir, create_time DESC, review_id DESC)";
function normalizeSqliteCatalogSql(e) {
return e.replace(/\s+/gu, " ").replace(/\s*([(),])\s*/gu, "$1").trim();
}
function withSqliteWritableSchema(e, t) {
e.enableDefensive?.(false);
e.exec("PRAGMA writable_schema = ON;");
try {
return t();
} finally {
try {
e.exec("PRAGMA writable_schema = RESET;");
} finally {
e.enableDefensive?.(true);
}
}
}
/** Detect only the known v15 review index left behind after its column was retired. */
function hasDanglingSkillWorkshopCollectionReviewIndex(e) {
return withSqliteWritableSchema(e, () => inspectSkillWorkshopCollectionReviewIndex(e));
}
function inspectSkillWorkshopCollectionReviewIndex(e) {
const t = e.prepare("SELECT tbl_name, rootpage, sql FROM sqlite_schema WHERE type = 'index' AND name = ?").get(LEGACY_SKILL_WORKSHOP_COLLECTION_REVIEWS_INDEX);
if (t?.tbl_name !== "skill_workshop_collection_reviews" || typeof t.rootpage !== "number" || t.rootpage <= 0 || typeof t.sql !== "string" || normalizeSqliteCatalogSql(t.sql) !== normalizeSqliteCatalogSql(LEGACY_SKILL_WORKSHOP_COLLECTION_REVIEWS_INDEX_SQL)) return false;
const n = e.prepare("PRAGMA table_info(skill_workshop_collection_reviews)").all();
return n.some((e) => e.name === "owner_agent_id") && !n.some((e) => e.name === "workspace_dir");
}
/** Doctor can inspect the exact legacy defect before its repair transaction. */
function openDoctorStateSchemaReadAdmission(e) {
if (isPostgresSqlConnection(e)) return;
invalidateSqliteSchemaFacts(e);
if (!hasDanglingSkillWorkshopCollectionReviewIndex(e)) return;
e.enableDefensive?.(false);
try {
e.exec("PRAGMA writable_schema = ON;");
} catch (t) {
e.enableDefensive?.(true);
throw t;
}
let t = true;
return () => {
if (!t) return;
t = false;
try {
e.exec("PRAGMA writable_schema = RESET;");
} finally {
e.enableDefensive?.(true);
}
};
}
//#endregion
export { openDoctorStateSchemaReadAdmission as n, withSqliteWritableSchema as r, hasDanglingSkillWorkshopCollectionReviewIndex as t };