OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor: share workbench migration repair instructions (#1496)
mldangelo-oaie4f2f3daf644
diff --git a/plugins/codex-security/mcp-app/src/workbench/migrations.ts b/plugins/codex-security/mcp-app/src/workbench/migrations.tsindex c85cb85b3..bc917ec1f 100644--- a/plugins/codex-security/mcp-app/src/workbench/migrations.ts+++ b/plugins/codex-security/mcp-app/src/workbench/migrations.ts@@ -2,9 +2,12 @@ import { createHash } from "node:crypto";import { basename } from "node:path";import type { DatabaseSync } from "node:sqlite";import history from "../../../shared/workbench-migrations.json";+import repairPlan from "../../../shared/workbench-history-repairs.json";import { parseJson, stringifyJson } from "../helpers/json";import { transaction } from "./transaction";+const repairHistory = JSON.stringify(history);+export interface Migration {version: number;name: string;@@ -36,219 +39,19 @@ function repairAdditive(database: DatabaseSync, version: number): void {addColumn(database, statement);}-function moveMigration(- database: DatabaseSync,- from: number,- to: number,- name: string,-): void {- if (- !database- .prepare("SELECT 1 FROM schema_migrations WHERE version = ? AND name = ?")- .get(from, name)- )- return;- database- .prepare(- "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",- )- .run(to, from, name);-}--function normalizeExecutionProfiles(database: DatabaseSync): void {- const scanColumns = columns(database, "scans");- const workspaceColumns = columns(database, "workspaces");- const recorded = new Map(- database- .prepare(- "SELECT version, name FROM schema_migrations WHERE version IN (11, 12, 25)",- )- .all()- .map((row) => [Number(row.version), String(row.name)]),- );- const modelName = migration(25).name;- if (recorded.get(25) === "dynamic scan execution profiles") {- database- .prepare("UPDATE schema_migrations SET name = ? WHERE version = 25")- .run(modelName);- recorded.set(25, modelName);- }- const legacyNames = new Map([- [11, ["scan execution profiles"]],- [12, ["scan execution profiles", "dynamic scan execution profiles"]],- ]);- const legacyHistory = [...legacyNames].some(([version, names]) =>- names.includes(recorded.get(version) ?? ""),- );- const legacyColumns =- scanColumns.has("execution_model") ||- workspaceColumns.has("execution_model") ||- workspaceColumns.has("reasoning_effort");- if (!legacyHistory && !legacyColumns) return;- const supported = new Map([- [11, ["deep scan orchestration state", "scan execution profiles"]],- [- 12,- [- "scan continuation threads",- "scan execution profiles",- "dynamic scan execution profiles",- ],- ],- [25, [modelName]],- ]);- const invalidHistory = [...supported].some(- ([version, names]) =>- recorded.has(version) && !names.includes(recorded.get(version)!),- );- const validColumns = [scanColumns, workspaceColumns].every(- (names) =>- names.has("execution_model") &&- names.has("reasoning_effort") &&- !names.has("legacy_execution_model") &&- !names.has("legacy_reasoning_effort"),- );- if (- invalidHistory ||- (legacyColumns && !validColumns) ||- (legacyHistory && !legacyColumns)- ) {- throw new Error(- "The Codex Security database has an unsupported execution-profile migration history.",- );- }- for (const table of ["workspaces", "scans"]) {- database.exec(`ALTER TABLE ${table} RENAME COLUMN execution_model TO legacy_execution_model;- ALTER TABLE ${table} RENAME COLUMN reasoning_effort TO legacy_reasoning_effort;`);- }- repairAdditive(database, 25);- database.exec(`UPDATE scans SET model = COALESCE(model, legacy_execution_model),- reasoning_effort = COALESCE(reasoning_effort, legacy_reasoning_effort)`);- const remove = database.prepare(- "DELETE FROM schema_migrations WHERE version = ? AND name = ?",- );- for (const [version, names] of legacyNames)- for (const name of names) remove.run(version, name);- if (!recorded.has(25)) {- database- .prepare(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (25, ?, ?)",- )- .run(modelName, new Date().toISOString());- }-}-function normalizeHistory(database: DatabaseSync): void {- const mirror = database- .prepare(- "SELECT version, name FROM schema_migrations WHERE version BETWEEN 29 AND 32 ORDER BY version",- )- .all();- const mirrorNames = new Map([- [29, "freeze stopped scan source digests"],- [30, "separate deep scan publication failures"],- ]);- if (mirror.some((row) => mirrorNames.get(Number(row.version)) === row.name)) {- if (- mirror.length !== 2 ||- mirror.some((row) => mirrorNames.get(Number(row.version)) !== row.name)- ) {- throw new Error(- "The Codex Security database has an unsupported mirror migration history.",- );+ for (const query of repairPlan) {+ for (const { operation, value } of database+ .prepare(query.join("\n"))+ .all({ ":history": repairHistory })) {+ if (operation === "error") throw new Error(String(value));+ if (operation === "additive") repairAdditive(database, Number(value));+ else if (operation === "column") addColumn(database, String(value));+ else if (operation === "timestamp")+ database.prepare(String(value)).run(new Date().toISOString());+ else database.exec(String(value));}- for (const [from, to] of [- [30, 32],- [29, 31],- ])- moveMigration(database, from, to, mirrorNames.get(from)!);}- moveMigration(- database,- 33,- 40,- "index finding identity and comparison history",- );- moveMigration(database, 25, 26, "persist scan completion warnings");- moveMigration(database, 12, 20, "phase-specific scan progress");- normalizeExecutionProfiles(database);- moveMigration(database, 13, 21, "current scan preflight state");- const shadowed = new Map([- [18, ["scan target summaries"]],- [- 19,- [- "structured scan guidance context",- "idempotent scan lifecycle requests",- ],- ],- [- 20,- [- "retain superseded scan lifecycle requests",- "threat model publication receipts",- ],- ],- [- 21,- [- "scan progress projection and activity",- "deep coordinator manifest receipts",- ],- ],- [22, ["dynamic scan execution profiles"]],- ]);- const lookup = database.prepare(- "SELECT name FROM schema_migrations WHERE version = ?",- );- for (const [version, names] of shadowed) {- const name = lookup.get(version)?.name;- if (typeof name !== "string" || !names.includes(name)) continue;- if (version === 18) database.exec(migration(version).statements.join("\n"));- else if (version === 19) {- for (const sql of migration(version).statements)- database.exec(- sql.replace("CREATE TABLE ", "CREATE TABLE IF NOT EXISTS "),- );- } else repairAdditive(database, version);- database- .prepare("UPDATE schema_migrations SET name = ? WHERE version = ?")- .run(migration(version).name, version);- }- if (lookup.get(2)?.name !== "finding management schema") return;- const expected = new Map([- [2, "finding management schema"],- [3, "scan handoff delivery claims"],- [4, "finding remediation action claims"],- [5, "scan target snapshot digests"],- ]);- const legacy = database- .prepare(- "SELECT version, name FROM schema_migrations WHERE version BETWEEN 2 AND 5",- )- .all();- if (legacy.some((row) => expected.get(Number(row.version)) !== row.name)) {- throw new Error(- "The Codex Security database has an unsupported pre-release migration history.",- );- }- database.exec("DELETE FROM schema_migrations WHERE version = 5");- for (const [from, to] of [- [4, 5],- [3, 4],- [2, 3],- ])- moveMigration(database, from, to, expected.get(from)!);- repairAdditive(database, 2);- addColumn(- database,- "ALTER TABLE scans ADD COLUMN target_snapshot_digest TEXT;",- );- database- .prepare(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (2, ?, ?)",- )- .run(migration(2).name, new Date().toISOString());}function repairDeepScan(database: DatabaseSync): void {diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts b/plugins/codex-security/mcp-app/tests/test_workbench_database.tsindex ef780c2fe..8b04b5327 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts@@ -335,7 +335,7 @@ test("legacy execution profiles retain values while allowing independent model sdatabase.exec("UPDATE scans SET execution_model = 'synthetic-model', reasoning_effort = 'future-effort'",);- applyMigrations(database);+ applyMigrations(database, []);const row = database.prepare("SELECT model, reasoning_effort, legacy_execution_model, legacy_reasoning_effort FROM scans",@@ -351,6 +351,7 @@ test("legacy execution profiles retain values while allowing independent model s},);database.exec("UPDATE scans SET model = NULL, reasoning_effort = 'high'");+ applyMigrations(database);assertMigrationNames(database, version);}});diff --git a/plugins/codex-security/plugin-files.json b/plugins/codex-security/plugin-files.jsonindex f5a412f06..856202bb4 100644--- a/plugins/codex-security/plugin-files.json+++ b/plugins/codex-security/plugin-files.json@@ -103,6 +103,7 @@"scripts/workbench_target.py","scripts/workbench_target_state.py","scripts/workbench_validation.py",+ "shared/workbench-history-repairs.json","shared/workbench-migrations.json","skills/attack-path-analysis/SKILL.md","skills/attack-path-analysis/agents/openai.yaml",diff --git a/plugins/codex-security/scripts/workbench_schema.py b/plugins/codex-security/scripts/workbench_schema.pyindex c1b40dcc8..90271d5c1 100644--- a/plugins/codex-security/scripts/workbench_schema.py+++ b/plugins/codex-security/scripts/workbench_schema.py@@ -6,13 +6,13 @@ import sqlite3from collections.abc import Callablefrom pathlib import Path+_SHARED = Path(__file__).resolve().parents[1] / "shared"+_HISTORY = json.loads((_SHARED / "workbench-migrations.json").read_text(encoding="utf-8"))+_REPAIR_HISTORY = json.dumps(_HISTORY)+_REPAIR_PLAN = json.loads((_SHARED / "workbench-history-repairs.json").read_text(encoding="utf-8"))MIGRATIONS = tuple((migration["version"], migration["name"], "\n".join(migration["statements"]))- for migration in json.loads(- (Path(__file__).resolve().parents[1] / "shared" / "workbench-migrations.json").read_text(- encoding="utf-8"- )- )+ for migration in _HISTORY)@@ -98,232 +98,19 @@ def apply_migrations(backfill_security_targets(connection)-def normalize_pre_release_execution_profile_migrations(- connection: sqlite3.Connection, timestamp: str-) -> None:- scan_columns = {row["name"] for row in connection.execute("PRAGMA table_info(scans)")}- workspace_columns = {row["name"] for row in connection.execute("PRAGMA table_info(workspaces)")}- legacy_columns = {"execution_model", "reasoning_effort"}- renamed_columns = {- "legacy_execution_model",- "legacy_reasoning_effort",- }- execution_migrations = {- row["version"]: row["name"]- for row in connection.execute(- "SELECT version, name FROM schema_migrations WHERE version IN (11, 12, 25)"- )- }- supported_execution_migrations = {- 11: {"deep scan orchestration state", "scan execution profiles"},- 12: {- "scan continuation threads",- "scan execution profiles",- "dynamic scan execution profiles",- },- }- model_migration_name = "persist scan model settings"- if execution_migrations.get(25) == "dynamic scan execution profiles":- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 25 AND name = ?",- (model_migration_name, "dynamic scan execution profiles"),- )- execution_migrations[25] = model_migration_name- has_legacy_profile_history = any(- execution_migrations.get(version) in legacy_names- for version, legacy_names in (- (11, {"scan execution profiles"}),- (12, {"scan execution profiles", "dynamic scan execution profiles"}),- )- )- has_legacy_profile_columns = any(- column in columns- for column, columns in (- ("execution_model", scan_columns),- ("execution_model", workspace_columns),- ("reasoning_effort", workspace_columns),- )- )- if not (has_legacy_profile_history or has_legacy_profile_columns):- return-- if any(- execution_migrations.get(version) not in ({None} | supported_names)- for version, supported_names in supported_execution_migrations.items()- ):- raise SystemExit(- "The Codex Security database has an unsupported execution-profile migration history."- )-- if has_legacy_profile_columns and not (- legacy_columns <= scan_columns- and legacy_columns <= workspace_columns- and not renamed_columns.intersection(scan_columns | workspace_columns)- ):- raise SystemExit(- "The Codex Security database has an unsupported execution-profile migration history."- )- if has_legacy_profile_history and not has_legacy_profile_columns:- raise SystemExit(- "The Codex Security database has an unsupported execution-profile migration history."- )-- if execution_migrations.get(25) not in (None, model_migration_name):- raise SystemExit(- "The Codex Security database has an unsupported execution-profile migration history."- )-- # Keep the historical values and constraints for recovery while moving- # them out of the namespace used by the current independent scan settings.- for table in ("workspaces", "scans"):- connection.execute(- f"ALTER TABLE {table} RENAME COLUMN execution_model TO legacy_execution_model"- )- connection.execute(- f"ALTER TABLE {table} RENAME COLUMN reasoning_effort TO legacy_reasoning_effort"- )- repair_additive_migration(connection, 25)- connection.execute(- """- UPDATE scans- SET model = COALESCE(model, legacy_execution_model),- reasoning_effort = COALESCE(reasoning_effort, legacy_reasoning_effort)- """- )- for version, name in (- (11, "scan execution profiles"),- (12, "scan execution profiles"),- (12, "dynamic scan execution profiles"),- ):- connection.execute(- "DELETE FROM schema_migrations WHERE version = ? AND name = ?",- (version, name),- )- if execution_migrations.get(25) is None:- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",- (25, model_migration_name, timestamp),- )---def move_pre_release_migration(- connection: sqlite3.Connection, old_version: int, new_version: int, name: str-) -> None:- if (- connection.execute(- "SELECT 1 FROM schema_migrations WHERE version = ? AND name = ?", (old_version, name)- ).fetchone()- is None- ):- return- connection.execute(- "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",- (new_version, old_version, name),- )--def normalize_pre_release_migrations(connection: sqlite3.Connection, timestamp: str) -> None:- normalize_mirror_lineage_migrations(connection)- move_pre_release_migration(connection, 33, 40, "index finding identity and comparison history")-- move_pre_release_migration(connection, 25, 26, "persist scan completion warnings")- move_pre_release_migration(connection, 12, 20, "phase-specific scan progress")-- normalize_pre_release_execution_profile_migrations(connection, timestamp)-- move_pre_release_migration(connection, 13, 21, "current scan preflight state")-- for version, legacy_names in (- (18, {"scan target summaries"}),- (19, {"structured scan guidance context", "idempotent scan lifecycle requests"}),- (20, {"retain superseded scan lifecycle requests", "threat model publication receipts"}),- (21, {"scan progress projection and activity", "deep coordinator manifest receipts"}),- (22, {"dynamic scan execution profiles"}),- ):- migration = connection.execute(- "SELECT name FROM schema_migrations WHERE version = ?", (version,)- ).fetchone()- if migration is None or migration["name"] not in legacy_names:- continue- _, name, sql = next(migration for migration in MIGRATIONS if migration[0] == version)- if version == 18:- connection.execute(- "UPDATE scans SET handoff_claimed_at = NULL, handoff_claim_token = NULL "- "WHERE handoff_status = 'delivered'"- )- elif version == 19:- for statement in sql_statements(sql):- connection.execute(- statement.replace("CREATE TABLE ", "CREATE TABLE IF NOT EXISTS ", 1)- )- else:- repair_additive_migration(connection, version)- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = ? AND name = ?",- (name, version, migration["name"]),- )-- migration = connection.execute(- "SELECT name FROM schema_migrations WHERE version = 2"- ).fetchone()- if migration is None or migration["name"] != "finding management schema":- return-- legacy_versions = {- row["version"]: row["name"]- for row in connection.execute(- "SELECT version, name FROM schema_migrations WHERE version BETWEEN 2 AND 5"- )- }- expected = {- 2: "finding management schema",- 3: "scan handoff delivery claims",- 4: "finding remediation action claims",- 5: "scan target snapshot digests",- }- for version, name in legacy_versions.items():- if expected.get(version) != name:- raise SystemExit(- "The Codex Security database has an unsupported pre-release migration history."- )-- connection.execute(- "DELETE FROM schema_migrations WHERE version = 5 AND name = ?",- (expected[5],),- )- for old_version, new_version in ((4, 5), (3, 4), (2, 3)):- connection.execute(- "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",- (new_version, old_version, expected[old_version]),- )- repair_additive_migration(connection, 2)- add_column_if_missing(connection, "scans", "target_snapshot_digest", "TEXT")- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",- (2, "persist capability preflight summaries", timestamp),- )---def normalize_mirror_lineage_migrations(connection: sqlite3.Connection) -> None:- mirror_names = {- 29: "freeze stopped scan source digests",- 30: "separate deep scan publication failures",- }- migrations = {- row["version"]: row["name"]- for row in connection.execute(- "SELECT version, name FROM schema_migrations WHERE version BETWEEN 29 AND 32"- )- }- if not any(migrations.get(version) == name for version, name in mirror_names.items()):- return- if migrations != mirror_names:- raise SystemExit("The Codex Security database has an unsupported mirror migration history.")- for old_version, new_version in ((30, 32), (29, 31)):- connection.execute(- "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",- (new_version, old_version, mirror_names[old_version]),- )+ for query in _REPAIR_PLAN:+ actions = connection.execute("\n".join(query), {"history": _REPAIR_HISTORY}).fetchall()+ for action in actions:+ operation, value = action["operation"], action["value"]+ if operation == "error":+ raise SystemExit(value)+ if operation == "additive":+ repair_additive_migration(connection, value)+ elif operation == "column":+ add_migration_column(connection, value)+ else:+ connection.execute(value, (timestamp,) if operation == "timestamp" else ())def repair_deep_scan_migration(connection: sqlite3.Connection) -> None:diff --git a/plugins/codex-security/shared/workbench-history-repairs.json b/plugins/codex-security/shared/workbench-history-repairs.jsonnew file mode 100644index 000000000..951814db6--- /dev/null+++ b/plugins/codex-security/shared/workbench-history-repairs.json@@ -0,0 +1,218 @@+[+ [+ "-- mirror and early-moves: relocate numbers before checking profiles.",+ "WITH",+ "-- Keep the common :history binding even though these moves do not inspect it.",+ "input AS (SELECT :history AS history),",+ "mirror_names(version, name, destination) AS (",+ " VALUES",+ " (29, 'freeze stopped scan source digests', 31),",+ " (30, 'separate deep scan publication failures', 32)",+ "),",+ "mirror AS (",+ " SELECT EXISTS (",+ " SELECT 1 FROM schema_migrations JOIN mirror_names USING (version, name)",+ " ) AS present,",+ " (SELECT COUNT(*) FROM schema_migrations WHERE version BETWEEN 29 AND 32) = 2",+ " AND (SELECT COUNT(*) FROM schema_migrations",+ " JOIN mirror_names USING (version, name)) = 2 AS valid",+ "),",+ "moves(position, version, destination, name) AS (",+ " VALUES",+ " (0, 33, 40, 'index finding identity and comparison history'),",+ " (1, 25, 26, 'persist scan completion warnings'),",+ " (2, 12, 20, 'phase-specific scan progress')",+ ")",+ "SELECT 0 AS position, 'error' AS operation,",+ " 'The Codex Security database has an unsupported mirror migration history.' AS value",+ " FROM mirror WHERE present AND NOT valid",+ "UNION ALL",+ "SELECT 32 - version, 'sql',",+ " 'UPDATE schema_migrations SET version = ' || destination ||",+ " ' WHERE version = ' || version || ' AND name = ' || quote(name)",+ " FROM mirror_names, mirror WHERE present",+ "UNION ALL",+ "-- early-moves",+ "SELECT 10 + position, 'sql', 'UPDATE schema_migrations SET version = ' || destination ||",+ " ' WHERE version = ' || version || ' AND name = ' || quote(name)",+ " FROM moves JOIN schema_migrations USING (version, name)",+ "ORDER BY position;"+ ],+ [+ "-- profiles: preserve legacy columns before the later repairs.",+ "WITH",+ "history AS (",+ " SELECT json_extract(value, '$.version') AS version,",+ " json_extract(value, '$.name') AS name",+ " FROM json_each(:history)",+ "),",+ "profile_columns AS (",+ " SELECT 'scans' AS table_name, name FROM pragma_table_info('scans')",+ " UNION ALL",+ " SELECT 'workspaces', name FROM pragma_table_info('workspaces')",+ "),",+ "profile_state AS (",+ " SELECT",+ " EXISTS (",+ " SELECT 1 FROM schema_migrations",+ " WHERE (version = 11 AND name = 'scan execution profiles')",+ " OR (version = 12 AND name IN (",+ " 'scan execution profiles', 'dynamic scan execution profiles'",+ " ))",+ " ) AS legacy_history,",+ " EXISTS (",+ " SELECT 1 FROM profile_columns",+ " WHERE name = 'execution_model'",+ " OR (table_name = 'workspaces' AND name = 'reasoning_effort')",+ " ) AS legacy_columns,",+ " (SELECT COUNT(*) FROM profile_columns",+ " WHERE name IN ('execution_model', 'reasoning_effort')) = 4",+ " AND NOT EXISTS (",+ " SELECT 1 FROM profile_columns",+ " WHERE name IN ('legacy_execution_model', 'legacy_reasoning_effort')",+ " ) AS valid_columns,",+ " NOT EXISTS (",+ " SELECT 1 FROM schema_migrations",+ " WHERE (version = 11 AND name NOT IN (",+ " 'deep scan orchestration state', 'scan execution profiles'",+ " )) OR (version = 12 AND name NOT IN (",+ " 'scan continuation threads', 'scan execution profiles',",+ " 'dynamic scan execution profiles'",+ " )) OR (version = 25 AND name NOT IN (",+ " 'dynamic scan execution profiles',",+ " (SELECT name FROM history WHERE version = 25)",+ " ))",+ " ) AS valid_history",+ ")",+ "SELECT 0 AS position, 'sql' AS operation,",+ " 'UPDATE schema_migrations SET name = ' ||",+ " quote((SELECT name FROM history WHERE version = 25)) || ' WHERE version = 25' AS value",+ " WHERE EXISTS (SELECT 1 FROM schema_migrations",+ " WHERE version = 25 AND name = 'dynamic scan execution profiles')",+ "UNION ALL",+ "SELECT 1, 'error',",+ " 'The Codex Security database has an unsupported execution-profile migration history.'",+ " FROM profile_state",+ " WHERE (legacy_history OR legacy_columns) AND (",+ " NOT valid_history OR (legacy_columns AND NOT valid_columns)",+ " OR (legacy_history AND NOT legacy_columns)",+ " )",+ "UNION ALL",+ "-- Keep historical values and constraints for recovery while moving",+ "-- them out of the namespace used by independent scan settings.",+ "SELECT 2 + CASE table_name WHEN 'workspaces' THEN 0 ELSE 2 END",+ " + CASE name WHEN 'execution_model' THEN 0 ELSE 1 END,",+ " 'sql', 'ALTER TABLE ' || table_name || ' RENAME COLUMN ' || name ||",+ " ' TO legacy_' || name",+ " FROM profile_columns, profile_state",+ " WHERE (legacy_history OR legacy_columns)",+ " AND name IN ('execution_model', 'reasoning_effort')",+ "UNION ALL",+ "SELECT 6, 'additive', 25 FROM profile_state",+ " WHERE legacy_history OR legacy_columns",+ "UNION ALL",+ "SELECT 7, 'sql',",+ " 'UPDATE scans SET model = COALESCE(model, legacy_execution_model),",+ " reasoning_effort = COALESCE(reasoning_effort, legacy_reasoning_effort)'",+ " FROM profile_state WHERE legacy_history OR legacy_columns",+ "UNION ALL",+ "SELECT 8, 'sql',",+ " 'DELETE FROM schema_migrations WHERE (version = 11 AND name = ''scan execution profiles'')",+ " OR (version = 12 AND name IN (''scan execution profiles'', ''dynamic scan execution profiles''))'",+ " FROM profile_state WHERE legacy_history OR legacy_columns",+ "UNION ALL",+ "SELECT 9, 'timestamp',",+ " 'INSERT INTO schema_migrations (version, name, applied_at) VALUES (25, ' ||",+ " quote((SELECT name FROM history WHERE version = 25)) || ', ?)'",+ " FROM profile_state WHERE (legacy_history OR legacy_columns)",+ " AND NOT EXISTS (SELECT 1 FROM schema_migrations WHERE version = 25)",+ "ORDER BY position;"+ ],+ [+ "-- late-moves, shadowed, and legacy: independent remaining histories.",+ "WITH",+ "history AS (",+ " SELECT json_extract(value, '$.version') AS version,",+ " json_extract(value, '$.name') AS name,",+ " json_extract(value, '$.statements') AS statements",+ " FROM json_each(:history)",+ "),",+ "shadow_names(version, name) AS (",+ " VALUES",+ " (18, 'scan target summaries'),",+ " (19, 'structured scan guidance context'),",+ " (19, 'idempotent scan lifecycle requests'),",+ " (20, 'retain superseded scan lifecycle requests'),",+ " (20, 'threat model publication receipts'),",+ " (21, 'scan progress projection and activity'),",+ " (21, 'deep coordinator manifest receipts'),",+ " (22, 'dynamic scan execution profiles')",+ "),",+ "shadowed AS (",+ " SELECT history.* FROM schema_migrations",+ " JOIN shadow_names USING (version, name) JOIN history USING (version)",+ "),",+ "legacy_names(version, name) AS (",+ " VALUES",+ " (2, 'finding management schema'),",+ " (3, 'scan handoff delivery claims'),",+ " (4, 'finding remediation action claims'),",+ " (5, 'scan target snapshot digests')",+ "),",+ "legacy AS (",+ " SELECT EXISTS (",+ " SELECT 1 FROM schema_migrations",+ " WHERE version = 2 AND name = 'finding management schema'",+ " ) AS present,",+ " NOT EXISTS (",+ " SELECT 1 FROM schema_migrations AS recorded",+ " LEFT JOIN legacy_names USING (version, name)",+ " WHERE recorded.version BETWEEN 2 AND 5 AND legacy_names.name IS NULL",+ " ) AS valid",+ ")",+ "SELECT 0 AS position, 'sql' AS operation,",+ " 'UPDATE schema_migrations SET version = 21 WHERE version = 13 AND name = ''current scan preflight state''' AS value",+ "WHERE EXISTS (SELECT 1 FROM schema_migrations",+ " WHERE version = 13 AND name = 'current scan preflight state')",+ "UNION ALL",+ "-- shadowed",+ "SELECT version * 10 + statements.key, 'sql',",+ " CASE version WHEN 19 THEN replace(statements.value,",+ " 'CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ')",+ " ELSE statements.value END",+ " FROM shadowed, json_each(shadowed.statements) AS statements",+ " WHERE version IN (18, 19)",+ "UNION ALL",+ "SELECT version * 10, 'additive', version",+ " FROM shadowed WHERE version IN (20, 21, 22)",+ "UNION ALL",+ "SELECT version * 10 + 9, 'sql',",+ " 'UPDATE schema_migrations SET name = ' || quote(name) ||",+ " ' WHERE version = ' || version",+ " FROM shadowed",+ "UNION ALL",+ "-- legacy",+ "SELECT 300, 'error',",+ " 'The Codex Security database has an unsupported pre-release migration history.'",+ " FROM legacy WHERE present AND NOT valid",+ "UNION ALL",+ "SELECT 301, 'sql', 'DELETE FROM schema_migrations WHERE version = 5'",+ " FROM legacy WHERE present",+ "UNION ALL",+ "SELECT 306 - version, 'sql',",+ " 'UPDATE schema_migrations SET version = ' || (version + 1) ||",+ " ' WHERE version = ' || version || ' AND name = ' || quote(name)",+ " FROM legacy_names, legacy WHERE present AND version < 5",+ "UNION ALL",+ "SELECT 305, 'additive', 2 FROM legacy WHERE present",+ "UNION ALL",+ "SELECT 306, 'column', 'ALTER TABLE scans ADD COLUMN target_snapshot_digest TEXT;'",+ " FROM legacy WHERE present",+ "UNION ALL",+ "SELECT 307, 'timestamp',",+ " 'INSERT INTO schema_migrations (version, name, applied_at) VALUES (2, ' ||",+ " quote((SELECT name FROM history WHERE version = 2)) || ', ?)'",+ " FROM legacy WHERE present",+ "ORDER BY position;"+ ]+]