Files
crank/crates/crank-registry/src/migrations/schema_guard_v10.rs
T

84 lines
3.7 KiB
Rust

use sqlx::{PgConnection, Row, query};
use super::authority::MigrationError;
use super::schema_guard::{normalize_definition, relation_exists, schema_error};
pub(super) async fn validate_v10_absent(
connection: &mut PgConnection,
) -> Result<bool, MigrationError> {
let old_present =
relation_exists(connection, "approval_requests_pending_fingerprint_idx").await?;
let new_present = relation_exists(
connection,
"approval_requests_pending_scope_fingerprint_idx",
)
.await?;
Ok(old_present && !new_present)
}
pub(super) async fn validate_v10_approval_side_effects(
connection: &mut PgConnection,
) -> Result<(), MigrationError> {
if !relation_exists(connection, "approval_requests_pending_fingerprint_idx").await? {
return Err(schema_error(10));
}
for column in ["request_id", "trace_id"] {
let present = query(
"select 1
from information_schema.columns
where table_schema = current_schema()
and table_name = 'approval_requests'
and column_name = $1",
)
.bind(column)
.fetch_optional(&mut *connection)
.await
.map_err(|_| MigrationError::storage("preflight.schema"))?
.is_some();
if !present {
return Err(schema_error(10));
}
}
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_indexdef(i.indexrelid, 4, true) as fourth_column,
pg_get_indexdef(i.indexrelid, 5, true) as fifth_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 = 'approval_requests_pending_scope_fingerprint_idx'",
)
.fetch_optional(connection)
.await
.map_err(|_| MigrationError::storage("preflight.schema"))?;
let valid = row.is_some_and(|row| {
row.try_get::<String, _>("table_name").ok().as_deref() == Some("approval_requests")
&& row.try_get::<String, _>("access_method").ok().as_deref() == Some("btree")
&& row.try_get::<bool, _>("indisvalid").ok() == Some(true)
&& row.try_get::<bool, _>("indisready").ok() == Some(true)
&& row.try_get::<bool, _>("indisunique").ok() == Some(true)
&& row.try_get::<String, _>("first_column").ok().as_deref() == Some("workspace_id")
&& row.try_get::<String, _>("second_column").ok().as_deref() == Some("agent_id")
&& row.try_get::<String, _>("third_column").ok().as_deref() == Some("operation_id")
&& row.try_get::<String, _>("fourth_column").ok().as_deref()
== Some("operation_version")
&& row.try_get::<String, _>("fifth_column").ok().as_deref()
== Some("request_fingerprint")
&& row
.try_get::<String, _>("predicate")
.ok()
.is_some_and(|value| {
normalize_definition(&value)
== "status='pending'::textandrequest_fingerprintisnotnull"
})
});
if valid { Ok(()) } else { Err(schema_error(10)) }
}