|
| 1 | +"""Add teams and team_members tables. |
| 2 | +
|
| 3 | +Revision ID: h9i0j1k2l3m4 |
| 4 | +Revises: g8h9i0j1k2l3 |
| 5 | +Create Date: 2026-05-01 |
| 6 | +""" |
| 7 | + |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from sqlalchemy.dialects.postgresql import UUID |
| 11 | + |
| 12 | + |
| 13 | +revision = "h9i0j1k2l3m4" |
| 14 | +down_revision = "g8h9i0j1k2l3" |
| 15 | +branch_labels = None |
| 16 | +depends_on = None |
| 17 | + |
| 18 | + |
| 19 | +def upgrade() -> None: |
| 20 | + op.create_table( |
| 21 | + "teams", |
| 22 | + sa.Column("id", UUID(as_uuid=True), nullable=False), |
| 23 | + sa.Column("user_id", UUID(as_uuid=True), nullable=False), |
| 24 | + sa.Column("name", sa.String(255), nullable=False), |
| 25 | + sa.Column("monthly_budget_usd", sa.Numeric(10, 2), nullable=False), |
| 26 | + sa.Column( |
| 27 | + "monthly_reset_policy", |
| 28 | + sa.String(20), |
| 29 | + nullable=False, |
| 30 | + server_default="reset", |
| 31 | + ), |
| 32 | + sa.Column("monthly_budget_start", sa.DateTime(), nullable=False), |
| 33 | + sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"), |
| 34 | + sa.Column("created_at", sa.DateTime(), nullable=False), |
| 35 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 36 | + sa.ForeignKeyConstraint(["user_id"], ["users.id"]), |
| 37 | + sa.PrimaryKeyConstraint("id"), |
| 38 | + ) |
| 39 | + op.create_index("ix_teams_user_id", "teams", ["user_id"]) |
| 40 | + |
| 41 | + op.create_table( |
| 42 | + "team_members", |
| 43 | + sa.Column("id", UUID(as_uuid=True), nullable=False), |
| 44 | + sa.Column("team_id", UUID(as_uuid=True), nullable=False), |
| 45 | + sa.Column("token_id", UUID(as_uuid=True), nullable=False), |
| 46 | + sa.Column( |
| 47 | + "allocated_usd", sa.Numeric(10, 2), nullable=False, server_default="0.00" |
| 48 | + ), |
| 49 | + sa.Column("created_at", sa.DateTime(), nullable=False), |
| 50 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 51 | + sa.ForeignKeyConstraint(["team_id"], ["teams.id"], ondelete="CASCADE"), |
| 52 | + sa.ForeignKeyConstraint(["token_id"], ["api_tokens.id"], ondelete="CASCADE"), |
| 53 | + sa.PrimaryKeyConstraint("id"), |
| 54 | + ) |
| 55 | + op.create_index("ix_team_members_team_id", "team_members", ["team_id"]) |
| 56 | + op.create_index( |
| 57 | + "ix_team_members_token_id", "team_members", ["token_id"], unique=True |
| 58 | + ) |
| 59 | + op.execute( |
| 60 | + "ALTER TABLE team_members " |
| 61 | + "ADD CONSTRAINT ck_allocated_non_negative CHECK (allocated_usd >= 0)" |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def downgrade() -> None: |
| 66 | + op.execute( |
| 67 | + "ALTER TABLE team_members DROP CONSTRAINT IF EXISTS ck_allocated_non_negative" |
| 68 | + ) |
| 69 | + op.drop_index("ix_team_members_token_id", "team_members") |
| 70 | + op.drop_index("ix_team_members_team_id", "team_members") |
| 71 | + op.drop_table("team_members") |
| 72 | + op.drop_index("ix_teams_user_id", "teams") |
| 73 | + op.drop_table("teams") |
0 commit comments