Skip to content

Commit 5faa048

Browse files
committed
feat(durable): record sqlite schema lifecycle
1 parent 99fa4dc commit 5faa048

6 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎src/durable/sqlite-store.test.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import os from "node:os";
33
import path from "node:path";
44
import { describe, expect, it } from "vitest";
55
import { requireNodeSqlite } from "../infra/node-sqlite.js";
6-
import { openDurableWorkflowSqliteStore } from "./sqlite-store.js";
6+
import {
7+
DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION,
8+
openDurableWorkflowSqliteStore,
9+
} from "./sqlite-store.js";
710

811
const DURABLE_TABLES = [
912
"durable_workflow_events",
@@ -16,6 +19,52 @@ const DURABLE_TABLES = [
1619
] as const;
1720

1821
describe("durable workflow sqlite store", () => {
22+
it("records the supported durable schema version", () => {
23+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-store-"));
24+
const store = openDurableWorkflowSqliteStore({
25+
path: path.join(dir, "openclaw.sqlite"),
26+
});
27+
try {
28+
expect(store.getStats()).toMatchObject({
29+
schemaVersion: DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION,
30+
});
31+
} finally {
32+
store.close();
33+
fs.rmSync(dir, { recursive: true, force: true });
34+
}
35+
});
36+
37+
it("rejects durable stores from a newer schema version", () => {
38+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-store-"));
39+
const dbPath = path.join(dir, "openclaw.sqlite");
40+
const { DatabaseSync } = requireNodeSqlite();
41+
const db = new DatabaseSync(dbPath);
42+
try {
43+
db.exec(`
44+
CREATE TABLE durable_schema_migrations (
45+
schema_name TEXT NOT NULL PRIMARY KEY,
46+
version INTEGER NOT NULL,
47+
applied_at INTEGER NOT NULL,
48+
metadata_json TEXT
49+
);
50+
`);
51+
db.prepare(
52+
`INSERT INTO durable_schema_migrations (schema_name, version, applied_at, metadata_json)
53+
VALUES (?, ?, ?, ?)`,
54+
).run("durable_workflows", DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION + 1, 100, null);
55+
} finally {
56+
db.close();
57+
}
58+
59+
try {
60+
expect(() => openDurableWorkflowSqliteStore({ path: dbPath })).toThrow(
61+
/newer than supported version/,
62+
);
63+
} finally {
64+
fs.rmSync(dir, { recursive: true, force: true });
65+
}
66+
});
67+
1968
it("creates runs, dedupes idempotency keys, and appends ordered events", () => {
2069
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-store-"));
2170
const store = openDurableWorkflowSqliteStore({

‎src/durable/sqlite-store.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,16 @@ type DurableWorkflowSignalRow = {
158158
};
159159

160160
type CountRow = { count: number | bigint };
161+
type DurableSchemaMigrationRow = {
162+
schema_name: string;
163+
version: number | bigint;
164+
applied_at: number | bigint;
165+
metadata_json: string | null;
166+
};
161167

162168
const DURABLE_WORKFLOW_SQLITE_BUSY_TIMEOUT_MS = 30_000;
169+
export const DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION = 1;
170+
const DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME = "durable_workflows";
163171

164172
function optionalText(value: string | undefined): string | null {
165173
return value && value.trim() ? value : null;
@@ -326,6 +334,13 @@ function ensureColumn(db: DatabaseSync, tableName: string, columnDefinition: str
326334

327335
function ensureDurableWorkflowSchema(db: DatabaseSync): void {
328336
db.exec(`
337+
CREATE TABLE IF NOT EXISTS durable_schema_migrations (
338+
schema_name TEXT NOT NULL PRIMARY KEY,
339+
version INTEGER NOT NULL,
340+
applied_at INTEGER NOT NULL,
341+
metadata_json TEXT
342+
);
343+
329344
CREATE TABLE IF NOT EXISTS durable_workflow_runs (
330345
workflow_run_id TEXT NOT NULL PRIMARY KEY,
331346
workflow_id TEXT NOT NULL,
@@ -507,6 +522,55 @@ function ensureDurableWorkflowSchema(db: DatabaseSync): void {
507522
for (const column of ["claimed_by TEXT", "claim_expires_at INTEGER", "heartbeat_at INTEGER"]) {
508523
ensureColumn(db, "durable_workflow_steps", column);
509524
}
525+
ensureDurableWorkflowSchemaVersion(db);
526+
}
527+
528+
function ensureDurableWorkflowSchemaVersion(db: DatabaseSync): void {
529+
const now = Date.now();
530+
const row = db
531+
.prepare("SELECT * FROM durable_schema_migrations WHERE schema_name = ?")
532+
.get(DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME) as DurableSchemaMigrationRow | undefined;
533+
const currentVersion = Number(row?.version ?? 0);
534+
if (currentVersion > DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION) {
535+
throw new Error(
536+
`Durable workflow schema version ${currentVersion} is newer than supported version ${DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION}`,
537+
);
538+
}
539+
if (currentVersion === DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION) {
540+
return;
541+
}
542+
543+
const metadata = JSON.stringify({
544+
kind: currentVersion === 0 ? "fresh-install" : "schema-upgrade",
545+
previousVersion: currentVersion,
546+
});
547+
if (row) {
548+
db.prepare(
549+
`UPDATE durable_schema_migrations
550+
SET version = ?,
551+
applied_at = ?,
552+
metadata_json = ?
553+
WHERE schema_name = ?`,
554+
).run(
555+
DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION,
556+
now,
557+
metadata,
558+
DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME,
559+
);
560+
return;
561+
}
562+
563+
db.prepare(
564+
`INSERT INTO durable_schema_migrations (schema_name, version, applied_at, metadata_json)
565+
VALUES (?, ?, ?, ?)`,
566+
).run(DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME, DURABLE_WORKFLOW_SQLITE_SCHEMA_VERSION, now, metadata);
567+
}
568+
569+
function getDurableWorkflowSchemaVersion(db: DatabaseSync): number {
570+
const row = db
571+
.prepare("SELECT * FROM durable_schema_migrations WHERE schema_name = ?")
572+
.get(DURABLE_WORKFLOW_SQLITE_SCHEMA_NAME) as DurableSchemaMigrationRow | undefined;
573+
return Number(row?.version ?? 0);
510574
}
511575

512576
function count(db: DatabaseSync, sql: string, values: SQLInputValue[] = []): number {
@@ -1386,6 +1450,7 @@ export function openDurableWorkflowSqliteStore(options?: {
13861450
getStats(): DurableWorkflowStoreStats {
13871451
return {
13881452
path: pathname,
1453+
schemaVersion: getDurableWorkflowSchemaVersion(db),
13891454
runs: count(db, "SELECT COUNT(*) AS count FROM durable_workflow_runs"),
13901455
events: count(db, "SELECT COUNT(*) AS count FROM durable_workflow_events"),
13911456
steps: count(db, "SELECT COUNT(*) AS count FROM durable_workflow_steps"),

‎src/durable/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ export type ClaimDurableWorkflowStepInput = {
358358

359359
export type DurableWorkflowStoreStats = {
360360
path: string;
361+
schemaVersion: number;
361362
runs: number;
362363
events: number;
363364
steps: number;

‎src/state/openclaw-state-db.generated.d.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ export interface DiagnosticStabilityBundles {
455455
reason: string;
456456
}
457457

458+
export interface DurableSchemaMigrations {
459+
applied_at: number;
460+
metadata_json: string | null;
461+
schema_name: string;
462+
version: number;
463+
}
464+
458465
export interface DurableWorkflowEvents {
459466
agent_invocation_id: string | null;
460467
causation_event_id: string | null;
@@ -1101,6 +1108,7 @@ export interface DB {
11011108
device_pairing_pending: DevicePairingPending;
11021109
diagnostic_events: DiagnosticEvents;
11031110
diagnostic_stability_bundles: DiagnosticStabilityBundles;
1111+
durable_schema_migrations: DurableSchemaMigrations;
11041112
durable_workflow_events: DurableWorkflowEvents;
11051113
durable_workflow_links: DurableWorkflowLinks;
11061114
durable_workflow_refs: DurableWorkflowRefs;

‎src/state/openclaw-state-schema.generated.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,13 @@ CREATE INDEX IF NOT EXISTS idx_flow_runs_status ON flow_runs(status);
11861186
CREATE INDEX IF NOT EXISTS idx_flow_runs_owner_key ON flow_runs(owner_key);
11871187
CREATE INDEX IF NOT EXISTS idx_flow_runs_updated_at ON flow_runs(updated_at);
11881188
1189+
CREATE TABLE IF NOT EXISTS durable_schema_migrations (
1190+
schema_name TEXT NOT NULL PRIMARY KEY,
1191+
version INTEGER NOT NULL,
1192+
applied_at INTEGER NOT NULL,
1193+
metadata_json TEXT
1194+
);
1195+
11891196
CREATE TABLE IF NOT EXISTS durable_workflow_runs (
11901197
workflow_run_id TEXT NOT NULL PRIMARY KEY,
11911198
workflow_id TEXT NOT NULL,

‎src/state/openclaw-state-schema.sql‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,13 @@ CREATE INDEX IF NOT EXISTS idx_flow_runs_status ON flow_runs(status);
11811181
CREATE INDEX IF NOT EXISTS idx_flow_runs_owner_key ON flow_runs(owner_key);
11821182
CREATE INDEX IF NOT EXISTS idx_flow_runs_updated_at ON flow_runs(updated_at);
11831183

1184+
CREATE TABLE IF NOT EXISTS durable_schema_migrations (
1185+
schema_name TEXT NOT NULL PRIMARY KEY,
1186+
version INTEGER NOT NULL,
1187+
applied_at INTEGER NOT NULL,
1188+
metadata_json TEXT
1189+
);
1190+
11841191
CREATE TABLE IF NOT EXISTS durable_workflow_runs (
11851192
workflow_run_id TEXT NOT NULL PRIMARY KEY,
11861193
workflow_id TEXT NOT NULL,

0 commit comments

Comments
 (0)