//! Тестовые helpers, возвращающие приватную схему к состоянию до конкретной //! миграции (вынесены из `migrations.rs`, чтобы файл оставался в пределах //! rust-code-health); поведение не меняется. pub(crate) async fn remove_v3_schema(pool: &sqlx::PgPool) { sqlx::raw_sql( "drop index if exists invocation_logs_workspace_request_id_idx; alter table invocation_logs drop column if exists trace_id;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v4_schema(pool: &sqlx::PgPool) { remove_v5_schema(pool).await; sqlx::raw_sql( "drop trigger if exists operation_versions_immutable_guard on operation_versions; drop function if exists crank_guard_operation_version_immutable(); drop trigger if exists published_operations_monotonic_guard on published_operations; drop function if exists crank_guard_published_operation_pointer(); drop trigger if exists operations_latest_pointer_monotonic_guard on operations; drop function if exists crank_guard_operation_latest_pointer(); alter table operation_versions drop constraint if exists operation_versions_snapshot_provenance_check, drop column if exists name, drop column if exists display_name, drop column if exists category, drop column if exists protocol, drop column if exists security_level, drop column if exists snapshot_provenance, drop column if exists snapshot_observed_at, drop column if exists published_at, drop column if exists published_by; delete from __crank_migrations where version = 4;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v5_schema(pool: &sqlx::PgPool) { remove_v24_schema(pool).await; remove_v6_schema(pool).await; sqlx::raw_sql( "drop index if exists invocation_logs_workspace_operation_version_idx; alter table invocation_logs drop column if exists operation_version, drop column if exists execution_stage, drop column if exists execution_error_code, drop column if exists retryability, drop column if exists outcome_certainty; delete from __crank_migrations where version = 5;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v6_schema(pool: &sqlx::PgPool) { remove_v7_schema(pool).await; sqlx::raw_sql( "drop index if exists platform_api_keys_workspace_name_active_idx; create unique index if not exists platform_api_keys_workspace_name_idx on platform_api_keys(workspace_id, name); delete from __crank_migrations where version = 6;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v7_schema(pool: &sqlx::PgPool) { remove_v8_schema(pool).await; sqlx::raw_sql( "alter table secret_versions drop constraint if exists secret_versions_target_all_or_none_check, drop constraint if exists secret_versions_target_epoch_check, drop constraint if exists secret_versions_master_key_epoch_check, drop column if exists target_master_key_epoch, drop column if exists target_key_version, drop column if exists target_ciphertext, drop column if exists master_key_epoch; drop table if exists master_key_rotations; drop table if exists master_key_identities; delete from __crank_migrations where version = 7;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v8_schema(pool: &sqlx::PgPool) { remove_v9_schema(pool).await; sqlx::raw_sql( "drop table if exists admin_security_audit_events; drop table if exists admin_login_backoff; drop table if exists admin_bootstrap_contracts; alter table user_sessions drop constraint if exists user_sessions_csrf_hash_check, drop column if exists csrf_hash, drop column if exists revoked_at; delete from __crank_migrations where version = 8;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v9_schema(pool: &sqlx::PgPool) { remove_v10_schema(pool).await; sqlx::raw_sql( "drop trigger if exists published_agents_monotonic_guard on published_agents; drop function if exists crank_reject_published_agent_pointer_rewind(); drop trigger if exists agent_operation_bindings_immutable_guard on agent_operation_bindings; drop function if exists crank_reject_published_agent_binding_mutation(); drop trigger if exists agent_versions_immutable_guard on agent_versions; drop function if exists crank_reject_published_agent_version_mutation(); alter table published_agents drop constraint if exists published_agents_catalog_revision_check, drop column if exists catalog_revision; alter table agents drop constraint if exists agents_catalog_revision_check, drop column if exists catalog_revision; delete from __crank_migrations where version = 9;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v10_schema(pool: &sqlx::PgPool) { remove_v11_schema(pool).await; sqlx::raw_sql( "drop index if exists approval_requests_pending_scope_fingerprint_idx; drop index if exists approval_requests_workspace_request_trace_idx; alter table approval_requests drop constraint if exists approval_requests_request_id_check; alter table approval_requests drop constraint if exists approval_requests_trace_id_check; alter table approval_requests drop column if exists request_id; alter table approval_requests drop column if exists trace_id; create unique index if not exists approval_requests_pending_fingerprint_idx on approval_requests(agent_id, operation_id, operation_version, request_fingerprint) where status = 'pending' and request_fingerprint is not null; delete from __crank_migrations where version = 10;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v11_schema(pool: &sqlx::PgPool) { remove_v12_schema(pool).await; sqlx::raw_sql( "drop table if exists onboarding_selections; drop trigger if exists product_events_append_only_guard on product_events; drop function if exists crank_reject_product_event_mutation(); drop table if exists product_event_daily_rollups; drop table if exists product_events; drop index if exists invocation_logs_workspace_agent_key_success_idx; alter table invocation_logs drop constraint if exists invocation_logs_platform_key_scope_fk; alter table invocation_logs drop column if exists platform_api_key_id; alter table platform_api_keys drop constraint if exists platform_api_keys_workspace_agent_id_unique; delete from __crank_migrations where version = 11;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v14_schema(pool: &sqlx::PgPool) { remove_v24_schema(pool).await; remove_v15_schema(pool).await; sqlx::raw_sql( "drop table if exists import_plan_applications; drop table if exists import_plans; drop function if exists crank_reject_import_plan_mutation(); alter table import_jobs drop constraint if exists import_jobs_workspace_id_unique; delete from __crank_migrations where version = 14;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v15_schema(pool: &sqlx::PgPool) { remove_v16_schema(pool).await; sqlx::raw_sql( "drop table if exists onboarding_import_selections; alter table operations drop constraint if exists operations_workspace_id_unique; alter table product_events drop constraint if exists product_events_name_check; alter table product_events add constraint product_events_name_check check (event_name in ( 'onboarding_eligible', 'onboarding_started', 'onboarding_resumed', 'onboarding_dismissed', 'onboarding_abandoned', 'onboarding_completed' )); alter table product_event_daily_rollups drop constraint if exists product_event_daily_rollups_name_check; alter table product_event_daily_rollups add constraint product_event_daily_rollups_name_check check (event_name in ( 'onboarding_eligible', 'onboarding_started', 'onboarding_resumed', 'onboarding_dismissed', 'onboarding_abandoned', 'onboarding_completed' )); delete from __crank_migrations where version = 15;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v16_schema(pool: &sqlx::PgPool) { remove_v17_schema(pool).await; sqlx::raw_sql( "drop table if exists resource_versions; drop index if exists resources_workspace_created_idx; drop table if exists resources; delete from __crank_migrations where version = 16;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v17_schema(pool: &sqlx::PgPool) { remove_v18_schema(pool).await; sqlx::raw_sql( "drop table if exists resource_publications; drop index if exists resource_publications_workspace_uri_idx; delete from __crank_migrations where version = 17;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v18_schema(pool: &sqlx::PgPool) { remove_v19_schema(pool).await; sqlx::raw_sql( "drop table if exists agent_resource_bindings; drop function if exists crank_reject_published_agent_resource_binding_mutation(); drop index if exists agent_resource_bindings_resource_idx; drop index if exists agents_workspace_id_idx; delete from __crank_migrations where version = 18;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v19_schema(pool: &sqlx::PgPool) { remove_v24_schema(pool).await; remove_v20_schema(pool).await; sqlx::raw_sql( "drop table if exists resource_security_revocations; drop function if exists crank_reject_resource_security_revocation_mutation(); delete from __crank_migrations where version = 19;", ) .execute(pool) .await .unwrap(); } /// Story 3.16: снять V23 (version-keyed пины зависимостей Prompt) перед возвратом к < 23. pub(crate) async fn remove_v23_schema(pool: &sqlx::PgPool) { sqlx::raw_sql( "drop table if exists prompt_message_resource_refs; drop table if exists prompt_dependencies; delete from __crank_migrations where version = 23;", ) .execute(pool) .await .unwrap(); } /// Story 3.13: снять V22 (Published Prompt Versions и typed Prompt bindings) /// перед возвратом к состоянию < 22. pub(crate) async fn remove_v22_schema(pool: &sqlx::PgPool) { remove_v23_schema(pool).await; sqlx::raw_sql( "drop table if exists agent_prompt_bindings; drop table if exists prompt_publications; drop trigger if exists prompt_arguments_immutable_guard on prompt_arguments; drop trigger if exists prompt_messages_immutable_guard on prompt_messages; alter table prompts drop constraint if exists prompts_latest_published_version_check; alter table prompts drop column if exists latest_published_version; drop function if exists crank_reject_published_prompt_version_mutation(); drop function if exists crank_reject_prompt_publication_mutation(); drop function if exists crank_reject_published_agent_prompt_binding_mutation(); delete from __crank_migrations where version = 22;", ) .execute(pool) .await .unwrap(); } /// Story 3.12: снять V21 (typed Prompt Drafts) перед возвратом к состоянию < 21. pub(crate) async fn remove_v21_schema(pool: &sqlx::PgPool) { remove_v22_schema(pool).await; sqlx::raw_sql( "drop table if exists prompt_messages; drop table if exists prompt_arguments; drop table if exists prompt_versions; drop table if exists prompts; delete from __crank_migrations where version = 21;", ) .execute(pool) .await .unwrap(); } /// Story 3.10: обратная совместимость fixture-ов — снять V20 и вернуть V16/V17 /// ограничения artifact-строк, чтобы схема соответствовала версии < 20. pub(crate) async fn remove_v20_schema(pool: &sqlx::PgPool) { remove_v21_schema(pool).await; sqlx::raw_sql( "drop table if exists resource_template_sources; drop function if exists crank_guard_resource_template_source_immutable(); alter table resource_versions drop constraint if exists resource_versions_source_kind_check; alter table resource_versions drop constraint if exists resource_versions_size_check; alter table resource_versions add constraint resource_versions_size_check check (size_bytes between 1 and 262144); alter table resource_versions drop constraint if exists resource_versions_source_id_check; alter table resource_versions add constraint resource_versions_source_id_check check (artifact_source_id ~ '^src_[A-Za-z0-9_-]{1,128}$'); alter table resource_versions drop column if exists source_kind; alter table resource_versions alter column artifact_source_id set not null; alter table resource_publications drop constraint if exists resource_publications_source_kind_check; alter table resource_publications drop constraint if exists resource_publications_size_check; alter table resource_publications add constraint resource_publications_size_check check (size_bytes between 1 and 262144); alter table resource_publications drop constraint if exists resource_publications_source_id_check; alter table resource_publications add constraint resource_publications_source_id_check check (artifact_source_id ~ '^src_[A-Za-z0-9_-]{1,128}$'); alter table resource_publications drop column if exists source_kind; alter table resource_publications alter column artifact_source_id set not null; delete from __crank_migrations where version = 20;", ) .execute(pool) .await .unwrap(); } pub(crate) async fn remove_v12_schema(pool: &sqlx::PgPool) { remove_v24_schema(pool).await; remove_v14_schema(pool).await; sqlx::raw_sql( "drop index if exists import_jobs_openapi_source_idx; drop index if exists import_jobs_expires_at_idx; drop index if exists artifact_sources_openapi_dangling_idx; drop index if exists artifact_sources_blob_lifecycle_detached_idx; drop index if exists artifact_blobs_expired_claim_idx; delete from __crank_migrations where version = 13; drop table if exists artifact_sources; drop table if exists artifact_blobs; delete from __crank_migrations where version = 12;", ) .execute(pool) .await .unwrap(); } /// Story 4.2: снять V25 (durable Tasks и Task-linked dispatch intent) перед возвратом к < 25. pub(crate) async fn remove_v25_schema(pool: &sqlx::PgPool) { // V25 created `tasks`; V26–V28 only altered it, so dropping the table removes their schema too. // V30 adds tables that reference `tasks`; they are dropped explicitly so the legacy preflight // sees an actually absent V30 shape instead of orphan continuation tables. sqlx::raw_sql( "drop table if exists task_input_audit cascade; drop table if exists task_input_requests cascade; drop table if exists tasks cascade; delete from __crank_migrations where version >= 25;", ) .execute(pool) .await .unwrap(); } /// Story 4.1: снять V24 (authoritative execution admission) перед возвратом к < 24. pub(crate) async fn remove_v24_schema(pool: &sqlx::PgPool) { remove_v25_schema(pool).await; sqlx::raw_sql( "drop table if exists execution_admission_entries; drop table if exists execution_admission_controllers; delete from __crank_migrations where version = 24;", ) .execute(pool) .await .unwrap(); }