"""Add typed resumable migration-engine records. Revision ID: 20260826_0019 Revises: 20260826_0018 """ from collections.abc import Sequence import sqlalchemy as sa from alembic import op revision: str = "20260826_0019" down_revision: str | None = "20260826_0018" branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None def _id() -> sa.Column[object]: return sa.Column("id", sa.Uuid(), primary_key=True, nullable=False) def _created() -> sa.Column[object]: return sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()) def upgrade() -> None: op.create_table( "migration_validation_policy_revisions", _id(), sa.Column("key", sa.String(128), nullable=False), sa.Column("revision", sa.Integer(), nullable=False), sa.Column("required_completeness", sa.Float(), nullable=False), sa.Column("allowed_failures", sa.Integer(), nullable=False), sa.Column("required_evaluation", sa.Boolean(), nullable=False), sa.Column("critical_regressions_allowed", sa.Integer(), nullable=False), sa.Column("maximum_latency_regression_ratio", sa.Float()), sa.Column("require_project_fit", sa.Boolean(), nullable=False), sa.Column("require_external_validation", sa.Boolean(), nullable=False), sa.Column("require_security_approved", sa.Boolean(), nullable=False), sa.Column("allow_isolated_lab_cutover", sa.Boolean(), nullable=False), sa.Column("fingerprint", sa.String(64), nullable=False), sa.Column("active", sa.Boolean(), nullable=False, server_default=sa.true()), sa.Column("created_by", sa.String(255), nullable=False), _created(), sa.Column("immutable_at", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.UniqueConstraint("key", "revision", name="uq_migration_validation_policy_revision"), sa.UniqueConstraint("fingerprint", name="uq_migration_validation_policy_fingerprint"), ) op.create_index( "ix_migration_validation_policy_revisions_key", "migration_validation_policy_revisions", ["key"], ) op.create_index( "ix_migration_validation_policy_revisions_active", "migration_validation_policy_revisions", ["active"], ) op.create_table( "migration_plans", _id(), sa.Column( "project_id", sa.Uuid(), sa.ForeignKey("projects.id", ondelete="RESTRICT"), nullable=False, ), sa.Column( "project_binding_id", sa.Uuid(), sa.ForeignKey("project_bindings.id", ondelete="RESTRICT"), nullable=False, ), sa.Column( "capability_contract_id", sa.Uuid(), sa.ForeignKey("capability_contracts.id", ondelete="RESTRICT"), nullable=False, ), sa.Column("migration_class", sa.String(32), nullable=False), sa.Column("environment", sa.String(32), nullable=False), sa.Column("adapter", sa.JSON(), nullable=False), sa.Column("source_identity", sa.JSON(), nullable=False), sa.Column("target_identity", sa.JSON(), nullable=False), sa.Column("source_data_target", sa.Text(), nullable=False), sa.Column("target_shadow_target", sa.Text(), nullable=False), sa.Column("source_space_ref", sa.String(255), nullable=False), sa.Column( "target_space_id", sa.Uuid(), sa.ForeignKey("embedding_spaces.id", ondelete="RESTRICT"), nullable=False, ), sa.Column("corpus_revision", sa.String(128), nullable=False), sa.Column("migration_policy_revision", sa.String(128), nullable=False), sa.Column( "validation_policy_revision_id", sa.Uuid(), sa.ForeignKey("migration_validation_policy_revisions.id", ondelete="RESTRICT"), nullable=False, ), sa.Column( "lifecycle_approval_id", sa.Uuid(), sa.ForeignKey("lifecycle_approval_requests.id", ondelete="RESTRICT"), nullable=False, ), sa.Column( "promotion_plan_id", sa.Uuid(), sa.ForeignKey("lifecycle_promotion_plans.id", ondelete="RESTRICT"), ), sa.Column("rollback_target_ref", sa.Text(), nullable=False), sa.Column("total_expected_items", sa.Integer(), nullable=False), sa.Column("batch_size", sa.Integer(), nullable=False), sa.Column("max_in_flight_batches", sa.Integer(), nullable=False), sa.Column("concurrency", sa.Integer(), nullable=False), sa.Column("priority", sa.String(32), nullable=False), sa.Column("target_storage", sa.JSON(), nullable=False), sa.Column("shadow_policy", sa.JSON(), nullable=False), sa.Column("cutover_policy", sa.JSON(), nullable=False), sa.Column("rollback_retention_days", sa.Integer(), nullable=False), sa.Column("environment_fingerprint", sa.String(64), nullable=False), sa.Column("idempotency_key", sa.String(128), nullable=False), sa.Column("created_by", sa.String(255), nullable=False), sa.Column("irreversible", sa.Boolean(), nullable=False, server_default=sa.false()), sa.Column("schema_steps", sa.JSON(), nullable=False), sa.Column("state", sa.String(48), nullable=False, server_default="PLANNED"), sa.Column("version", sa.Integer(), nullable=False, server_default="1"), sa.Column("generation", sa.BigInteger(), nullable=False, server_default="1"), sa.Column("plan_fingerprint", sa.String(64), nullable=False), sa.Column("approval_fingerprint", sa.String(64), nullable=False), sa.Column("completed_items", sa.Integer(), nullable=False, server_default="0"), sa.Column("failed_items", sa.Integer(), nullable=False, server_default="0"), sa.Column("retryable_items", sa.Integer(), nullable=False, server_default="0"), sa.Column("permanent_failed_items", sa.Integer(), nullable=False, server_default="0"), sa.Column("last_cursor", sa.String(255)), sa.Column("cancel_requested", sa.Boolean(), nullable=False, server_default=sa.false()), sa.Column("failure_code", sa.String(64)), sa.Column("failure_details", sa.JSON(), nullable=False), sa.Column("started_at", sa.DateTime(timezone=True)), sa.Column("completed_at", sa.DateTime(timezone=True)), sa.Column("immutable_at", sa.DateTime(timezone=True), server_default=sa.func.now()), _created(), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.UniqueConstraint("idempotency_key", name="uq_migration_plan_idempotency"), sa.UniqueConstraint("plan_fingerprint", name="uq_migration_plan_fingerprint"), sa.UniqueConstraint("target_shadow_target", name="uq_migration_plans_target_shadow_target"), sa.CheckConstraint("version >= 1", name="ck_migration_plan_version"), sa.CheckConstraint("generation >= 1", name="ck_migration_plan_generation"), sa.CheckConstraint( "completed_items >= 0 AND failed_items >= 0", name="ck_migration_plan_progress" ), ) for column in ( "project_id", "project_binding_id", "capability_contract_id", "migration_class", "environment", "target_space_id", "corpus_revision", "validation_policy_revision_id", "lifecycle_approval_id", "promotion_plan_id", "state", ): op.create_index(f"ix_migration_plans_{column}", "migration_plans", [column]) op.create_index( "uq_migration_one_production_active", "migration_plans", ["project_id", "capability_contract_id"], unique=True, postgresql_where=sa.text( "environment = 'PRODUCTION' AND state NOT IN " "('CUTOVER_COMMITTED','ROLLED_BACK','FAILED','CANCELLED')" ), ) op.create_table( "migration_batch_checkpoints", _id(), sa.Column( "migration_plan_id", sa.Uuid(), sa.ForeignKey("migration_plans.id", ondelete="RESTRICT"), nullable=False, ), sa.Column("batch_number", sa.Integer(), nullable=False), sa.Column("generation", sa.BigInteger(), nullable=False), sa.Column("cursor_start", sa.String(255), nullable=False), sa.Column("cursor_end", sa.String(255), nullable=False), sa.Column("item_count", sa.Integer(), nullable=False), sa.Column("completed_items", sa.Integer(), nullable=False, server_default="0"), sa.Column("failed_items", sa.Integer(), nullable=False, server_default="0"), sa.Column("retryable_items", sa.Integer(), nullable=False, server_default="0"), sa.Column("permanent_failed_items", sa.Integer(), nullable=False, server_default="0"), sa.Column("item_fingerprint", sa.String(64), nullable=False), sa.Column("result_fingerprint", sa.String(64)), sa.Column("status", sa.String(32), nullable=False), sa.Column("attempts", sa.Integer(), nullable=False, server_default="0"), sa.Column("duration_ms", sa.Float()), sa.Column("error_code", sa.String(64)), sa.Column("bounded_errors", sa.JSON(), nullable=False), sa.Column("started_at", sa.DateTime(timezone=True)), sa.Column("completed_at", sa.DateTime(timezone=True)), sa.UniqueConstraint( "migration_plan_id", "batch_number", "generation", name="uq_migration_batch" ), ) op.create_index( "ix_migration_batch_checkpoints_migration_plan_id", "migration_batch_checkpoints", ["migration_plan_id"], ) op.create_index( "ix_migration_batch_checkpoints_status", "migration_batch_checkpoints", ["status"] ) op.create_table( "migration_validation_snapshots", _id(), sa.Column( "migration_plan_id", sa.Uuid(), sa.ForeignKey("migration_plans.id", ondelete="RESTRICT"), nullable=False, ), sa.Column( "validation_policy_revision_id", sa.Uuid(), sa.ForeignKey("migration_validation_policy_revisions.id", ondelete="RESTRICT"), nullable=False, ), sa.Column("generation", sa.BigInteger(), nullable=False), sa.Column("expected_count", sa.Integer(), nullable=False), sa.Column("actual_count", sa.Integer(), nullable=False), sa.Column("missing_count", sa.Integer(), nullable=False), sa.Column("duplicate_count", sa.Integer(), nullable=False), sa.Column("malformed_count", sa.Integer(), nullable=False), sa.Column("non_finite_count", sa.Integer(), nullable=False), sa.Column("wrong_dimension_count", sa.Integer(), nullable=False), sa.Column("content_hash_mismatch_count", sa.Integer(), nullable=False), sa.Column("wrong_space_count", sa.Integer(), nullable=False), sa.Column("index_schema_matches", sa.Boolean(), nullable=False), sa.Column("distance_metric_matches", sa.Boolean(), nullable=False), sa.Column("payload_integrity", sa.Boolean(), nullable=False), sa.Column("target_fingerprint", sa.String(64), nullable=False), sa.Column("evaluation_run_ids", sa.JSON(), nullable=False), sa.Column("comparable", sa.Boolean(), nullable=False), sa.Column("critical_regressions", sa.Integer(), nullable=False), sa.Column("latency_regression_ratio", sa.Float()), sa.Column("project_fit_eligible", sa.Boolean(), nullable=False), sa.Column("external_validation_satisfied", sa.Boolean(), nullable=False), sa.Column("security_approved", sa.Boolean(), nullable=False), sa.Column("evidence", sa.JSON(), nullable=False), sa.Column("snapshot_fingerprint", sa.String(64), nullable=False), sa.Column("passed", sa.Boolean(), nullable=False), sa.Column("technical_cutover_eligible", sa.Boolean(), nullable=False), sa.Column("project_promotion_eligible", sa.Boolean(), nullable=False), sa.Column("blockers", sa.JSON(), nullable=False), _created(), sa.Column("immutable_at", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.UniqueConstraint("snapshot_fingerprint", name="uq_migration_validation_snapshot"), ) op.create_index( "ix_migration_validation_snapshots_migration_plan_id", "migration_validation_snapshots", ["migration_plan_id"], ) op.create_index( "ix_migration_validation_snapshots_validation_policy_revision_id", "migration_validation_snapshots", ["validation_policy_revision_id"], ) op.create_index( "ix_migration_validation_snapshots_passed", "migration_validation_snapshots", ["passed"] ) op.create_table( "migration_shadow_sessions", _id(), sa.Column( "migration_plan_id", sa.Uuid(), sa.ForeignKey("migration_plans.id", ondelete="RESTRICT"), nullable=False, ), sa.Column("generation", sa.BigInteger(), nullable=False), sa.Column("status", sa.String(32), nullable=False), sa.Column("request_count", sa.Integer(), nullable=False), sa.Column("source_error_count", sa.Integer(), nullable=False), sa.Column("target_error_count", sa.Integer(), nullable=False), sa.Column("source_latency_p95_ms", sa.Float(), nullable=False), sa.Column("target_latency_p95_ms", sa.Float(), nullable=False), sa.Column("critical_regressions", sa.Integer(), nullable=False), sa.Column("metrics", sa.JSON(), nullable=False), sa.Column("evidence_refs", sa.JSON(), nullable=False), sa.Column("result_fingerprint", sa.String(64), nullable=False), sa.Column("started_at", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.Column("completed_at", sa.DateTime(timezone=True)), sa.UniqueConstraint("result_fingerprint", name="uq_migration_shadow_result_fingerprint"), ) op.create_index( "ix_migration_shadow_sessions_migration_plan_id", "migration_shadow_sessions", ["migration_plan_id"], ) op.create_index("ix_migration_shadow_sessions_status", "migration_shadow_sessions", ["status"]) op.create_table( "migration_rollback_snapshots", _id(), sa.Column( "migration_plan_id", sa.Uuid(), sa.ForeignKey("migration_plans.id", ondelete="RESTRICT"), nullable=False, ), sa.Column("snapshot", sa.JSON(), nullable=False), sa.Column("snapshot_fingerprint", sa.String(64), nullable=False), sa.Column("retain_until", sa.DateTime(timezone=True), nullable=False), _created(), sa.Column("immutable_at", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.UniqueConstraint("migration_plan_id", name="uq_migration_rollback_plan"), sa.UniqueConstraint("snapshot_fingerprint", name="uq_migration_rollback_fingerprint"), ) op.create_index( "ix_migration_rollback_snapshots_migration_plan_id", "migration_rollback_snapshots", ["migration_plan_id"], ) op.create_index( "ix_migration_rollback_snapshots_retain_until", "migration_rollback_snapshots", ["retain_until"], ) op.create_table( "migration_cutover_operations", _id(), sa.Column( "migration_plan_id", sa.Uuid(), sa.ForeignKey("migration_plans.id", ondelete="RESTRICT"), nullable=False, ), sa.Column("stage", sa.String(32), nullable=False), sa.Column("idempotency_key", sa.String(128), nullable=False), sa.Column("generation", sa.BigInteger(), nullable=False), sa.Column("expected_plan_version", sa.Integer(), nullable=False), sa.Column("source_before", sa.Text(), nullable=False), sa.Column("target_after", sa.Text(), nullable=False), sa.Column("external_state_fingerprint", sa.String(64), nullable=False), sa.Column("health_evidence", sa.JSON(), nullable=False), sa.Column("failure_code", sa.String(64)), sa.Column("failure_details", sa.JSON(), nullable=False), sa.Column("switch_duration_ms", sa.Float()), sa.Column("rollback_duration_ms", sa.Float()), sa.Column("started_at", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.Column("finished_at", sa.DateTime(timezone=True)), sa.UniqueConstraint("idempotency_key", name="uq_migration_cutover_idempotency"), ) op.create_index( "ix_migration_cutover_operations_migration_plan_id", "migration_cutover_operations", ["migration_plan_id"], ) op.create_index( "ix_migration_cutover_operations_stage", "migration_cutover_operations", ["stage"] ) op.create_table( "migration_events", _id(), sa.Column( "migration_plan_id", sa.Uuid(), sa.ForeignKey("migration_plans.id", ondelete="RESTRICT"), nullable=False, ), sa.Column( "operation_id", sa.Uuid(), sa.ForeignKey("migration_cutover_operations.id", ondelete="RESTRICT"), ), sa.Column("event_type", sa.String(64), nullable=False), sa.Column("before_state", sa.String(48)), sa.Column("after_state", sa.String(48)), sa.Column("actor", sa.String(255), nullable=False), sa.Column("policy_revision", sa.String(128), nullable=False), sa.Column("evidence_refs", sa.JSON(), nullable=False), sa.Column("reason", sa.Text(), nullable=False), sa.Column("source_identity", sa.JSON(), nullable=False), sa.Column("target_identity", sa.JSON(), nullable=False), sa.Column("generation", sa.BigInteger(), nullable=False), sa.Column("change_id", sa.String(128), nullable=False), sa.Column("details", sa.JSON(), nullable=False), sa.Column("occurred_at", sa.DateTime(timezone=True), server_default=sa.func.now()), ) for column in ("migration_plan_id", "operation_id", "event_type", "change_id", "occurred_at"): op.create_index(f"ix_migration_events_{column}", "migration_events", [column]) def downgrade() -> None: op.drop_table("migration_events") op.drop_table("migration_cutover_operations") op.drop_table("migration_rollback_snapshots") op.drop_table("migration_shadow_sessions") op.drop_table("migration_validation_snapshots") op.drop_table("migration_batch_checkpoints") op.drop_table("migration_plans") op.drop_table("migration_validation_policy_revisions")