//! V29 schema guard: durable per-outbound attempt history, pinned retry policy and cancellation //! (Story 4.4). use sqlx::{PgConnection, Row, query}; use super::authority::MigrationError; use super::schema_guard::{column_exists, relation_exists}; const TASKS: &str = "tasks"; const ATTEMPTS: &str = "task_attempts"; pub(super) async fn validate_v29_absent( connection: &mut PgConnection, ) -> Result { if relation_exists(connection, TASKS).await? { if column_exists(connection, TASKS, "max_attempts").await? { return Ok(false); } if relation_exists(connection, ATTEMPTS).await? { return Ok(false); } } Ok(true) } pub(super) async fn validate_v29_durable_attempts( connection: &mut PgConnection, ) -> Result<(), MigrationError> { // The V28 column set is still required and unchanged; V29 only appends columns and one table. for column in [ "platform_api_key_id", "max_attempts", "retry_backoff_step_ms", "next_attempt_at", "cancel_requested_at", "cancel_reason", ] { if !column_exists(connection, TASKS, column).await? { return Err(invalid(29, "v29.columns")); } } // Exact total column count for version 29: the 38 V28 columns + 5 V29 columns. let count: i64 = query( "select count(*)::bigint as count from information_schema.columns where table_schema = current_schema() and table_name = $1", ) .bind(TASKS) .fetch_one(&mut *connection) .await .map_err(|_| MigrationError::storage("preflight.schema"))? .try_get("count") .map_err(|_| MigrationError::storage("preflight.schema"))?; if count != 43_i64 { return Err(invalid(29, "v29.columns")); } if !relation_exists(connection, ATTEMPTS).await? { return Err(invalid(29, "v29.attempts")); } for column in [ "attempt_id", "task_id", "ordinal", "owner", "fence", "input_fingerprint", "upstream_key_digest", "retry_allowed", "checkpoint_at", "resolved_at", "outcome", "certainty", "phase", ] { if !column_exists(connection, ATTEMPTS, column).await? { return Err(invalid(29, "v29.attempts")); } } Ok(()) } fn invalid(version: i64, stage: &'static str) -> MigrationError { MigrationError::new( "partial_sequence", stage, Some(version), "restore_known_good_backup", ) }