"""Initial relations tables Revision ID: relations_0001 Revises: None Create Date: 2026-02-27 """ import sqlalchemy as sa from alembic import op revision = "relations_0001" down_revision = None branch_labels = None depends_on = None def _table_exists(conn, name): result = conn.execute(sa.text( "SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name=:t" ), {"t": name}) return result.scalar() is not None def upgrade(): if _table_exists(op.get_bind(), "container_relations"): return op.create_table( "container_relations", sa.Column("id", sa.Integer, primary_key=True, autoincrement=True), sa.Column("parent_type", sa.String(32), nullable=False), sa.Column("parent_id", sa.Integer, nullable=False), sa.Column("child_type", sa.String(32), nullable=False), sa.Column("child_id", sa.Integer, nullable=False), sa.Column("sort_order", sa.Integer, nullable=False, server_default="0"), sa.Column("label", sa.String(255), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.UniqueConstraint( "parent_type", "parent_id", "child_type", "child_id", name="uq_container_relations_parent_child", ), ) op.create_index("ix_container_relations_parent", "container_relations", ["parent_type", "parent_id"]) op.create_index("ix_container_relations_child", "container_relations", ["child_type", "child_id"]) def downgrade(): op.drop_table("container_relations")