"""Create permission_delegations table. Revision ID: 0057 Revises: 0056 Create Date: 2026-07-29 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects.postgresql import JSONB, UUID as PGUUID revision = "0057" down_revision = "0056" branch_labels = None depends_on = None def upgrade() -> None: op.create_table( "permission_delegations", sa.Column("id", PGUUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), sa.Column("from_user_id", PGUUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False), sa.Column("to_user_id", PGUUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False), sa.Column("start_at", sa.DateTime(timezone=True), nullable=False), sa.Column("end_at", sa.DateTime(timezone=True), nullable=False), sa.Column("scope", JSONB, nullable=True), sa.Column("active", sa.Boolean, nullable=False, server_default=sa.text("true")), sa.Column("tenant_id", PGUUID(as_uuid=True), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")), sa.CheckConstraint( "end_at > start_at", name="ck_pd_end_after_start", ), ) op.execute('CREATE INDEX IF NOT EXISTS ix_pd_from_user ON permission_delegations (from_user_id)') op.execute('CREATE INDEX IF NOT EXISTS ix_pd_to_user ON permission_delegations (to_user_id)') op.execute('CREATE INDEX IF NOT EXISTS ix_pd_tenant ON permission_delegations (tenant_id)') op.execute('CREATE INDEX IF NOT EXISTS ix_pd_active ON permission_delegations (active)') def downgrade() -> None: op.drop_index("ix_pd_active", table_name="permission_delegations") op.drop_index("ix_pd_tenant", table_name="permission_delegations") op.drop_index("ix_pd_to_user", table_name="permission_delegations") op.drop_index("ix_pd_from_user", table_name="permission_delegations") op.drop_table("permission_delegations")