45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
"""Senior Field Executive: a field role like MR that may cover several headquarters
|
|
|
|
Revision ID: y5c1e7a3b0d2
|
|
Revises: x4b0d6f2a9c1
|
|
Create Date: 2026-10-27
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
|
|
from alembic import op
|
|
|
|
revision: str = "y5c1e7a3b0d2"
|
|
down_revision: str | None = "x4b0d6f2a9c1"
|
|
branch_labels: str | Sequence[str] | None = None
|
|
depends_on: str | Sequence[str] | None = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.drop_constraint("ck_user_mr_requires_hq", "user", type_="check")
|
|
op.create_check_constraint(
|
|
"ck_user_mr_requires_hq",
|
|
"user",
|
|
"(role IN ('MR', 'SFE') AND headquarters_id IS NOT NULL) "
|
|
"OR (role NOT IN ('MR', 'SFE') AND headquarters_id IS NULL)",
|
|
)
|
|
op.create_table(
|
|
"user_extra_headquarters",
|
|
sa.Column("user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="CASCADE"), primary_key=True),
|
|
sa.Column(
|
|
"headquarters_id", sa.UUID(), sa.ForeignKey("headquarters.id", ondelete="RESTRICT"), primary_key=True
|
|
),
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_table("user_extra_headquarters")
|
|
op.drop_constraint("ck_user_mr_requires_hq", "user", type_="check")
|
|
op.create_check_constraint(
|
|
"ck_user_mr_requires_hq",
|
|
"user",
|
|
"(role = 'MR' AND headquarters_id IS NOT NULL) OR (role <> 'MR' AND headquarters_id IS NULL)",
|
|
)
|