"""add budgets and budget alerts tables Revision ID: add_budget_tables Revises: add_model_capability_tables Create Date: 2026-02-25 """ from alembic import op import sqlalchemy as sa revision = "add_budget_tables" down_revision = "add_model_capability_tables" branch_labels = None depends_on = None def _table_exists(table_name: str) -> bool: bind = op.get_bind() inspector = sa.inspect(bind) return table_name in inspector.get_table_names() def upgrade(): if not _table_exists("budgets"): op.create_table( "budgets", sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False), sa.Column("budget_type", sa.String(length=20), nullable=False), sa.Column("project_id", sa.BigInteger(), nullable=True), sa.Column("period", sa.String(length=20), nullable=False), sa.Column("budget_amount_usd", sa.DECIMAL(12, 2), nullable=False, server_default="0"), sa.Column("alert_threshold_percent", sa.Float(), nullable=False, server_default="80"), sa.Column("hard_limit_enabled", sa.Boolean(), nullable=False, server_default=sa.true()), sa.Column("current_usage_usd", sa.DECIMAL(12, 2), nullable=False, server_default="0"), sa.Column("current_period_start", sa.TIMESTAMP(), nullable=True), sa.Column("current_period_end", sa.TIMESTAMP(), nullable=True), sa.Column("status", sa.String(length=20), nullable=False, server_default="active"), sa.Column("override_history", sa.JSON(), nullable=False), sa.Column("created_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()), sa.Column("updated_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()), sa.ForeignKeyConstraint(["project_id"], ["projects.id"]), ) op.create_index("ix_budgets_tenant_id", "budgets", ["tenant_id"]) op.create_index("ix_budgets_budget_type", "budgets", ["budget_type"]) op.create_index("ix_budgets_project_id", "budgets", ["project_id"]) if not _table_exists("budget_alerts"): op.create_table( "budget_alerts", sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False), sa.Column("budget_id", sa.BigInteger(), nullable=False), sa.Column("alert_type", sa.String(length=50), nullable=False), sa.Column("usage_usd", sa.DECIMAL(12, 2), nullable=False, server_default="0"), sa.Column("usage_percent", sa.Float(), nullable=False, server_default="0"), sa.Column("message", sa.Text(), nullable=False), sa.Column("is_read", sa.Boolean(), nullable=False, server_default=sa.false()), sa.Column("action_taken", sa.String(length=50), nullable=True), sa.Column("status", sa.String(length=20), nullable=False, server_default="active"), sa.Column("created_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()), sa.Column("updated_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()), sa.ForeignKeyConstraint(["budget_id"], ["budgets.id"]), ) op.create_index("ix_budget_alerts_tenant_id", "budget_alerts", ["tenant_id"]) op.create_index("ix_budget_alerts_budget_id", "budget_alerts", ["budget_id"]) op.create_index("ix_budget_alerts_alert_type", "budget_alerts", ["alert_type"]) def downgrade(): if _table_exists("budget_alerts"): op.drop_index("ix_budget_alerts_alert_type", table_name="budget_alerts") op.drop_index("ix_budget_alerts_budget_id", table_name="budget_alerts") op.drop_index("ix_budget_alerts_tenant_id", table_name="budget_alerts") op.drop_table("budget_alerts") if _table_exists("budgets"): op.drop_index("ix_budgets_project_id", table_name="budgets") op.drop_index("ix_budgets_budget_type", table_name="budgets") op.drop_index("ix_budgets_tenant_id", table_name="budgets") op.drop_table("budgets")