{"record":{"id":"85fa0a748d0449ee","repo":"nautechsystems/nautilus_trader","slug":"failed-to-inspect-legacy-execution-transactions","errorCode":null,"errorMessage":"Failed to inspect legacy execution transactions: {e}","messagePattern":"Failed to inspect legacy execution transactions: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/database.rs","lineNumber":3615,"sourceCode":"        if let Some(installed_version) = installed_version\n            && installed_version > EXECUTION_SCHEMA_VERSION\n        {\n            anyhow::bail!(\n                \"Execution schema version {} is newer than supported version {EXECUTION_SCHEMA_VERSION}\",\n                installed_version\n            );\n        }\n\n        let unresolved_legacy = sqlx::query_scalar::<_, i64>(\n            \"\n            SELECT COUNT(*)\n            FROM execution_transaction\n            WHERE status IN ('pending', 'included', 'reverted')\n            \",\n        )\n        .fetch_one(&mut *transaction)\n        .await\n        .map_err(|e| anyhow::anyhow!(\"Failed to inspect legacy execution transactions: {e}\"))?;\n        anyhow::ensure!(\n            unresolved_legacy == 0,\n            \"Cannot safely migrate {unresolved_legacy} unresolved execution schema version 1 transaction(s); resolve them with the prior version before enabling version {EXECUTION_SCHEMA_VERSION}\"\n        );\n\n        for statement in [\n            \"\n            CREATE OR REPLACE FUNCTION execution_transaction_v2_fence()\n            RETURNS TRIGGER AS $$\n            BEGIN\n                RAISE EXCEPTION 'Legacy execution writer refused after schema version 2 activation';\n            END;\n            $$ LANGUAGE plpgsql\n            \",\n            \"DROP TRIGGER IF EXISTS execution_transaction_v2_fence ON execution_transaction\",\n            \"\n            CREATE TRIGGER execution_transaction_v2_fence\n            BEFORE INSERT OR UPDATE OR DELETE ON execution_transaction","sourceCodeStart":3597,"sourceCodeEnd":3633,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/cache/database.rs#L3597-L3633","documentation":"This error wraps failure of the safety inspection query that counts unresolved legacy transactions (status IN ('pending','included','reverted')) before migrating the execution schema. The library must verify no unresolved version-1 transactions exist; failing to run this check aborts migration.","triggerScenarios":"Running ensure_execution_transaction_schema when the execution_transaction table or status column does not exist yet, the connection fails mid-transaction, or the aggregate query (fetch_one) errors due to permissions or type issues.","commonSituations":"Migrating a database where the legacy table was dropped or renamed; partially applied migrations leaving the table without expected columns; DB user lacking SELECT privileges.","solutions":["Confirm the execution_transaction table and status column exist before migration","Check the inner `{e}` for the exact Postgres error and address it (table, column, privileges)","If legacy rows are the concern, resolve pending/included/reverted transactions with the prior app version before migrating","Restore the expected version-1 schema from backups or prior migration scripts"],"exampleFix":"// before\n.map_err(|e| anyhow::anyhow!(\"Failed to inspect legacy execution transactions: {e}\"))?;\n// after\n.map_err(|e| {\n    tracing::error!(error = %e, \"legacy execution transaction inspection failed\");\n    anyhow::anyhow!(\"Failed to inspect legacy execution transactions: {e}\")\n})?;","handlingStrategy":"validation","validationCode":"let unresolved = sqlx::query_scalar::<_, i64>(\n    \"SELECT count(*) FROM execution_transaction WHERE status IN ('pending','included','reverted')\"\n).fetch_one(&pool).await?;\nif unresolved > 0 { anyhow::bail!(\"resolve {unresolved} legacy transactions before migrating\"); }","typeGuard":null,"tryCatchPattern":"match db.ensure_execution_transaction_schema().await {\n    Ok(()) => (),\n    Err(e) if e.to_string().contains(\"Failed to inspect legacy\") => {\n        // check table/column existence, then retry after repair\n        repair_legacy_table(&pool).await?;\n        db.ensure_execution_transaction_schema().await?;\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Drain pending/included/reverted legacy transactions before upgrading versions","Verify the legacy table and status column exist before migrating","Run migrations in a controlled window with no active writers"],"tags":["database","migration","postgres"],"backgroundTag":"database-query-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}