Files
crank/crates/crank-registry/src/migrations/schema_guard_v30.rs
T
bsodfather 90e5357a08
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 21m42s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
feat(epic4): продолжение ожидающей Task после ввода (Story 4.5)
Durable worker приостанавливает Task как input_required до dispatch, если
закреплённая схема требует отсутствующее поле; authorized schema-validated
update атомарно и с аудитом возобновляет тот же Task ровно один раз.
Replay/conflict/TTL безопасны, ожидание удерживает admission permit.
Additive schema v30: task_input_requests, fingerprint/version, TTL-индекс.
2026-10-30 16:24:12 +00:00

195 lines
5.8 KiB
Rust

//! V30 schema guard: continuation through validated input (Story 4.5).
use sqlx::{PgConnection, Row, query};
use super::authority::MigrationError;
use super::schema_guard::{column_exists, relation_exists};
pub(super) async fn validate_v30_absent(
connection: &mut PgConnection,
) -> Result<bool, MigrationError> {
if relation_exists(connection, "tasks").await?
&& column_exists(connection, "tasks", "effective_input_fingerprint").await?
{
return Ok(false);
}
if relation_exists(connection, "task_input_requests").await? {
return Ok(false);
}
if relation_exists(connection, "task_input_audit").await? {
return Ok(false);
}
Ok(true)
}
pub(super) async fn validate_v30_input_continuation(
connection: &mut PgConnection,
) -> Result<(), MigrationError> {
// V30 builds on the applied V29 shape. The exact V29 column count (43) no longer holds once V30
// appends its column (44), so the V29-specific columns are re-checked here by name instead of
// calling the exact-count V29 guard, which is only valid before V30 is applied.
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(30, "v30.columns"));
}
}
// V30 appends one column to tasks.
if !column_exists(connection, "tasks", "effective_input_fingerprint").await? {
return Err(invalid(30, "v30.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 != 44_i64 {
return Err(invalid(30, "v30.columns"));
}
// Tables.
for table in ["task_input_requests", "task_input_audit"] {
if !relation_exists(connection, table).await? {
return Err(invalid(30, "v30.tables"));
}
}
// task_input_requests columns.
for column in [
"task_id",
"wait_id",
"expected_version",
"schema_revision",
"schema_format_version",
"fingerprint_format_version",
"schema_snapshot",
"projected_schema",
"prompt_code",
"missing_fields",
"expires_at",
"created_at",
"accepted_at",
"update_key_digest",
"canonical_fingerprint",
"effective_input_fingerprint",
"committed_state",
"committed_version",
] {
if !column_exists(connection, "task_input_requests", column).await? {
return Err(invalid(30, "v30.task_input_requests"));
}
}
// task_input_audit columns.
for column in [
"id",
"task_id",
"wait_id",
"event",
"from_state",
"to_state",
"from_version",
"to_version",
"actor",
"reason_code",
"created_at",
] {
if !column_exists(connection, "task_input_audit", column).await? {
return Err(invalid(30, "v30.task_input_audit"));
}
}
// Constraints.
for constraint in [
"task_input_requests_pkey",
"task_input_requests_wait_unique",
"task_input_requests_prompt_check",
"task_input_requests_receipt_check",
"task_input_audit_event_check",
"task_input_audit_actor_check",
"task_input_audit_reason_check",
"tasks_effective_input_fingerprint_check",
] {
let table = if constraint.starts_with("task_input_audit")
|| constraint.starts_with("task_input_audit_")
{
"task_input_audit"
} else if constraint == "tasks_effective_input_fingerprint_check" {
"tasks"
} else {
"task_input_requests"
};
if !named_constraint_exists(connection, table, constraint).await? {
return Err(invalid(30, "v30.constraints"));
}
}
// Indexes.
for index in [
"task_input_requests_active_ttl_idx",
"task_input_audit_task_idx",
] {
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::<bool, _>("present")
.map_err(|_| MigrationError::storage("preflight.schema"))?;
if !present {
return Err(invalid(30, "v30.indexes"));
}
}
Ok(())
}
fn invalid(version: i64, stage: &'static str) -> MigrationError {
MigrationError::new(
"partial_sequence",
stage,
Some(version),
"restore_known_good_backup",
)
}
// helper
async fn named_constraint_exists(
connection: &mut PgConnection,
table: &str,
constraint: &str,
) -> Result<bool, MigrationError> {
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::<bool, _>("present")
.map_err(|_| MigrationError::storage("preflight.schema"))
}