|
| 1 | +""" |
| 2 | +Add billing tables for organization-scoped Stripe subscriptions. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + uv run python -m migrations.add_billing_tables .env |
| 6 | + uv run python -m migrations.add_billing_tables .env --apply |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +from dataclasses import dataclass |
| 13 | + |
| 14 | +from dotenv import load_dotenv |
| 15 | +from sqlalchemy import text |
| 16 | +from sqlmodel import Session, create_engine |
| 17 | + |
| 18 | +from utils.core.db import get_connection_url |
| 19 | + |
| 20 | +TABLES = ("organizationbilling", "stripewebhookevent") |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class MigrationStats: |
| 25 | + missing_tables: tuple[str, ...] = () |
| 26 | + all_present: bool = False |
| 27 | + |
| 28 | + |
| 29 | +def _table_exists(session: Session, table_name: str) -> bool: |
| 30 | + result = session.connection().execute( |
| 31 | + text( |
| 32 | + """ |
| 33 | + SELECT 1 |
| 34 | + FROM information_schema.tables |
| 35 | + WHERE table_schema = 'public' |
| 36 | + AND table_name = :table_name |
| 37 | + """ |
| 38 | + ), |
| 39 | + {"table_name": table_name}, |
| 40 | + ) |
| 41 | + return result.first() is not None |
| 42 | + |
| 43 | + |
| 44 | +def add_billing_tables(env_file: str, apply: bool) -> MigrationStats: |
| 45 | + load_dotenv(env_file, override=True) |
| 46 | + engine = create_engine(get_connection_url()) |
| 47 | + stats = MigrationStats() |
| 48 | + |
| 49 | + try: |
| 50 | + with Session(engine) as session: |
| 51 | + missing = tuple( |
| 52 | + table for table in TABLES if not _table_exists(session, table) |
| 53 | + ) |
| 54 | + stats.missing_tables = missing |
| 55 | + stats.all_present = not missing |
| 56 | + |
| 57 | + if apply and missing: |
| 58 | + session.connection().execute( |
| 59 | + text( |
| 60 | + """ |
| 61 | + CREATE TABLE IF NOT EXISTS organizationbilling ( |
| 62 | + id SERIAL PRIMARY KEY, |
| 63 | + organization_id INTEGER NOT NULL UNIQUE REFERENCES organization(id) ON DELETE CASCADE, |
| 64 | + stripe_customer_id VARCHAR, |
| 65 | + stripe_subscription_id VARCHAR, |
| 66 | + status VARCHAR NOT NULL DEFAULT 'none', |
| 67 | + price_id VARCHAR, |
| 68 | + current_period_start TIMESTAMP, |
| 69 | + current_period_end TIMESTAMP, |
| 70 | + last_payment_at TIMESTAMP, |
| 71 | + cancel_at_period_end BOOLEAN NOT NULL DEFAULT FALSE, |
| 72 | + created_at TIMESTAMP NOT NULL, |
| 73 | + updated_at TIMESTAMP NOT NULL |
| 74 | + ); |
| 75 | + CREATE INDEX IF NOT EXISTS ix_organizationbilling_organization_id |
| 76 | + ON organizationbilling (organization_id); |
| 77 | + CREATE INDEX IF NOT EXISTS ix_organizationbilling_stripe_customer_id |
| 78 | + ON organizationbilling (stripe_customer_id); |
| 79 | + CREATE INDEX IF NOT EXISTS ix_organizationbilling_stripe_subscription_id |
| 80 | + ON organizationbilling (stripe_subscription_id); |
| 81 | + CREATE INDEX IF NOT EXISTS ix_organizationbilling_status |
| 82 | + ON organizationbilling (status); |
| 83 | +
|
| 84 | + CREATE TABLE IF NOT EXISTS stripewebhookevent ( |
| 85 | + id SERIAL PRIMARY KEY, |
| 86 | + stripe_event_id VARCHAR NOT NULL UNIQUE, |
| 87 | + processed_at TIMESTAMP NOT NULL |
| 88 | + ); |
| 89 | + CREATE INDEX IF NOT EXISTS ix_stripewebhookevent_stripe_event_id |
| 90 | + ON stripewebhookevent (stripe_event_id); |
| 91 | + """ |
| 92 | + ) |
| 93 | + ) |
| 94 | + session.commit() |
| 95 | + else: |
| 96 | + session.rollback() |
| 97 | + finally: |
| 98 | + engine.dispose() |
| 99 | + |
| 100 | + return stats |
| 101 | + |
| 102 | + |
| 103 | +def main() -> None: |
| 104 | + parser = argparse.ArgumentParser(description=__doc__) |
| 105 | + parser.add_argument("env_file", help="Path to .env file") |
| 106 | + parser.add_argument( |
| 107 | + "--apply", |
| 108 | + action="store_true", |
| 109 | + help="Apply migration (default is dry-run)", |
| 110 | + ) |
| 111 | + args = parser.parse_args() |
| 112 | + stats = add_billing_tables(args.env_file, apply=args.apply) |
| 113 | + if stats.all_present: |
| 114 | + print("All billing tables already exist.") |
| 115 | + return |
| 116 | + print("Missing tables:", ", ".join(stats.missing_tables)) |
| 117 | + if args.apply: |
| 118 | + print("Migration applied.") |
| 119 | + else: |
| 120 | + print("Dry run only. Re-run with --apply to create tables.") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments