|
| 1 | +"""add premium token quota columns and purchase table |
| 2 | +
|
| 3 | +Revision ID: 126 |
| 4 | +Revises: 125 |
| 5 | +Create Date: 2026-04-15 |
| 6 | +
|
| 7 | +Adds premium_tokens_limit, premium_tokens_used, premium_tokens_reserved |
| 8 | +to the user table and creates the premium_token_purchases table. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import os |
| 14 | +from collections.abc import Sequence |
| 15 | + |
| 16 | +import sqlalchemy as sa |
| 17 | +from sqlalchemy.dialects import postgresql |
| 18 | + |
| 19 | +from alembic import op |
| 20 | + |
| 21 | +revision: str = "126" |
| 22 | +down_revision: str | None = "125" |
| 23 | +branch_labels: str | Sequence[str] | None = None |
| 24 | +depends_on: str | Sequence[str] | None = None |
| 25 | + |
| 26 | +PREMIUM_TOKEN_LIMIT_DEFAULT = os.getenv("PREMIUM_TOKEN_LIMIT", "5000000") |
| 27 | + |
| 28 | + |
| 29 | +def upgrade() -> None: |
| 30 | + conn = op.get_bind() |
| 31 | + |
| 32 | + # --- User table: add premium token columns if missing --- |
| 33 | + inspector = sa.inspect(conn) |
| 34 | + user_columns = {c["name"] for c in inspector.get_columns("user")} |
| 35 | + |
| 36 | + if "premium_tokens_limit" not in user_columns: |
| 37 | + op.add_column( |
| 38 | + "user", |
| 39 | + sa.Column( |
| 40 | + "premium_tokens_limit", |
| 41 | + sa.BigInteger(), |
| 42 | + nullable=False, |
| 43 | + server_default=PREMIUM_TOKEN_LIMIT_DEFAULT, |
| 44 | + ), |
| 45 | + ) |
| 46 | + if "premium_tokens_used" not in user_columns: |
| 47 | + op.add_column( |
| 48 | + "user", |
| 49 | + sa.Column( |
| 50 | + "premium_tokens_used", |
| 51 | + sa.BigInteger(), |
| 52 | + nullable=False, |
| 53 | + server_default="0", |
| 54 | + ), |
| 55 | + ) |
| 56 | + if "premium_tokens_reserved" not in user_columns: |
| 57 | + op.add_column( |
| 58 | + "user", |
| 59 | + sa.Column( |
| 60 | + "premium_tokens_reserved", |
| 61 | + sa.BigInteger(), |
| 62 | + nullable=False, |
| 63 | + server_default="0", |
| 64 | + ), |
| 65 | + ) |
| 66 | + |
| 67 | + # --- PremiumTokenPurchase enum + table --- |
| 68 | + enum_exists = conn.execute( |
| 69 | + sa.text("SELECT 1 FROM pg_type WHERE typname = 'premiumtokenpurchasestatus'") |
| 70 | + ).fetchone() |
| 71 | + if not enum_exists: |
| 72 | + purchase_status_enum = postgresql.ENUM( |
| 73 | + "PENDING", |
| 74 | + "COMPLETED", |
| 75 | + "FAILED", |
| 76 | + name="premiumtokenpurchasestatus", |
| 77 | + create_type=False, |
| 78 | + ) |
| 79 | + purchase_status_enum.create(conn, checkfirst=True) |
| 80 | + |
| 81 | + if not inspector.has_table("premium_token_purchases"): |
| 82 | + op.create_table( |
| 83 | + "premium_token_purchases", |
| 84 | + sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), |
| 85 | + sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False), |
| 86 | + sa.Column( |
| 87 | + "stripe_checkout_session_id", |
| 88 | + sa.String(length=255), |
| 89 | + nullable=False, |
| 90 | + ), |
| 91 | + sa.Column( |
| 92 | + "stripe_payment_intent_id", |
| 93 | + sa.String(length=255), |
| 94 | + nullable=True, |
| 95 | + ), |
| 96 | + sa.Column("quantity", sa.Integer(), nullable=False), |
| 97 | + sa.Column("tokens_granted", sa.BigInteger(), nullable=False), |
| 98 | + sa.Column("amount_total", sa.Integer(), nullable=True), |
| 99 | + sa.Column("currency", sa.String(length=10), nullable=True), |
| 100 | + sa.Column( |
| 101 | + "status", |
| 102 | + postgresql.ENUM( |
| 103 | + "PENDING", |
| 104 | + "COMPLETED", |
| 105 | + "FAILED", |
| 106 | + name="premiumtokenpurchasestatus", |
| 107 | + create_type=False, |
| 108 | + ), |
| 109 | + nullable=False, |
| 110 | + server_default=sa.text("'PENDING'::premiumtokenpurchasestatus"), |
| 111 | + ), |
| 112 | + sa.Column("completed_at", sa.TIMESTAMP(timezone=True), nullable=True), |
| 113 | + sa.Column( |
| 114 | + "created_at", |
| 115 | + sa.TIMESTAMP(timezone=True), |
| 116 | + server_default=sa.text("now()"), |
| 117 | + nullable=False, |
| 118 | + ), |
| 119 | + sa.Column( |
| 120 | + "updated_at", |
| 121 | + sa.TIMESTAMP(timezone=True), |
| 122 | + server_default=sa.text("now()"), |
| 123 | + nullable=False, |
| 124 | + ), |
| 125 | + sa.ForeignKeyConstraint( |
| 126 | + ["user_id"], |
| 127 | + ["user.id"], |
| 128 | + ondelete="CASCADE", |
| 129 | + ), |
| 130 | + sa.PrimaryKeyConstraint("id"), |
| 131 | + sa.UniqueConstraint( |
| 132 | + "stripe_checkout_session_id", |
| 133 | + name="uq_premium_token_purchases_stripe_checkout_session_id", |
| 134 | + ), |
| 135 | + ) |
| 136 | + |
| 137 | + op.execute( |
| 138 | + "CREATE INDEX IF NOT EXISTS ix_premium_token_purchases_user_id " |
| 139 | + "ON premium_token_purchases (user_id)" |
| 140 | + ) |
| 141 | + op.execute( |
| 142 | + "CREATE UNIQUE INDEX IF NOT EXISTS ix_premium_token_purchases_stripe_session " |
| 143 | + "ON premium_token_purchases (stripe_checkout_session_id)" |
| 144 | + ) |
| 145 | + op.execute( |
| 146 | + "CREATE INDEX IF NOT EXISTS ix_premium_token_purchases_payment_intent " |
| 147 | + "ON premium_token_purchases (stripe_payment_intent_id)" |
| 148 | + ) |
| 149 | + op.execute( |
| 150 | + "CREATE INDEX IF NOT EXISTS ix_premium_token_purchases_status " |
| 151 | + "ON premium_token_purchases (status)" |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +def downgrade() -> None: |
| 156 | + op.execute("DROP INDEX IF EXISTS ix_premium_token_purchases_status") |
| 157 | + op.execute("DROP INDEX IF EXISTS ix_premium_token_purchases_payment_intent") |
| 158 | + op.execute("DROP INDEX IF EXISTS ix_premium_token_purchases_stripe_session") |
| 159 | + op.execute("DROP INDEX IF EXISTS ix_premium_token_purchases_user_id") |
| 160 | + op.execute("DROP TABLE IF EXISTS premium_token_purchases") |
| 161 | + postgresql.ENUM(name="premiumtokenpurchasestatus").drop( |
| 162 | + op.get_bind(), checkfirst=True |
| 163 | + ) |
| 164 | + op.drop_column("user", "premium_tokens_reserved") |
| 165 | + op.drop_column("user", "premium_tokens_used") |
| 166 | + op.drop_column("user", "premium_tokens_limit") |
0 commit comments