|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { DatabaseSync } from "node:sqlite"; |
| 3 | +import { isRecord } from "@openclaw/normalization-core/record-coerce"; |
| 4 | +import { expect, it } from "vitest"; |
| 5 | +import { OPENCLAW_AGENT_SCHEMA_VERSION } from "./openclaw-agent-db-contract.js"; |
| 6 | +import retirementManifest from "./openclaw-schema-retirements.json" with { type: "json" }; |
| 7 | +import { OPENCLAW_STATE_SCHEMA_VERSION } from "./openclaw-state-db-contract.js"; |
| 8 | + |
| 9 | +type DatabaseOwner = "state" | "agent"; |
| 10 | +type RetirementStatus = "planned" | "completed"; |
| 11 | + |
| 12 | +type SchemaRetirement = { |
| 13 | + database: DatabaseOwner; |
| 14 | + status: RetirementStatus; |
| 15 | + targetVersion: number; |
| 16 | + table: string; |
| 17 | + indexes: string[]; |
| 18 | + note?: string; |
| 19 | +}; |
| 20 | + |
| 21 | +function assertInvariant(condition: unknown, message: string): asserts condition { |
| 22 | + if (!condition) { |
| 23 | + throw new Error(message); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function parseString(value: unknown, field: string): string { |
| 28 | + assertInvariant(typeof value === "string" && value.length > 0, `${field} must be a string`); |
| 29 | + return value; |
| 30 | +} |
| 31 | + |
| 32 | +function parseStringArray(value: unknown, field: string): string[] { |
| 33 | + assertInvariant(Array.isArray(value), `${field} must be an array`); |
| 34 | + return value.map((entry, index) => parseString(entry, `${field}[${index}]`)); |
| 35 | +} |
| 36 | + |
| 37 | +function parseRetirement(value: unknown, index: number): SchemaRetirement { |
| 38 | + assertInvariant(isRecord(value), `retirements[${index}] must be an object`); |
| 39 | + assertInvariant( |
| 40 | + value.database === "state" || value.database === "agent", |
| 41 | + `retirements[${index}].database must name a database owner`, |
| 42 | + ); |
| 43 | + assertInvariant( |
| 44 | + value.status === "planned" || value.status === "completed", |
| 45 | + `retirements[${index}].status must be planned or completed`, |
| 46 | + ); |
| 47 | + assertInvariant( |
| 48 | + Number.isInteger(value.targetVersion) && Number(value.targetVersion) >= 0, |
| 49 | + `retirements[${index}].targetVersion must be a non-negative integer`, |
| 50 | + ); |
| 51 | + assertInvariant( |
| 52 | + value.note === undefined || typeof value.note === "string", |
| 53 | + `retirements[${index}].note must be a string when present`, |
| 54 | + ); |
| 55 | + |
| 56 | + return { |
| 57 | + database: value.database, |
| 58 | + status: value.status, |
| 59 | + targetVersion: Number(value.targetVersion), |
| 60 | + table: parseString(value.table, `retirements[${index}].table`), |
| 61 | + indexes: parseStringArray(value.indexes, `retirements[${index}].indexes`), |
| 62 | + note: value.note, |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +function parseRetirementManifest(value: unknown): SchemaRetirement[] { |
| 67 | + assertInvariant(isRecord(value), "retirement manifest must be an object"); |
| 68 | + assertInvariant(Array.isArray(value.retirements), "retirements must be an array"); |
| 69 | + return value.retirements.map(parseRetirement); |
| 70 | +} |
| 71 | + |
| 72 | +function assertUniqueRetirements(retirements: readonly SchemaRetirement[]): void { |
| 73 | + const seen = new Set<string>(); |
| 74 | + for (const retirement of retirements) { |
| 75 | + const key = `${retirement.database}/${retirement.table}`; |
| 76 | + assertInvariant(!seen.has(key), `duplicate schema retirement: ${key}`); |
| 77 | + seen.add(key); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function hasSchemaObject(database: DatabaseSync, type: "table" | "index", name: string): boolean { |
| 82 | + return ( |
| 83 | + database.prepare("SELECT 1 FROM sqlite_schema WHERE type = ? AND name = ?").get(type, name) !== |
| 84 | + undefined |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +it("keeps the schema retirement ledger aligned with canonical database schemas", () => { |
| 89 | + const retirements = parseRetirementManifest(retirementManifest); |
| 90 | + assertUniqueRetirements(retirements); |
| 91 | + |
| 92 | + const databases = { |
| 93 | + state: { |
| 94 | + currentVersion: OPENCLAW_STATE_SCHEMA_VERSION, |
| 95 | + schemaUrl: new URL("./openclaw-state-schema.sql", import.meta.url), |
| 96 | + database: new DatabaseSync(":memory:"), |
| 97 | + }, |
| 98 | + agent: { |
| 99 | + currentVersion: OPENCLAW_AGENT_SCHEMA_VERSION, |
| 100 | + schemaUrl: new URL("./openclaw-agent-schema.sql", import.meta.url), |
| 101 | + database: new DatabaseSync(":memory:"), |
| 102 | + }, |
| 103 | + } satisfies Record< |
| 104 | + DatabaseOwner, |
| 105 | + { currentVersion: number; schemaUrl: URL; database: DatabaseSync } |
| 106 | + >; |
| 107 | + |
| 108 | + try { |
| 109 | + for (const database of Object.values(databases)) { |
| 110 | + database.database.exec(readFileSync(database.schemaUrl, "utf8")); |
| 111 | + } |
| 112 | + |
| 113 | + for (const retirement of retirements) { |
| 114 | + const { currentVersion, database } = databases[retirement.database]; |
| 115 | + const shouldExist = retirement.status === "planned"; |
| 116 | + |
| 117 | + if (shouldExist) { |
| 118 | + expect(retirement.targetVersion).toBe(currentVersion + 1); |
| 119 | + } else { |
| 120 | + expect(retirement.targetVersion).toBeLessThanOrEqual(currentVersion); |
| 121 | + } |
| 122 | + |
| 123 | + expect(hasSchemaObject(database, "table", retirement.table)).toBe(shouldExist); |
| 124 | + for (const index of retirement.indexes) { |
| 125 | + expect(hasSchemaObject(database, "index", index)).toBe(shouldExist); |
| 126 | + } |
| 127 | + } |
| 128 | + } finally { |
| 129 | + for (const database of Object.values(databases)) { |
| 130 | + database.database.close(); |
| 131 | + } |
| 132 | + } |
| 133 | +}); |
0 commit comments