"""business_entities Revision ID: 0002 Revises: 0001 Create Date: 2026-06-03 Adds 8 new tables for the Phase 4b business logic: - accounts, contacts, deals, activities (with polymorphic parent FKs) - notes, tags, tag_links (polymorphic parent, no DB FK) - deal_stage_history (audit trail of stage transitions) All tables include org_id for multi-tenant isolation (R-1) and deleted_at for soft-delete (R-1 default filter). """ from __future__ import annotations from collections.abc import Sequence from typing import Union import sqlalchemy as sa from alembic import op # revision identifiers, used by Alembic. revision: str = "0002" down_revision: Union[str, None] = "0001" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: # === accounts === op.create_table( "accounts", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("org_id", sa.Integer(), nullable=False), sa.Column("name", sa.String(length=255), nullable=False), sa.Column("website", sa.String(length=512), nullable=True), sa.Column("industry", sa.String(length=32), nullable=True), sa.Column("size", sa.String(length=32), nullable=True), sa.Column("address", sa.JSON(), nullable=True), sa.Column("owner_id", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["org_id"], ["orgs.id"], ondelete="CASCADE", name="fk_accounts_org_id"), sa.ForeignKeyConstraint(["owner_id"], ["users.id"], name="fk_accounts_owner_id"), ) op.create_index("ix_accounts_name", "accounts", ["name"]) op.create_index("ix_accounts_industry", "accounts", ["industry"]) op.create_index("ix_accounts_org_id", "accounts", ["org_id"]) op.create_index("ix_accounts_owner_id", "accounts", ["owner_id"]) op.create_index("ix_accounts_created_at", "accounts", ["created_at"]) op.create_index("ix_accounts_deleted_at", "accounts", ["deleted_at"]) # === contacts === op.create_table( "contacts", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("org_id", sa.Integer(), nullable=False), sa.Column("first_name", sa.String(length=128), nullable=False), sa.Column("last_name", sa.String(length=128), nullable=False), sa.Column("email", sa.String(length=255), nullable=True), sa.Column("phone", sa.String(length=64), nullable=True), sa.Column("account_id", sa.Integer(), nullable=True), sa.Column("owner_id", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["org_id"], ["orgs.id"], ondelete="CASCADE", name="fk_contacts_org_id"), sa.ForeignKeyConstraint(["account_id"], ["accounts.id"], name="fk_contacts_account_id"), sa.ForeignKeyConstraint(["owner_id"], ["users.id"], name="fk_contacts_owner_id"), ) op.create_index("ix_contacts_last_name", "contacts", ["last_name"]) op.create_index("ix_contacts_email", "contacts", ["email"]) op.create_index("ix_contacts_org_id", "contacts", ["org_id"]) op.create_index("ix_contacts_account_id", "contacts", ["account_id"]) op.create_index("ix_contacts_owner_id", "contacts", ["owner_id"]) op.create_index("ix_contacts_created_at", "contacts", ["created_at"]) op.create_index("ix_contacts_deleted_at", "contacts", ["deleted_at"]) # === deals === op.create_table( "deals", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("org_id", sa.Integer(), nullable=False), sa.Column("title", sa.String(length=255), nullable=False), sa.Column("value", sa.Numeric(12, 2), nullable=False, server_default="0"), sa.Column("currency", sa.String(length=3), nullable=False, server_default="EUR"), sa.Column("stage", sa.String(length=32), nullable=False, server_default="lead"), sa.Column("close_date", sa.Date(), nullable=True), sa.Column("account_id", sa.Integer(), nullable=False), sa.Column("owner_id", sa.Integer(), nullable=False), sa.Column("won_lost_reason", sa.String(length=512), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["org_id"], ["orgs.id"], ondelete="CASCADE", name="fk_deals_org_id"), sa.ForeignKeyConstraint(["account_id"], ["accounts.id"], name="fk_deals_account_id"), sa.ForeignKeyConstraint(["owner_id"], ["users.id"], name="fk_deals_owner_id"), ) op.create_index("ix_deals_stage", "deals", ["stage"]) op.create_index("ix_deals_org_id", "deals", ["org_id"]) op.create_index("ix_deals_account_id", "deals", ["account_id"]) op.create_index("ix_deals_owner_id", "deals", ["owner_id"]) op.create_index("ix_deals_created_at", "deals", ["created_at"]) op.create_index("ix_deals_deleted_at", "deals", ["deleted_at"]) # === activities === op.create_table( "activities", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("org_id", sa.Integer(), nullable=False), sa.Column("type", sa.String(length=32), nullable=False), sa.Column("subject", sa.String(length=255), nullable=False), sa.Column("body", sa.Text(), nullable=True), sa.Column("due_date", sa.DateTime(timezone=True), nullable=True), sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True), sa.Column("account_id", sa.Integer(), nullable=True), sa.Column("contact_id", sa.Integer(), nullable=True), sa.Column("deal_id", sa.Integer(), nullable=True), sa.Column("owner_id", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["org_id"], ["orgs.id"], ondelete="CASCADE", name="fk_activities_org_id"), sa.ForeignKeyConstraint(["account_id"], ["accounts.id"], name="fk_activities_account_id"), sa.ForeignKeyConstraint(["contact_id"], ["contacts.id"], name="fk_activities_contact_id"), sa.ForeignKeyConstraint(["deal_id"], ["deals.id"], name="fk_activities_deal_id"), sa.ForeignKeyConstraint(["owner_id"], ["users.id"], name="fk_activities_owner_id"), ) op.create_index("ix_activities_type", "activities", ["type"]) op.create_index("ix_activities_due_date", "activities", ["due_date"]) op.create_index("ix_activities_org_id", "activities", ["org_id"]) op.create_index("ix_activities_account_id", "activities", ["account_id"]) op.create_index("ix_activities_contact_id", "activities", ["contact_id"]) op.create_index("ix_activities_deal_id", "activities", ["deal_id"]) op.create_index("ix_activities_owner_id", "activities", ["owner_id"]) op.create_index("ix_activities_created_at", "activities", ["created_at"]) op.create_index("ix_activities_deleted_at", "activities", ["deleted_at"]) # === notes === op.create_table( "notes", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("org_id", sa.Integer(), nullable=False), sa.Column("body", sa.Text(), nullable=False), sa.Column("author_id", sa.Integer(), nullable=False), sa.Column("parent_type", sa.String(length=32), nullable=False), sa.Column("parent_id", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["org_id"], ["orgs.id"], ondelete="CASCADE", name="fk_notes_org_id"), sa.ForeignKeyConstraint(["author_id"], ["users.id"], name="fk_notes_author_id"), ) op.create_index("ix_notes_parent_type", "notes", ["parent_type"]) op.create_index("ix_notes_parent_id", "notes", ["parent_id"]) op.create_index("ix_notes_author_id", "notes", ["author_id"]) op.create_index("ix_notes_org_id", "notes", ["org_id"]) op.create_index("ix_notes_created_at", "notes", ["created_at"]) op.create_index("ix_notes_deleted_at", "notes", ["deleted_at"]) # === tags === op.create_table( "tags", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("org_id", sa.Integer(), nullable=False), sa.Column("name", sa.String(length=64), nullable=False), sa.Column("color", sa.String(length=7), nullable=False, server_default="#3B82F6"), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["org_id"], ["orgs.id"], ondelete="CASCADE", name="fk_tags_org_id"), ) op.create_index("ix_tags_name", "tags", ["name"]) op.create_index("ix_tags_org_id", "tags", ["org_id"]) op.create_index("ix_tags_created_at", "tags", ["created_at"]) op.create_index("ix_tags_deleted_at", "tags", ["deleted_at"]) # === tag_links === op.create_table( "tag_links", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("org_id", sa.Integer(), nullable=False), sa.Column("tag_id", sa.Integer(), nullable=False), sa.Column("parent_type", sa.String(length=32), nullable=False), sa.Column("parent_id", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["org_id"], ["orgs.id"], ondelete="CASCADE", name="fk_tag_links_org_id"), sa.ForeignKeyConstraint(["tag_id"], ["tags.id"], ondelete="CASCADE", name="fk_tag_links_tag_id"), sa.UniqueConstraint("tag_id", "parent_type", "parent_id", name="uq_tag_link"), ) op.create_index("ix_tag_links_tag_id", "tag_links", ["tag_id"]) op.create_index("ix_tag_links_parent_type", "tag_links", ["parent_type"]) op.create_index("ix_tag_links_parent_id", "tag_links", ["parent_id"]) op.create_index("ix_tag_links_org_id", "tag_links", ["org_id"]) op.create_index("ix_tag_links_created_at", "tag_links", ["created_at"]) op.create_index("ix_tag_links_deleted_at", "tag_links", ["deleted_at"]) # === deal_stage_history === op.create_table( "deal_stage_history", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("org_id", sa.Integer(), nullable=False), sa.Column("deal_id", sa.Integer(), nullable=False), sa.Column("from_stage", sa.String(length=32), nullable=True), sa.Column("to_stage", sa.String(length=32), nullable=False), sa.Column("changed_by", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.ForeignKeyConstraint(["org_id"], ["orgs.id"], ondelete="CASCADE", name="fk_dsh_org_id"), sa.ForeignKeyConstraint(["deal_id"], ["deals.id"], ondelete="CASCADE", name="fk_dsh_deal_id"), sa.ForeignKeyConstraint(["changed_by"], ["users.id"], name="fk_dsh_changed_by"), ) op.create_index("ix_dsh_deal_id", "deal_stage_history", ["deal_id"]) op.create_index("ix_dsh_org_id", "deal_stage_history", ["org_id"]) op.create_index("ix_dsh_created_at", "deal_stage_history", ["created_at"]) def downgrade() -> None: op.drop_index("ix_dsh_created_at", table_name="deal_stage_history") op.drop_index("ix_dsh_org_id", table_name="deal_stage_history") op.drop_index("ix_dsh_deal_id", table_name="deal_stage_history") op.drop_table("deal_stage_history") op.drop_index("ix_tag_links_deleted_at", table_name="tag_links") op.drop_index("ix_tag_links_created_at", table_name="tag_links") op.drop_index("ix_tag_links_org_id", table_name="tag_links") op.drop_index("ix_tag_links_parent_id", table_name="tag_links") op.drop_index("ix_tag_links_parent_type", table_name="tag_links") op.drop_index("ix_tag_links_tag_id", table_name="tag_links") op.drop_table("tag_links") op.drop_index("ix_tags_deleted_at", table_name="tags") op.drop_index("ix_tags_created_at", table_name="tags") op.drop_index("ix_tags_org_id", table_name="tags") op.drop_index("ix_tags_name", table_name="tags") op.drop_table("tags") op.drop_index("ix_notes_deleted_at", table_name="notes") op.drop_index("ix_notes_created_at", table_name="notes") op.drop_index("ix_notes_org_id", table_name="notes") op.drop_index("ix_notes_author_id", table_name="notes") op.drop_index("ix_notes_parent_id", table_name="notes") op.drop_index("ix_notes_parent_type", table_name="notes") op.drop_table("notes") op.drop_index("ix_activities_deleted_at", table_name="activities") op.drop_index("ix_activities_created_at", table_name="activities") op.drop_index("ix_activities_owner_id", table_name="activities") op.drop_index("ix_activities_deal_id", table_name="activities") op.drop_index("ix_activities_contact_id", table_name="activities") op.drop_index("ix_activities_account_id", table_name="activities") op.drop_index("ix_activities_org_id", table_name="activities") op.drop_index("ix_activities_due_date", table_name="activities") op.drop_index("ix_activities_type", table_name="activities") op.drop_table("activities") op.drop_index("ix_deals_deleted_at", table_name="deals") op.drop_index("ix_deals_created_at", table_name="deals") op.drop_index("ix_deals_owner_id", table_name="deals") op.drop_index("ix_deals_account_id", table_name="deals") op.drop_index("ix_deals_org_id", table_name="deals") op.drop_index("ix_deals_stage", table_name="deals") op.drop_table("deals") op.drop_index("ix_contacts_deleted_at", table_name="contacts") op.drop_index("ix_contacts_created_at", table_name="contacts") op.drop_index("ix_contacts_owner_id", table_name="contacts") op.drop_index("ix_contacts_account_id", table_name="contacts") op.drop_index("ix_contacts_org_id", table_name="contacts") op.drop_index("ix_contacts_email", table_name="contacts") op.drop_index("ix_contacts_last_name", table_name="contacts") op.drop_table("contacts") op.drop_index("ix_accounts_deleted_at", table_name="accounts") op.drop_index("ix_accounts_created_at", table_name="accounts") op.drop_index("ix_accounts_owner_id", table_name="accounts") op.drop_index("ix_accounts_org_id", table_name="accounts") op.drop_index("ix_accounts_industry", table_name="accounts") op.drop_index("ix_accounts_name", table_name="accounts") op.drop_table("accounts")