use sqlx::{PgConnection, Row, query}; use super::authority::MigrationError; mod required_tables; pub(super) use required_tables::OWNED_RELATIONS; use required_tables::{REQUIRED_COLUMN_TYPES, REQUIRED_COLUMNS}; pub(super) async fn relation_exists( connection: &mut PgConnection, relation: &str, ) -> Result { query("select to_regclass(format('%I.%I', current_schema(), $1))::text is not null as present") .bind(relation) .fetch_one(connection) .await .map_err(|_| MigrationError::storage("preflight.inventory"))? .try_get::("present") .map_err(|_| MigrationError::storage("preflight.inventory")) } pub(super) async fn validate_required_relations( connection: &mut PgConnection, relations: &[&str], version: i64, ) -> Result<(), MigrationError> { for relation in relations { if !relation_exists(connection, relation).await? { return Err(schema_error(version)); } let kind = query( "select c.relkind::text as kind from pg_catalog.pg_class c join pg_catalog.pg_namespace n on n.oid = c.relnamespace where n.nspname = current_schema() and c.relname = $1", ) .bind(relation) .fetch_one(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .try_get::("kind") .map_err(|_| MigrationError::storage("preflight.schema"))?; if !matches!(kind.as_str(), "r" | "p") { return Err(schema_error(version)); } } Ok(()) } pub(super) async fn validate_schema_fingerprint( connection: &mut PgConnection, current_version: i64, ) -> Result<(), MigrationError> { for (table, required) in REQUIRED_COLUMNS { if !relation_exists(connection, table).await? { continue; } let rows = query( "select column_name from information_schema.columns where table_schema = current_schema() and table_name = $1 order by ordinal_position limit 257", ) .bind(table) .fetch_all(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))?; let actual = rows .iter() .filter_map(|row| row.try_get::("column_name").ok()) .collect::>(); let required = required .iter() .copied() .filter(|column| { !(current_version == 1 && *table == "__crank_ext_migrations" && *column == "checksum") }) .collect::>(); if actual.len() != required.len() || required .iter() .any(|column| !actual.iter().any(|actual| actual == column)) { return Err(schema_error(current_version)); } } for (table, column, data_type, nullable) in REQUIRED_COLUMN_TYPES { if !relation_exists(connection, table).await? { continue; } if current_version == 1 && *table == "__crank_ext_migrations" && *column == "checksum" { continue; } let row = query( "select data_type, is_nullable from information_schema.columns where table_schema = current_schema() and table_name = $1 and column_name = $2", ) .bind(table) .bind(column) .fetch_optional(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))?; let valid = row.is_some_and(|row| { row.try_get::("data_type").ok().as_deref() == Some(*data_type) && row.try_get::("is_nullable").ok().as_deref() == Some(if *nullable { "YES" } else { "NO" }) }); if !valid { return Err(schema_error(current_version)); } } if current_version < 3 { let trace_column_present = query( "select exists ( select 1 from information_schema.columns where table_schema = current_schema() and table_name = 'invocation_logs' and column_name = 'trace_id' ) as present", ) .fetch_one(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .try_get::("present") .map_err(|_| MigrationError::storage("preflight.schema"))?; let trace_constraint_present = named_constraint_exists( connection, "invocation_logs", "invocation_logs_trace_id_format_check", ) .await?; let v3_index_present = relation_exists(connection, "invocation_logs_workspace_request_id_idx").await? || relation_exists(connection, "invocation_logs_workspace_trace_id_idx").await?; if trace_column_present || trace_constraint_present || v3_index_present { return Err(schema_error(current_version)); } } else { let trace_id = query( "select data_type, is_nullable from information_schema.columns where table_schema = current_schema() and table_name = 'invocation_logs' and column_name = 'trace_id'", ) .fetch_optional(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))?; let valid = trace_id.is_some_and(|row| { row.try_get::("data_type").ok().as_deref() == Some("text") && row.try_get::("is_nullable").ok().as_deref() == Some("YES") }); if !valid { return Err(schema_error(current_version)); } let constraint = query( "select pg_get_expr(c.conbin, c.conrelid) as expression, c.convalidated from pg_catalog.pg_constraint c join pg_catalog.pg_class t on t.oid = c.conrelid join pg_catalog.pg_namespace n on n.oid = t.relnamespace where n.nspname = current_schema() and t.relname = 'invocation_logs' and c.conname = 'invocation_logs_trace_id_format_check' and c.contype = 'c'", ) .fetch_optional(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))?; let constraint_valid = constraint.is_some_and(|row| { row.try_get::("expression") .ok() .is_some_and(|value| { normalize_definition(&value) == "trace_idisnullortrace_id~'^[0-9a-f]{32}$'::textandtrace_id<>'00000000000000000000000000000000'::text" }) && row.try_get::("convalidated").ok() == Some(false) }); if !constraint_valid { return Err(schema_error(current_version)); } } if relation_exists(connection, "mcp_transport_sessions").await? { let required_indexes = [ "mcp_transport_sessions_workspace_agent_idx", "mcp_transport_sessions_expires_at_idx", ]; for index in required_indexes { let present = query( "select exists (select 1 from pg_catalog.pg_indexes where schemaname = current_schema() and indexname = $1) as present", ) .bind(index) .fetch_one(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .try_get::("present") .map_err(|_| MigrationError::storage("preflight.schema"))?; if !present { return Err(schema_error(current_version)); } } } if current_version >= 3 { validate_index( connection, "invocation_logs_workspace_request_id_idx", "request_id", "request_idisnotnullandoctet_lengthrequest_id<=128", ) .await?; validate_index( connection, "invocation_logs_workspace_trace_id_idx", "trace_id", "trace_idisnotnull", ) .await?; } let lifecycle_columns = [ "name", "display_name", "category", "protocol", "security_level", "snapshot_provenance", "snapshot_observed_at", "published_at", "published_by", ]; for column in lifecycle_columns { let present = query( "select exists ( select 1 from information_schema.columns where table_schema = current_schema() and table_name = 'operation_versions' and column_name = $1 ) as present", ) .bind(column) .fetch_one(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .try_get::("present") .map_err(|_| MigrationError::storage("preflight.schema"))?; if present != (current_version >= 4) { return Err(schema_error(current_version)); } } if current_version >= 4 { for (table, trigger) in [ ("operation_versions", "operation_versions_immutable_guard"), ( "published_operations", "published_operations_monotonic_guard", ), ("operations", "operations_latest_pointer_monotonic_guard"), ] { let trigger_present = query( "select exists ( select 1 from pg_catalog.pg_trigger tg join pg_catalog.pg_class t on t.oid = tg.tgrelid join pg_catalog.pg_namespace n on n.oid = t.relnamespace where n.nspname = current_schema() and t.relname = $1 and tg.tgname = $2 and not tg.tgisinternal ) as present", ) .bind(table) .bind(trigger) .fetch_one(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .try_get::("present") .map_err(|_| MigrationError::storage("preflight.schema"))?; if !trigger_present { return Err(schema_error(current_version)); } } } let outcome_columns = [ "operation_version", "execution_stage", "execution_error_code", "retryability", "outcome_certainty", ]; for column in outcome_columns { let row = query( "select data_type, is_nullable from information_schema.columns where table_schema = current_schema() and table_name = 'invocation_logs' and column_name = $1", ) .bind(column) .fetch_optional(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))?; if current_version < 5 { if row.is_some() { return Err(schema_error(current_version)); } } else { let expected_type = if column == "operation_version" { "integer" } else { "text" }; let valid = row.is_some_and(|row| { row.try_get::("data_type").ok().as_deref() == Some(expected_type) && row.try_get::("is_nullable").ok().as_deref() == Some("YES") }); if !valid { return Err(schema_error(current_version)); } } } if current_version >= 5 { validate_v5_execution_contract(connection).await?; } if current_version >= 6 { validate_v6_platform_key_name_reuse(connection).await?; } if current_version < 7 { let v7_relations_present = relation_exists(connection, "master_key_identities").await? || relation_exists(connection, "master_key_rotations").await?; let mut v7_secret_columns_present = false; for column in [ "master_key_epoch", "target_ciphertext", "target_key_version", "target_master_key_epoch", ] { v7_secret_columns_present |= column_exists(connection, "secret_versions", column).await?; } if v7_relations_present || v7_secret_columns_present { return Err(schema_error(current_version)); } } else { super::schema_guard_v7::validate_v7_master_key_identity(connection).await?; } if current_version < 8 { let v8_relations_present = relation_exists(connection, "admin_bootstrap_contracts").await? || relation_exists(connection, "admin_login_backoff").await? || relation_exists(connection, "admin_security_audit_events").await?; let v8_session_columns_present = column_exists(connection, "user_sessions", "csrf_hash") .await? || column_exists(connection, "user_sessions", "revoked_at").await?; if v8_relations_present || v8_session_columns_present { return Err(schema_error(current_version)); } } else { super::schema_guard_v8::validate_v8_admin_auth_lifecycle(connection).await?; } if current_version < 9 { if !super::schema_guard_v9::validate_v9_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v9::validate_v9_agent_catalog_lifecycle(connection).await?; } if current_version < 10 { if !super::schema_guard_v10::validate_v10_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v10::validate_v10_approval_side_effects(connection).await?; } if current_version < 11 { if !super::schema_guard_v11::validate_v11_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v11::validate_v11_onboarding_product_events(connection).await?; } if current_version < 12 { if !super::schema_guard_v12::validate_v12_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v12::validate_v12_artifact_metadata(connection).await?; } if current_version < 13 { if !super::schema_guard_v13::validate_v13_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v13::validate_v13_artifact_cleanup_indexes(connection).await?; } if current_version < 14 { if !super::schema_guard_v14::validate_v14_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v14::validate_v14_import_plan(connection).await?; } if current_version < 15 { if !super::schema_guard_v15::validate_v15_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v15::validate_v15_onboarding_import_selection(connection).await?; } if current_version < 16 { if !super::schema_guard_v16::validate_v16_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v16::validate_v16_resource_drafts(connection, current_version).await?; } if current_version < 17 { if !super::schema_guard_v17::validate_v17_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v17::validate_v17_resource_publications(connection, current_version) .await?; } if current_version < 18 { if !super::schema_guard_v18::validate_v18_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v18::validate_v18_agent_resource_bindings(connection).await?; } if current_version < 19 { if !super::schema_guard_v19::validate_v19_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v19::validate_v19_resource_security_revocations(connection).await?; } if current_version < 20 { if !super::schema_guard_v20::validate_v20_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v20::validate_v20_resource_template_sources(connection).await?; } if current_version < 21 { for relation in [ "prompts", "prompt_versions", "prompt_arguments", "prompt_messages", ] { if relation_exists(connection, relation).await? { return Err(schema_error(current_version)); } } } else { validate_required_relations(connection, super::owned_relations::PROMPTS, current_version) .await?; } if current_version < 22 { if !super::schema_guard_v22::validate_v22_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v22::validate_v22_prompt_publications(connection).await?; } if current_version < 23 { if !super::schema_guard_v23::validate_v23_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v23::validate_v23_prompt_dependencies(connection).await?; } if current_version < 24 { if !super::schema_guard_v24::validate_v24_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v24::validate_v24_execution_admission(connection, current_version) .await?; } if current_version < 25 { if !super::schema_guard_v25::validate_v25_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v26::validate_v26_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v27::validate_v27_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v28::validate_v28_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v29::validate_v29_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v30::validate_v30_absent(connection).await? { return Err(schema_error(current_version)); } } else if current_version < 26 { super::schema_guard_v25::validate_v25_durable_tasks(connection).await?; if !super::schema_guard_v26::validate_v26_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v28::validate_v28_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v29::validate_v29_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v30::validate_v30_absent(connection).await? { return Err(schema_error(current_version)); } } else if current_version < 27 { super::schema_guard_v26::validate_v26_durable_tasks(connection).await?; if !super::schema_guard_v27::validate_v27_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v28::validate_v28_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v29::validate_v29_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v30::validate_v30_absent(connection).await? { return Err(schema_error(current_version)); } } else if current_version < 28 { super::schema_guard_v26::validate_v26_base_columns(connection).await?; super::schema_guard_v27::validate_v27_durable_tasks(connection).await?; if !super::schema_guard_v28::validate_v28_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v29::validate_v29_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v30::validate_v30_absent(connection).await? { return Err(schema_error(current_version)); } } else if current_version < 29 { super::schema_guard_v26::validate_v26_base_columns(connection).await?; super::schema_guard_v28::validate_v28_durable_tasks(connection).await?; if !super::schema_guard_v29::validate_v29_absent(connection).await? { return Err(schema_error(current_version)); } if !super::schema_guard_v30::validate_v30_absent(connection).await? { return Err(schema_error(current_version)); } } else if current_version < 30 { super::schema_guard_v26::validate_v26_base_columns(connection).await?; super::schema_guard_v29::validate_v29_durable_attempts(connection).await?; if !super::schema_guard_v30::validate_v30_absent(connection).await? { return Err(schema_error(current_version)); } } else { super::schema_guard_v26::validate_v26_base_columns(connection).await?; super::schema_guard_v30::validate_v30_input_continuation(connection).await?; } Ok(()) } pub(super) async fn column_exists( connection: &mut PgConnection, table: &str, column: &str, ) -> Result { query( "select exists ( select 1 from information_schema.columns where table_schema = current_schema() and table_name = $1 and column_name = $2 ) as present", ) .bind(table) .bind(column) .fetch_one(connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .try_get::("present") .map_err(|_| MigrationError::storage("preflight.schema")) } async fn validate_v5_execution_contract( connection: &mut PgConnection, ) -> Result<(), MigrationError> { let operation_version = constraint_expression( connection, "invocation_logs", "invocation_logs_operation_version_check", ) .await?; if normalize_definition(&operation_version) != "operation_versionisnulloroperation_version>0" { return Err(schema_error(5)); } for (constraint, column, allowed) in [ ( "invocation_logs_execution_stage_check", "execution_stage", &[ "authorization", "input_schema", "input_mapping", "request_preparation", "admission", "adapter", "upstream", "output_mapping", "output_schema", "mandatory_persistence", "runtime", ][..], ), ( "invocation_logs_execution_error_code_check", "execution_error_code", &[ "authorization_denied", "auth_profile_not_found", "secret_not_found", "secret_invalid", "input_schema_invalid", "input_mapping_invalid", "prepared_request_invalid", "execution_overloaded", "safety_store_unavailable", "protocol_unsupported", "execution_mode_unsupported", "adapter_configuration_invalid", "outbound_target_rejected", "upstream_auth_error", "upstream_not_found", "upstream_rate_limited", "upstream_server_error", "upstream_status_error", "upstream_timeout", "upstream_transport_error", "upstream_request_too_large", "upstream_response_too_large", "output_mapping_invalid", "output_schema_invalid", "persistence_unavailable", "runtime_internal", "confirmation_required", "confirmation_invalid", "idempotency_in_progress", "idempotency_conflict", "idempotency_outcome_unknown", ][..], ), ( "invocation_logs_retryability_check", "retryability", &[ "never", "safe", "after_delay", "manual_reconcile", "requires_confirmation", ][..], ), ( "invocation_logs_outcome_certainty_check", "outcome_certainty", &["certain", "outcome_unknown"][..], ), ] { let expression = constraint_expression(connection, "invocation_logs", constraint).await?; if !enum_constraint_matches(&expression, column, allowed) { return Err(schema_error(5)); } } validate_v5_index(connection).await } pub(super) async fn constraint_expression( connection: &mut PgConnection, table: &str, constraint: &str, ) -> Result { query( "select pg_get_expr(c.conbin, c.conrelid) as expression from pg_catalog.pg_constraint c join pg_catalog.pg_class t on t.oid = c.conrelid join pg_catalog.pg_namespace n on n.oid = t.relnamespace where n.nspname = current_schema() and t.relname = $1 and c.conname = $2 and c.contype = 'c'", ) .bind(table) .bind(constraint) .fetch_optional(connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .and_then(|row| row.try_get::("expression").ok()) .ok_or_else(|| schema_error(5)) } pub(super) fn enum_constraint_matches(expression: &str, column: &str, allowed: &[&str]) -> bool { let normalized = normalize_definition(expression); if !normalized.contains(column) || normalized.contains("ortrue") { return false; } let mut values = expression .split('\'') .enumerate() .filter_map(|(index, value)| (index % 2 == 1).then_some(value)) .collect::>(); values.sort_unstable(); values.dedup(); let mut expected = allowed.to_vec(); expected.sort_unstable(); values == expected } async fn validate_v5_index(connection: &mut PgConnection) -> Result<(), MigrationError> { let row = query( "select t.relname as table_name, am.amname as access_method, i.indisvalid, i.indisready, i.indisunique, pg_get_indexdef(i.indexrelid, 1, true) as first_column, pg_get_indexdef(i.indexrelid, 2, true) as second_column, pg_get_indexdef(i.indexrelid, 3, true) as third_column, pg_get_expr(i.indpred, i.indrelid) as predicate from pg_catalog.pg_index i join pg_catalog.pg_class idx on idx.oid = i.indexrelid join pg_catalog.pg_class t on t.oid = i.indrelid join pg_catalog.pg_namespace n on n.oid = t.relnamespace join pg_catalog.pg_am am on am.oid = idx.relam where n.nspname = current_schema() and idx.relname = 'invocation_logs_workspace_operation_version_idx'", ) .fetch_optional(connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))?; let valid = row.is_some_and(|row| { row.try_get::("table_name").ok().as_deref() == Some("invocation_logs") && row.try_get::("access_method").ok().as_deref() == Some("btree") && row.try_get::("indisvalid").ok() == Some(true) && row.try_get::("indisready").ok() == Some(true) && row.try_get::("indisunique").ok() == Some(false) && row.try_get::("first_column").ok().as_deref() == Some("workspace_id") && row.try_get::("second_column").ok().as_deref() == Some("operation_id") && row.try_get::("third_column").ok().as_deref() == Some("operation_version") && row .try_get::("predicate") .ok() .is_some_and(|value| normalize_definition(&value) == "operation_versionisnotnull") }); if valid { Ok(()) } else { Err(schema_error(5)) } } async fn validate_v6_platform_key_name_reuse( connection: &mut PgConnection, ) -> Result<(), MigrationError> { if relation_exists(connection, "platform_api_keys_workspace_name_idx").await? { return Err(schema_error(6)); } let row = query( "select t.relname as table_name, am.amname as access_method, i.indisvalid, i.indisready, i.indisunique, pg_get_indexdef(i.indexrelid, 1, true) as first_column, pg_get_indexdef(i.indexrelid, 2, true) as second_column, pg_get_expr(i.indpred, i.indrelid) as predicate from pg_catalog.pg_index i join pg_catalog.pg_class idx on idx.oid = i.indexrelid join pg_catalog.pg_class t on t.oid = i.indrelid join pg_catalog.pg_namespace n on n.oid = t.relnamespace join pg_catalog.pg_am am on am.oid = idx.relam where n.nspname = current_schema() and idx.relname = 'platform_api_keys_workspace_name_active_idx'", ) .fetch_optional(connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))?; let valid = row.is_some_and(|row| { row.try_get::("table_name").ok().as_deref() == Some("platform_api_keys") && row.try_get::("access_method").ok().as_deref() == Some("btree") && row.try_get::("indisvalid").ok() == Some(true) && row.try_get::("indisready").ok() == Some(true) && row.try_get::("indisunique").ok() == Some(true) && row.try_get::("first_column").ok().as_deref() == Some("workspace_id") && row.try_get::("second_column").ok().as_deref() == Some("name") && row .try_get::("predicate") .ok() .is_some_and(|value| { matches!( normalize_definition(&value).as_str(), "status<>'deleted'::text" | "status!='deleted'::text" ) }) }); if valid { Ok(()) } else { Err(schema_error(6)) } } async fn named_constraint_exists( connection: &mut PgConnection, table: &str, constraint: &str, ) -> Result { query( "select exists ( select 1 from pg_catalog.pg_constraint c join pg_catalog.pg_class t on t.oid = c.conrelid join pg_catalog.pg_namespace n on n.oid = t.relnamespace where n.nspname = current_schema() and t.relname = $1 and c.conname = $2 ) as present", ) .bind(table) .bind(constraint) .fetch_one(connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .try_get::("present") .map_err(|_| MigrationError::storage("preflight.schema")) } async fn validate_index( connection: &mut PgConnection, index: &str, second_column: &str, expected_predicate: &str, ) -> Result<(), MigrationError> { let row = query( "select t.relname as table_name, am.amname as access_method, i.indisvalid, i.indisready, i.indisunique, pg_get_indexdef(i.indexrelid, 1, true) as first_column, pg_get_indexdef(i.indexrelid, 2, true) as second_column, pg_get_expr(i.indpred, i.indrelid) as predicate from pg_catalog.pg_index i join pg_catalog.pg_class idx on idx.oid = i.indexrelid join pg_catalog.pg_class t on t.oid = i.indrelid join pg_catalog.pg_namespace n on n.oid = t.relnamespace join pg_catalog.pg_am am on am.oid = idx.relam where n.nspname = current_schema() and idx.relname = $1", ) .bind(index) .fetch_optional(connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))?; let valid = row.is_some_and(|row| { row.try_get::("table_name").ok().as_deref() == Some("invocation_logs") && row.try_get::("access_method").ok().as_deref() == Some("btree") && row.try_get::("indisvalid").ok() == Some(true) && row.try_get::("indisready").ok() == Some(true) && row.try_get::("indisunique").ok() == Some(false) && row.try_get::("first_column").ok().as_deref() == Some("workspace_id") && row.try_get::("second_column").ok().as_deref() == Some(second_column) && row .try_get::("predicate") .ok() .is_some_and(|value| normalize_definition(&value) == expected_predicate) }); if valid { Ok(()) } else { Err(schema_error(3)) } } pub(super) fn normalize_definition(value: &str) -> String { value .chars() .filter(|character| !character.is_ascii_whitespace() && !matches!(character, '(' | ')')) .flat_map(char::to_lowercase) .collect() } pub(super) fn schema_error(version: i64) -> MigrationError { MigrationError::new( "partial_sequence", "preflight.schema", Some(version), "restore_known_good_backup", ) }