//! V26 schema guard: lease/fencing columns for durable background Tasks (Story 4.3). use sqlx::{PgConnection, Row, query}; use super::authority::MigrationError; use super::schema_guard::relation_exists; const TASKS: &str = "tasks"; const TASK_COLUMNS: &[(&str, &str, bool)] = &[ ("task_id", "uuid", false), ("workspace_id", "text", false), ("owner_kind", "text", false), ("owner_id", "text", false), ("authorization_scope", "text", false), ("operation_id", "text", false), ("operation_version", "integer", false), ("scoped_key", "text", false), ("input_fingerprint", "text", false), ("request_id", "text", false), ("trace_id", "text", false), ("traceparent", "text", false), ("trace_sampled", "boolean", false), ("policy_mode", "text", false), ("approval_reference", "text", true), ("state", "text", false), ("version", "integer", false), ("attempts", "integer", false), ("input_envelope", "jsonb", false), ("progress_envelope", "jsonb", true), ("result_envelope", "jsonb", true), ("error_envelope", "jsonb", true), ("execution_deadline", "timestamp with time zone", false), ("retain_until", "timestamp with time zone", false), ("created_at", "timestamp with time zone", false), ("updated_at", "timestamp with time zone", false), ("fence", "bigint", false), ("lease_owner", "text", true), ("lease_until", "timestamp with time zone", true), ("current_attempt_id", "text", true), ("dispatch_started_at", "timestamp with time zone", true), ]; fn v26_error(stage: &'static str) -> MigrationError { MigrationError::new( "partial_sequence", stage, Some(26), "restore_known_good_backup", ) } pub(super) async fn validate_v26_absent( connection: &mut PgConnection, ) -> Result { if relation_exists(connection, TASKS).await? { // delegate to v25 absent for relation mostly; V26 columns must not exist. for column in [ "fence", "lease_owner", "lease_until", "current_attempt_id", "dispatch_started_at", ] { if super::schema_guard::column_exists(connection, TASKS, column).await? { return Ok(false); } } } Ok(true) } pub(super) async fn validate_v26_base_columns( connection: &mut PgConnection, ) -> Result<(), MigrationError> { if !relation_exists(connection, TASKS).await? { return Err(v26_error("v26.relations")); } for (column, data_type, nullable) in TASK_COLUMNS { 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(TASKS) .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(v26_error("v26.columns")); } } Ok(()) } pub(super) async fn validate_v26_durable_tasks( connection: &mut PgConnection, ) -> Result<(), MigrationError> { validate_v26_base_columns(connection).await?; 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 != i64::try_from(TASK_COLUMNS.len()).unwrap_or_default() { return Err(v26_error("v26.columns")); } // Constraints/index checks inherited conceptually from V25; V26 adds columns only. Ok(()) }