"""production idempotency, corrections and slot guards Revision ID: 202605150004 Revises: 202605150003 Create Date: 2026-05-15 16:00:00.000000 """ from collections.abc import Sequence import sqlalchemy as sa from alembic import op revision: str = "202605150004" down_revision: str | None = "202605150003" branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None def upgrade() -> None: op.add_column("service_visits", sa.Column("version", sa.Integer(), server_default="1", nullable=False)) op.add_column("service_visits", sa.Column("completed_snapshot", sa.JSON(), nullable=True)) op.create_index( "uq_service_entries_service_visit_id_not_null", "service_entries", ["service_visit_id"], unique=True, postgresql_where=sa.text("service_visit_id is not null"), ) op.create_index( "uq_expense_entries_service_visit_id_not_null", "expense_entries", ["service_visit_id"], unique=True, postgresql_where=sa.text("service_visit_id is not null"), ) op.create_index( "uq_active_service_appointment_slot", "service_appointments", ["service_center_id", "requested_start_at", "requested_end_at"], unique=True, postgresql_where=sa.text("status in ('requested','confirmed','confirmed_by_sto','proposed_new_time')"), ) op.create_table( "work_order_corrections", sa.Column("id", sa.Integer(), nullable=False), sa.Column("service_visit_id", sa.Integer(), nullable=False), sa.Column("requested_by_user_id", sa.Integer(), nullable=True), sa.Column("reason", sa.Text(), nullable=False), sa.Column("proposed_changes", sa.JSON(), nullable=True), sa.Column("status", sa.String(length=24), server_default="pending", nullable=False), sa.Column("owner_approval_required", sa.Boolean(), server_default=sa.text("true"), nullable=False), sa.Column("created_version", sa.Integer(), server_default="1", nullable=False), sa.Column("resolved_at", sa.DateTime(timezone=True), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.ForeignKeyConstraint(["requested_by_user_id"], ["users.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["service_visit_id"], ["service_visits.id"], ondelete="CASCADE"), sa.PrimaryKeyConstraint("id"), ) op.create_index("ix_work_order_corrections_created_at", "work_order_corrections", ["created_at"]) op.create_index("ix_work_order_corrections_service_visit_id", "work_order_corrections", ["service_visit_id"]) op.create_index("ix_work_order_corrections_status", "work_order_corrections", ["status"]) op.create_table( "inventory_transactions", sa.Column("id", sa.Integer(), nullable=False), sa.Column("service_center_id", sa.Integer(), nullable=False), sa.Column("service_visit_id", sa.Integer(), nullable=True), sa.Column("product_item_id", sa.Integer(), nullable=True), sa.Column("transaction_type", sa.String(length=32), nullable=False), sa.Column("sku", sa.String(length=120), nullable=True), sa.Column("title", sa.String(length=180), nullable=True), sa.Column("quantity", sa.Numeric(10, 3), server_default="0", nullable=False), sa.Column("unit", sa.String(length=24), server_default="pcs", nullable=False), sa.Column("actor_user_id", sa.Integer(), nullable=True), sa.Column("metadata_json", sa.JSON(), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.ForeignKeyConstraint(["actor_user_id"], ["users.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["product_item_id"], ["service_product_items.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["service_center_id"], ["service_centers.id"], ondelete="CASCADE"), sa.ForeignKeyConstraint(["service_visit_id"], ["service_visits.id"], ondelete="SET NULL"), sa.PrimaryKeyConstraint("id"), ) op.create_index("ix_inventory_transactions_actor_user_id", "inventory_transactions", ["actor_user_id"]) op.create_index("ix_inventory_transactions_created_at", "inventory_transactions", ["created_at"]) op.create_index("ix_inventory_transactions_product_item_id", "inventory_transactions", ["product_item_id"]) op.create_index("ix_inventory_transactions_service_center_id", "inventory_transactions", ["service_center_id"]) op.create_index("ix_inventory_transactions_service_visit_id", "inventory_transactions", ["service_visit_id"]) op.create_index("ix_inventory_transactions_sku", "inventory_transactions", ["sku"]) op.create_index("ix_inventory_transactions_transaction_type", "inventory_transactions", ["transaction_type"]) def downgrade() -> None: op.drop_index("ix_inventory_transactions_transaction_type", table_name="inventory_transactions") op.drop_index("ix_inventory_transactions_sku", table_name="inventory_transactions") op.drop_index("ix_inventory_transactions_service_visit_id", table_name="inventory_transactions") op.drop_index("ix_inventory_transactions_service_center_id", table_name="inventory_transactions") op.drop_index("ix_inventory_transactions_product_item_id", table_name="inventory_transactions") op.drop_index("ix_inventory_transactions_created_at", table_name="inventory_transactions") op.drop_index("ix_inventory_transactions_actor_user_id", table_name="inventory_transactions") op.drop_table("inventory_transactions") op.drop_index("ix_work_order_corrections_status", table_name="work_order_corrections") op.drop_index("ix_work_order_corrections_service_visit_id", table_name="work_order_corrections") op.drop_index("ix_work_order_corrections_created_at", table_name="work_order_corrections") op.drop_table("work_order_corrections") op.drop_index("uq_active_service_appointment_slot", table_name="service_appointments") op.drop_index("uq_expense_entries_service_visit_id_not_null", table_name="expense_entries") op.drop_index("uq_service_entries_service_visit_id_not_null", table_name="service_entries") op.drop_column("service_visits", "completed_snapshot") op.drop_column("service_visits", "version")