agilentics / boiler
"""subscriptions: what an organisation pays for

One row per organisation, enforced by a named unique constraint rather than a
column-level `unique=True`. The difference matters to `alembic check`: a
column-level flag produces a second, unnamed unique index, which the model and
this migration would then disagree about forever.

Nothing is backfilled. `plans.subscription_for` creates a free row the first time
an organisation's plan is read, so an org that predates billing gets one on
demand rather than needing a data migration that a fresh database would run
against no rows at all.

Revision ID: 0004
Revises: 0003
Create Date: 2026-01-04
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "0004"
down_revision = "0003"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "subscriptions",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("org_id", sa.Integer(), nullable=False),
        sa.Column("tier", sa.String(length=20), nullable=False),
        sa.Column("status", sa.String(length=20), nullable=False),
        sa.Column("stripe_customer_id", sa.String(length=255), nullable=True),
        sa.Column("stripe_subscription_id", sa.String(length=255), nullable=True),
        sa.Column("stripe_price_id", sa.String(length=255), nullable=True),
        sa.Column("current_period_end", sa.DateTime(), nullable=True),
        sa.Column("cancel_at_period_end", sa.Boolean(),
                  server_default=sa.false(), nullable=False),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(),
                  nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(),
                  nullable=False),
        # CASCADE: a deleted organisation takes its billing row with it. What it
        # does not take is the Stripe customer - see billing.cancel_everything,
        # which has to run before the tenant is deleted, because after it there
        # is no row left to say which Stripe customer to end.
        sa.ForeignKeyConstraint(["org_id"], ["organizations.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("org_id", name="uq_subscription_org"),
    )
    op.create_index("ix_subscriptions_org_id", "subscriptions", ["org_id"])
    # The webhook resolves a row by Stripe's ids on every event, and Stripe
    # retries. Both are looked up by equality and neither is the primary key.
    op.create_index("ix_subscriptions_stripe_customer_id", "subscriptions",
                    ["stripe_customer_id"])
    op.create_index("ix_subscriptions_stripe_subscription_id", "subscriptions",
                    ["stripe_subscription_id"])


def downgrade() -> None:
    op.drop_index("ix_subscriptions_stripe_subscription_id",
                  table_name="subscriptions")
    op.drop_index("ix_subscriptions_stripe_customer_id", table_name="subscriptions")
    op.drop_index("ix_subscriptions_org_id", table_name="subscriptions")
    op.drop_table("subscriptions")