"""Add environments table and environment_id FK on tickets Revision ID: 0002 Revises: 0001 Create Date: 2026-05-25 """ from typing import Sequence, Union import sqlalchemy as sa from alembic import op revision: str = "0002" down_revision: Union[str, None] = "0001" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: op.create_table( "environments", sa.Column("id", sa.Integer(), primary_key=True), sa.Column("prefix", sa.String(16), nullable=False, unique=True), sa.Column("name", sa.String(64), nullable=False), sa.Column("description", sa.Text(), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()), ) op.execute("INSERT INTO environments (prefix, name) VALUES ('HOME', 'Homelab')") op.add_column("tickets", sa.Column("environment_id", sa.Integer(), sa.ForeignKey("environments.id", ondelete="SET NULL"), nullable=True)) op.execute( "UPDATE tickets SET environment_id = (SELECT id FROM environments WHERE prefix = 'HOME')" ) op.create_index("ix_tickets_environment_id", "tickets", ["environment_id"]) def downgrade() -> None: op.drop_index("ix_tickets_environment_id", table_name="tickets") op.drop_column("tickets", "environment_id") op.drop_table("environments")