Skip to content

Commit 0748bfa

Browse files
authored
refactor(state): simplify schema migration validation (#122959)
1 parent 3f731db commit 0748bfa

4 files changed

Lines changed: 108 additions & 210 deletions

File tree

‎src/state/openclaw-state-db-maintenance.ts‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const STATE_V5_ADDITIVE_TABLES = [
4242
"worker_transcript_commits",
4343
...STATE_V6_ADDITIVE_TABLES,
4444
] as const;
45+
const STATE_MIGRATION_ALLOWED_MISSING_TABLES = {
46+
5: STATE_V5_ADDITIVE_TABLES,
47+
6: STATE_V6_ADDITIVE_TABLES,
48+
} as const satisfies Record<number, readonly string[]>;
49+
type OpenClawStateMigrationVersion = keyof typeof STATE_MIGRATION_ALLOWED_MISSING_TABLES;
4550

4651
/** Open shared SQLite database handle plus WAL maintenance lifecycle. */
4752

@@ -133,7 +138,7 @@ export function assertOpenClawStateDatabaseForMaintenance(
133138

134139
function assertOpenClawStateDatabaseVersionForMigration(
135140
database: DatabaseSync,
136-
options: { pathname: string; version: number; allowedMissingTables: readonly string[] },
141+
options: { pathname: string; version: OpenClawStateMigrationVersion },
137142
): void {
138143
const userVersion = readSqliteUserVersion(database);
139144
if (userVersion !== options.version) {
@@ -153,7 +158,7 @@ function assertOpenClawStateDatabaseVersionForMigration(
153158
);
154159
}
155160
assertSqliteSchemaTablesPresent(database, options.pathname, OPENCLAW_STATE_SCHEMA_SQL, {
156-
allowedMissingTables: options.allowedMissingTables,
161+
allowedMissingTables: STATE_MIGRATION_ALLOWED_MISSING_TABLES[options.version],
157162
});
158163
}
159164

@@ -162,23 +167,15 @@ export function assertOpenClawStateDatabaseV5ForMigration(
162167
database: DatabaseSync,
163168
options: { pathname: string },
164169
): void {
165-
assertOpenClawStateDatabaseVersionForMigration(database, {
166-
...options,
167-
version: 5,
168-
allowedMissingTables: STATE_V5_ADDITIVE_TABLES,
169-
});
170+
assertOpenClawStateDatabaseVersionForMigration(database, { ...options, version: 5 });
170171
}
171172

172173
/** Require every stable v6 table before the v7 retirement migration can run. */
173174
export function assertOpenClawStateDatabaseV6ForMigration(
174175
database: DatabaseSync,
175176
options: { pathname: string },
176177
): void {
177-
assertOpenClawStateDatabaseVersionForMigration(database, {
178-
...options,
179-
version: 6,
180-
allowedMissingTables: STATE_V6_ADDITIVE_TABLES,
181-
});
178+
assertOpenClawStateDatabaseVersionForMigration(database, { ...options, version: 6 });
182179
}
183180

184181
export function resolveDatabasePath(options: OpenClawStateDatabaseOptions = {}): string {

‎src/state/openclaw-state-db-schema-repair.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ const ADDITIVE_RETIRED_COMMITMENTS_SCHEMA_COMPATIBILITY: SqliteSchemaCompatibili
186186
function hasSupportedRetiredCommitmentsSchema(
187187
db: DatabaseSync,
188188
schemaSql: string,
189-
expectedIndexNames: readonly string[],
190-
compatibility: SqliteSchemaCompatibility = {},
189+
compatibility: SqliteSchemaCompatibility,
191190
): boolean {
192191
if (collectSqliteSchemaIssues(db, schemaSql, compatibility).length > 0) {
193192
return false;
@@ -202,7 +201,7 @@ function hasSupportedRetiredCommitmentsSchema(
202201
ORDER BY type, name`,
203202
)
204203
.all() as Array<{ name: string; type: string }>;
205-
const expectedIndexes = new Set(expectedIndexNames);
204+
const expectedIndexes = new Set<string>(RETIRED_COMMITMENTS_INDEX_NAMES);
206205
return attachedObjects.every(
207206
(object) => object.type === "index" && expectedIndexes.has(object.name),
208207
);
@@ -228,13 +227,11 @@ function hasRecognizedRetiredCommitmentsSchema(db: DatabaseSync): boolean {
228227
hasSupportedRetiredCommitmentsSchema(
229228
db,
230229
RETIRED_COMMITMENTS_SCHEMA_SQL,
231-
RETIRED_COMMITMENTS_INDEX_NAMES,
232230
RETIRED_COMMITMENTS_SCHEMA_COMPATIBILITY,
233231
) ||
234232
hasSupportedRetiredCommitmentsSchema(
235233
db,
236234
ADDITIVE_RETIRED_COMMITMENTS_SCHEMA_SQL,
237-
RETIRED_COMMITMENTS_INDEX_NAMES,
238235
ADDITIVE_RETIRED_COMMITMENTS_SCHEMA_COMPATIBILITY,
239236
)
240237
);

0 commit comments

Comments
 (0)