|
3 | 3 | import { afterEach, describe, expect, it } from "vitest"; |
4 | 4 | import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
5 | 5 | import { requireNodeSqlite } from "../infra/node-sqlite.js"; |
| 6 | +import { CLAW_FIRST_USE_ADDITIVE_STATE_COLUMN_DEFINITIONS } from "./openclaw-state-db-additive-columns.js"; |
6 | 7 | import { |
7 | 8 | closeOpenClawStateDatabaseForTest, |
8 | 9 | openOpenClawStateDatabase, |
9 | 10 | OPENCLAW_STATE_SCHEMA_VERSION, |
10 | 11 | repairOpenClawStateDatabaseSchema, |
11 | 12 | } from "./openclaw-state-db.js"; |
| 13 | +import { OPENCLAW_STATE_SCHEMA_SQL } from "./openclaw-state-schema.js"; |
12 | 14 |
|
13 | 15 | const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
14 | 16 |
|
@@ -68,6 +70,40 @@ function makePreStrictDatabaseWithoutColumns(params: { |
68 | 70 | return databasePath; |
69 | 71 | } |
70 | 72 |
|
| 73 | +describe("first-use additive column definitions", () => { |
| 74 | + // Materializing these columns early is only safe while every one of them is |
| 75 | + // bare and nullable: the pre-STRICT rebuild adds them to tables that already |
| 76 | + // hold rows, so anything NOT NULL, defaulted, constrained, or key-bearing |
| 77 | + // would fail or silently rewrite existing data. Enforce that here rather than |
| 78 | + // leaving it to the convention documented on the definition list. |
| 79 | + it.each( |
| 80 | + CLAW_FIRST_USE_ADDITIVE_STATE_COLUMN_DEFINITIONS.map((definition) => [ |
| 81 | + `${definition.tableName}.${definition.columnName}`, |
| 82 | + definition, |
| 83 | + ]), |
| 84 | + )("keeps %s bare and nullable in the canonical schema", (_label, definition) => { |
| 85 | + const { columnName, dataType, tableName } = definition as { |
| 86 | + columnName: string; |
| 87 | + dataType: string; |
| 88 | + tableName: string; |
| 89 | + }; |
| 90 | + const tableStart = OPENCLAW_STATE_SCHEMA_SQL.indexOf( |
| 91 | + `CREATE TABLE IF NOT EXISTS ${tableName} (`, |
| 92 | + ); |
| 93 | + expect(tableStart).toBeGreaterThanOrEqual(0); |
| 94 | + const tableBody = OPENCLAW_STATE_SCHEMA_SQL.slice( |
| 95 | + tableStart, |
| 96 | + OPENCLAW_STATE_SCHEMA_SQL.indexOf(") STRICT;", tableStart), |
| 97 | + ); |
| 98 | + const declaration = tableBody |
| 99 | + .split("\n") |
| 100 | + .find((line) => new RegExp(`^\\s*${columnName}\\s`, "u").test(line)) |
| 101 | + ?.trim() |
| 102 | + .replace(/,$/u, ""); |
| 103 | + expect(declaration).toBe(`${columnName} ${dataType}`); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
71 | 107 | describe("first-use additive column STRICT migration", () => { |
72 | 108 | it("repairs device_bootstrap_tokens without setup_id while migrating to STRICT", () => { |
73 | 109 | const stateDir = tempDirs.make("openclaw-state-first-use-column-"); |
|
0 commit comments