refactor(state): inline canonical database schemas at build (#117433)

This commit is contained in:
Peter Steinberger
2026-08-01 08:39:42 -07:00
committed by GitHub
parent 1130f30445
commit e7950d968b
26 changed files with 141 additions and 2885 deletions

View File

@@ -31,6 +31,7 @@ const TSDOWN_SOURCE_EXTENSIONS = [
".json5",
".mjs",
".mts",
".sql",
".ts",
".tsx",
".yaml",

View File

@@ -10,15 +10,11 @@ const SCHEMAS = [
name: "openclaw-state",
schema: "src/state/openclaw-state-schema.sql",
outFile: "src/state/openclaw-state-db.generated.d.ts",
schemaOutFile: "src/state/openclaw-state-schema.generated.ts",
schemaExport: "OPENCLAW_STATE_SCHEMA_SQL",
},
{
name: "openclaw-agent",
schema: "src/state/openclaw-agent-schema.sql",
outFile: "src/state/openclaw-agent-db.generated.d.ts",
schemaOutFile: "src/state/openclaw-agent-schema.generated.ts",
schemaExport: "OPENCLAW_AGENT_SCHEMA_SQL",
},
];
@@ -118,39 +114,19 @@ function readUtf8(file) {
return fs.readFileSync(file, "utf8");
}
function generatedSchemaModule(schema) {
const source = readUtf8(schema.schema).trimEnd();
const literal = source.replaceAll("\\", "\\\\").replaceAll("`", "\\`").replaceAll("${", "\\${");
return [
"/**",
" * This file was generated from the SQLite schema source.",
" * Please do not edit it manually.",
" */",
"",
`export const ${schema.schemaExport} = \`${literal}\\n\`;`,
"",
].join("\n");
}
function generate(schema) {
const db = new DatabaseSync(":memory:");
try {
db.exec(readUtf8(schema.schema));
const typesSource = generateTypes(db);
const schemaSource = generatedSchemaModule(schema);
if (verify) {
if (typesSource !== readUtf8(schema.outFile)) {
console.error(`${schema.outFile} is out of date. Run pnpm db:kysely:gen.`);
process.exitCode = 1;
}
if (schemaSource !== readUtf8(schema.schemaOutFile)) {
console.error(`${schema.schemaOutFile} is out of date. Run pnpm db:kysely:gen.`);
process.exitCode = 1;
}
} else {
fs.writeFileSync(schema.outFile, typesSource);
fs.writeFileSync(schema.schemaOutFile, schemaSource);
}
} finally {
db.close();