Production

This commit is contained in:
Shubhankar45
2026-09-22 06:23:46 +00:00
commit 8fdd20b29f
217 changed files with 31212 additions and 0 deletions
@@ -0,0 +1,77 @@
"""company forms belong to one MR, and so does their approval
The tour plan, standard tour plan and fare chart were one shared table with one shared
approval, so any Area Manager could sign for any MR. Each row now records its owner (the MR),
and an approval is kept per (form, MR) so it can only be requested from, and signed by, the
Area Manager who manages that MR's headquarters.
Revision ID: l2b8d4e0a7c9
Revises: k1a7c3d9f6b8
Create Date: 2026-09-21
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "l2b8d4e0a7c9"
down_revision: str | None = "k1a7c3d9f6b8"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
OWNED = ("tour_plan", "standard_tour_plan", "fare_chart")
def upgrade() -> None:
op.add_column("form_approval", sa.Column("owner_user_id", sa.UUID(), nullable=True))
# The MR who sent a form for approval is the one whose data it was.
op.execute("UPDATE form_approval SET owner_user_id = requested_by_user_id")
op.execute("DELETE FROM form_approval WHERE owner_user_id IS NULL")
op.drop_constraint("form_approval_pkey", "form_approval", type_="primary")
op.alter_column("form_approval", "owner_user_id", nullable=False)
op.create_primary_key("form_approval_pkey", "form_approval", ["table_name", "owner_user_id"])
op.create_foreign_key(
"fk_form_approval_owner", "form_approval", "user", ["owner_user_id"], ["id"], ondelete="CASCADE"
)
for table in OWNED:
op.add_column(table, sa.Column("owner_user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="SET NULL"), nullable=True))
op.create_index(f"ix_{table}_owner", table, ["owner_user_id"])
# Rows that existed belong to the MR who put the form up for approval, with that MR's identity.
op.execute(
f'''UPDATE "{table}" t
SET owner_user_id = a.owner_user_id, name = u.full_name, headquarter = h.name
FROM form_approval a
JOIN "user" u ON u.id = a.owner_user_id
LEFT JOIN headquarters h ON h.id = u.headquarters_id
WHERE a.table_name = '{table}' '''
)
# An approval by an Area Manager who does not manage the MR's headquarters was never valid:
# send it back to the right one.
op.execute(
'''UPDATE form_approval a
SET status = 'pending', approved_by_user_id = NULL, approved_by_name = NULL
FROM "user" u
WHERE u.id = a.owner_user_id AND a.status = 'approved'
AND NOT EXISTS (
SELECT 1 FROM am_headquarters m
WHERE m.headquarters_id = u.headquarters_id AND m.effective_to IS NULL
AND m.area_manager_user_id = a.approved_by_user_id)'''
)
def downgrade() -> None:
for table in OWNED:
op.drop_index(f"ix_{table}_owner", table_name=table)
op.drop_column(table, "owner_user_id")
op.drop_constraint("fk_form_approval_owner", "form_approval", type_="foreignkey")
op.drop_constraint("form_approval_pkey", "form_approval", type_="primary")
op.execute(
"DELETE FROM form_approval a USING form_approval b "
"WHERE a.table_name = b.table_name AND a.ctid < b.ctid"
)
op.drop_column("form_approval", "owner_user_id")
op.create_primary_key("form_approval_pkey", "form_approval", ["table_name"])