import sqlalchemy as sa from alembic import op from sqlalchemy.dialects.mysql import BIGINT, BINARY, DATETIME revision = "0004_fulfillment" down_revision = "0003_proposal_submitter" branch_labels = None depends_on = None def identifier(name, primary=False): return sa.Column(name, BINARY(16), primary_key=primary, nullable=False) def upgrade(): op.drop_constraint(op.f("ck_tickets_status"), "tickets", type_="check") op.create_check_constraint( op.f("ck_tickets_status"), "tickets", "status IN ('DRAFT','WAITING_REVIEW','APPROVED','REJECTED','CANCELLED'," "'PROCESSING','RESULT_UNKNOWN','COMPLETED','FAILED','NEEDS_REVISION')", ) op.add_column("business_events", sa.Column("lease_until", DATETIME(fsp=6), nullable=True)) op.add_column( "business_events", sa.Column("lease_token", BIGINT(unsigned=True), nullable=False, server_default="0"), ) op.create_table( "oms_connectors", identifier("tenant_id", True), identifier("store_id", True), sa.Column("source_system", sa.String(64), nullable=False), sa.Column("base_url", sa.String(512), nullable=False), sa.Column("credential_env", sa.String(128), nullable=False), sa.Column("enabled", sa.Boolean(), nullable=False), sa.ForeignKeyConstraint(["tenant_id", "store_id"], ["stores.tenant_id", "stores.id"]), ) op.create_table( "external_operations", identifier("id", True), identifier("tenant_id"), identifier("ticket_id"), identifier("store_id"), identifier("order_id"), sa.Column("proposal_version", BIGINT(unsigned=True), nullable=False), sa.Column("request", sa.JSON(), nullable=False), sa.Column("status", sa.String(32), nullable=False), sa.Column("external_version", BIGINT(unsigned=True), nullable=False), sa.Column("lease_token", BIGINT(unsigned=True), nullable=False), sa.Column("lease_until", DATETIME(fsp=6), nullable=True), sa.Column("next_attempt_at", DATETIME(fsp=6), nullable=False), sa.Column("last_error", sa.String(64), nullable=True), sa.Column("base_url", sa.String(512), nullable=False), sa.Column("credential_env", sa.String(128), nullable=False), sa.UniqueConstraint( "tenant_id", "ticket_id", "proposal_version", name="uq_operation_proposal" ), sa.UniqueConstraint("tenant_id", "id", name="uq_operation_tenant_id"), sa.ForeignKeyConstraint(["tenant_id", "ticket_id"], ["tickets.tenant_id", "tickets.id"]), sa.CheckConstraint( "status IN ('PREPARED','UNKNOWN','ACCEPTED','COMPLETED','FAILED')", name=op.f("ck_external_operations_status"), ), ) op.create_index("ix_operations_due", "external_operations", ["status", "next_attempt_at", "id"]) op.create_table( "reservations", identifier("operation_id", True), identifier("tenant_id"), identifier("order_id"), sa.Column("quantity", sa.Integer(), nullable=False), sa.Column("status", sa.String(16), nullable=False), sa.ForeignKeyConstraint( ["tenant_id", "operation_id"], ["external_operations.tenant_id", "external_operations.id"], ), sa.CheckConstraint("quantity > 0", name=op.f("ck_reservations_quantity")), sa.CheckConstraint( "status IN ('HELD','CONSUMED','RELEASED')", name=op.f("ck_reservations_status") ), ) op.create_index("ix_reservations_order", "reservations", ["tenant_id", "order_id", "status"]) op.create_table( "consumer_receipts", sa.Column("consumer", sa.String(64), primary_key=True), identifier("event_id", True), identifier("tenant_id"), sa.Column("processed_at", DATETIME(fsp=6), nullable=False), ) def downgrade(): op.drop_table("consumer_receipts") op.drop_table("reservations") op.drop_table("external_operations") op.drop_table("oms_connectors") op.drop_column("business_events", "lease_token") op.drop_column("business_events", "lease_until") op.drop_constraint(op.f("ck_tickets_status"), "tickets", type_="check") op.create_check_constraint( op.f("ck_tickets_status"), "tickets", "status IN ('DRAFT','WAITING_REVIEW','APPROVED','REJECTED','CANCELLED')", )