Skip to content

Commit 52ba68e

Browse files
saju01claude
andcommitted
test(state): enforce that first-use additive columns stay bare nullable
Materializing these columns before the STRICT rebuild is only safe while every one of them is bare and nullable: the rebuild adds them to tables that already hold rows, so a NOT NULL, defaulted, constrained, or key-bearing column would fail the migration or silently rewrite existing data. That property was documented as a convention on the definition list. Assert it against the canonical schema instead, one case per first-use column, so adding a non-nullable column to the list fails CI rather than pre-STRICT upgrades. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent ccb5875 commit 52ba68e

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎src/state/openclaw-state-db-first-use-column-strict-migration.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import { afterEach, describe, expect, it } from "vitest";
44
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
55
import { requireNodeSqlite } from "../infra/node-sqlite.js";
6+
import { CLAW_FIRST_USE_ADDITIVE_STATE_COLUMN_DEFINITIONS } from "./openclaw-state-db-additive-columns.js";
67
import {
78
closeOpenClawStateDatabaseForTest,
89
openOpenClawStateDatabase,
910
OPENCLAW_STATE_SCHEMA_VERSION,
1011
repairOpenClawStateDatabaseSchema,
1112
} from "./openclaw-state-db.js";
13+
import { OPENCLAW_STATE_SCHEMA_SQL } from "./openclaw-state-schema.js";
1214

1315
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
1416

@@ -68,6 +70,40 @@ function makePreStrictDatabaseWithoutColumns(params: {
6870
return databasePath;
6971
}
7072

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+
71107
describe("first-use additive column STRICT migration", () => {
72108
it("repairs device_bootstrap_tokens without setup_id while migrating to STRICT", () => {
73109
const stateDir = tempDirs.make("openclaw-state-first-use-column-");

0 commit comments

Comments
 (0)