From 44ec84136e0be10d1bf1ae3a503e399ce90f997d Mon Sep 17 00:00:00 2001 From: Agent Zero Date: Wed, 19 Aug 2026 00:06:05 +0200 Subject: [PATCH] =?UTF-8?q?fix(ARCH-F-2):=20migration=200127=20=E2=80=94?= =?UTF-8?q?=20drop=20tasks=5Fcontact=5Fid=5Ffkey=20(contact=5Fid=20derived?= =?UTF-8?q?=20from=20entity=5Fid,=20FK=20redundant)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../versions/0127_drop_tasks_contact_id_fk.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 alembic/versions/0127_drop_tasks_contact_id_fk.py diff --git a/alembic/versions/0127_drop_tasks_contact_id_fk.py b/alembic/versions/0127_drop_tasks_contact_id_fk.py new file mode 100644 index 0000000..d6fd486 --- /dev/null +++ b/alembic/versions/0127_drop_tasks_contact_id_fk.py @@ -0,0 +1,36 @@ +"""Drop tasks_contact_id_fkey — contact_id is now derived from entity_id (ARCH-F-2). + +The tasks table has both a contact_id FK column (referencing contacts) and +polymorphic entity_type/entity_id columns. The code now derives contact_id +from entity_id when entity_type='contact', and stores NULL in the FK column. +The FK constraint is redundant and prevents creating tasks with arbitrary +entity references. This migration drops the FK constraint but keeps the column +for backward compatibility. + +Revision ID: 0127 +Revises: 0126 +""" + +from alembic import op + +revision = "0127" +down_revision = "0126" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # Drop the FK constraint on tasks.contact_id + op.drop_constraint("tasks_contact_id_fkey", "tasks", type_="foreignkey") + + +def downgrade() -> None: + # Re-create the FK constraint (best-effort — may fail if orphaned rows exist) + op.create_foreign_key( + "tasks_contact_id_fkey", + "tasks", + "contacts", + ["contact_id"], + ["id"], + ondelete="SET NULL", + )