//! Story 3.8: V19 immutable Resource security revocations — migration evidence. //! //! Fresh install, upgrade from V18 without rewriting the prior ledger, schema //! drift detection, failed-DDL rollback and the fail-closed future-version gate //! (the path an old binary takes when it meets a database newer than itself). use super::*; use crank_registry::MigrationApplyResult; #[tokio::test] async fn fresh_install_creates_the_empty_revocation_authority() { let database_url = crank_test_support::postgres_schema_url("test_v19_fresh_install").await; let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); MigrationAuthority::apply(&pool).await.unwrap(); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::Current { version: 30 } ); let rows: i64 = sqlx::query_scalar("select count(*) from resource_security_revocations") .fetch_one(&pool) .await .expect("V19 table must be queryable after a fresh install"); assert_eq!( rows, 0, "an empty authoritative table must preserve the previous (unrevoked) behaviour; \ no data backfill or mass migration is allowed" ); } #[tokio::test] async fn v18_upgrades_to_v19_without_rewriting_the_prior_ledger() { let database_url = crank_test_support::postgres_schema_url("test_v19_upgrade").await; let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); MigrationAuthority::apply(&pool).await.unwrap(); let before: Vec<(i64, String, String)> = sqlx::query_as("select version, name, checksum from __crank_migrations where version < 19 order by version") .fetch_all(&pool) .await .unwrap(); remove_v19_schema(&pool).await; assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 18, target: 30, } ); assert_eq!( MigrationAuthority::apply(&pool).await.unwrap(), MigrationApplyResult::Applied { from: 18, to: 30 } ); let after: Vec<(i64, String, String)> = sqlx::query_as("select version, name, checksum from __crank_migrations where version < 19 order by version") .fetch_all(&pool) .await .unwrap(); assert_eq!(before, after, "V16–V18 ledger rows must not be rewritten"); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::Current { version: 30 } ); } #[tokio::test] async fn v19_guard_rejects_a_missing_revocation_trigger() { let database_url = crank_test_support::postgres_schema_url("test_v19_trigger_drift").await; let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); MigrationAuthority::apply(&pool).await.unwrap(); sqlx::query( "drop trigger resource_security_revocations_immutable_guard on resource_security_revocations", ) .execute(&pool) .await .expect("the V19 trigger must be droppable for the drift fixture"); let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); assert_eq!(error.code(), "partial_sequence"); assert_eq!(error.version(), Some(19)); assert_eq!(error.stage(), "v19.immutable_trigger"); } #[tokio::test] async fn a_database_newer_than_the_binary_fails_closed_at_startup() { let database_url = crank_test_support::postgres_schema_url("test_v19_future_version").await; let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); MigrationAuthority::apply(&pool).await.unwrap(); // The same `version > CURRENT_VERSION` branch an older binary takes when it meets a newer // database; a synthetic version above the current one exercises it without a second binary. let current: i64 = sqlx::query_scalar("select max(version) from __crank_migrations") .fetch_one(&pool) .await .unwrap(); let future = current + 1; sqlx::query( "insert into __crank_migrations (version, name, checksum, phase, compatibility) values ($1, 'future-synthetic', repeat('a', 64), 'expand', 'n-minus-one-readable')", ) .bind(future) .execute(&pool) .await .unwrap(); let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); assert_eq!(error.code(), "future_version"); assert_eq!(error.version(), Some(future)); assert_eq!(error.recovery(), "install_matching_application"); assert!( MigrationAuthority::require_current(&pool).await.is_err(), "startup must refuse a newer database instead of serving with unknown schema" ); } #[tokio::test] async fn failed_v19_migration_rolls_back_schema_and_ledger() { let _event_trigger_guard = EVENT_TRIGGER_TEST_LOCK.lock().await; let database_url = crank_test_support::postgres_schema_url("test_v19_rollback").await; let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); MigrationAuthority::apply(&pool).await.unwrap(); remove_v19_schema(&pool).await; let schema: String = sqlx::query_scalar("select current_schema()") .fetch_one(&pool) .await .unwrap(); let failure_trigger = format!( "create function reject_story38_v19() returns event_trigger language plpgsql as $$ begin if current_schema() = '{schema}' and current_query() like '%resource_security_revocations%' then raise exception 'injected v19 ddl failure'; end if; end $$; create event trigger reject_story38_v19 on ddl_command_start execute function reject_story38_v19();" ); sqlx::raw_sql(sqlx::AssertSqlSafe(failure_trigger)) .execute(&pool) .await .unwrap(); let error = MigrationAuthority::apply(&pool).await.unwrap_err(); sqlx::raw_sql( "drop event trigger reject_story38_v19; drop function reject_story38_v19();", ) .execute(&pool) .await .unwrap(); assert_eq!(error.code(), "apply_failed"); assert_eq!(error.version(), Some(19)); let present: bool = sqlx::query_scalar( "select to_regclass(format('%I.%I', current_schema(), 'resource_security_revocations')) is not null", ) .fetch_one(&pool) .await .unwrap(); assert!(!present, "failed V19 DDL must roll the table back"); let ledger_v19: i64 = sqlx::query_scalar("select count(*) from __crank_migrations where version = 19") .fetch_one(&pool) .await .unwrap(); assert_eq!(ledger_v19, 0); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 18, target: 30, } ); }