48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
"""subscriptions pilot complimentary backfill
|
|
|
|
This migration converts existing pilot/dev accounts to permanent complimentary
|
|
Pro per the self-serve signup spec section 5. Forward-only; downgrade is
|
|
prohibited because original status is not preserved.
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision: str = "c6cbfc534fad"
|
|
down_revision: Union[str, None] = "c982a3fc4bf1"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""Set status='complimentary' and plan='pro' for all existing accounts that
|
|
don't have a canceled or past_due subscription. Pilot users transition to
|
|
permanent complimentary Pro per spec section 5.
|
|
|
|
Forward-only — does not preserve original status values."""
|
|
conn = op.get_bind()
|
|
# Update existing rows
|
|
conn.execute(sa.text("""
|
|
UPDATE subscriptions
|
|
SET status = 'complimentary', plan = 'pro',
|
|
current_period_end = NULL, current_period_start = NULL,
|
|
updated_at = now()
|
|
WHERE status NOT IN ('canceled', 'past_due')
|
|
"""))
|
|
# Backfill: any account without a Subscription row gets one
|
|
conn.execute(sa.text("""
|
|
INSERT INTO subscriptions (id, account_id, plan, status, cancel_at_period_end, created_at, updated_at)
|
|
SELECT gen_random_uuid(), a.id, 'pro', 'complimentary', false, now(), now()
|
|
FROM accounts a
|
|
WHERE NOT EXISTS (SELECT 1 FROM subscriptions s WHERE s.account_id = a.id)
|
|
"""))
|
|
|
|
|
|
def downgrade() -> None:
|
|
raise RuntimeError(
|
|
"Cannot downgrade: original subscription state is not preserved. "
|
|
"Restore from backup if needed."
|
|
)
|