T01: core infrastructure + auth + multi-tenant + RLS

- 10 models: tenants, users, user_tenants, roles, sessions, audit_log, deletion_log, notifications, password_reset_tokens, api_tokens
- Session-based auth (Redis + PostgreSQL audit trail)
- Multi-tenant with ORM-level filtering + PostgreSQL RLS (set_config)
- RBAC with roles/permissions + field-level permissions
- CSRF protection via Origin header validation
- Auth rate limiting (Redis counters with TTL)
- CORS with explicit origins (no wildcard)
- Health endpoint (no auth required)
- Notification service + audit log middleware
- 29 tests, 26 ACs, all passing
- Coverage: 62% (infrastructure modules pending coverage in later tasks)
This commit is contained in:
leocrm-bot
2026-06-29 00:10:10 +02:00
parent 6520e88d53
commit 3ab4925783
137 changed files with 3866 additions and 10195 deletions
-103
View File
@@ -1,103 +0,0 @@
"""init_orgs_and_users
Revision ID: 0001
Revises:
Create Date: 2026-06-03
"""
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 = "0001"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# === orgs ===
op.create_table(
"orgs",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("logo_url", sa.String(length=1024), nullable=True),
sa.Column(
"default_currency",
sa.String(length=3),
nullable=False,
server_default="EUR",
),
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,
),
)
op.create_index("ix_orgs_created_at", "orgs", ["created_at"])
# === users ===
op.create_table(
"users",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("org_id", sa.Integer(), nullable=False),
sa.Column("email", sa.String(length=255), nullable=False),
sa.Column("password_hash", sa.String(length=255), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column(
"role",
sa.String(length=32),
nullable=False,
server_default="sales_rep",
),
sa.Column("avatar_url", sa.String(length=1024), nullable=True),
sa.Column(
"email_notifications",
sa.Boolean(),
nullable=False,
server_default=sa.text("1"),
),
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_users_org_id"
),
sa.UniqueConstraint("org_id", "email", name="uq_users_org_email"),
)
op.create_index("ix_users_email", "users", ["email"])
op.create_index("ix_users_org_id", "users", ["org_id"])
op.create_index("ix_users_created_at", "users", ["created_at"])
op.create_index("ix_users_deleted_at", "users", ["deleted_at"])
def downgrade() -> None:
op.drop_index("ix_users_deleted_at", table_name="users")
op.drop_index("ix_users_created_at", table_name="users")
op.drop_index("ix_users_org_id", table_name="users")
op.drop_index("ix_users_email", table_name="users")
op.drop_table("users")
op.drop_index("ix_orgs_created_at", table_name="orgs")
op.drop_table("orgs")
+198
View File
@@ -0,0 +1,198 @@
"""Initial migration - all core tables.
Revision ID: 0001_initial
Revises:
Create Date: 2026-06-28
"""
from __future__ import annotations
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
revision: str = "0001_initial"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# tenants
op.create_table(
"tenants",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("name", sa.String(200), nullable=False),
sa.Column("slug", sa.String(100), nullable=False, unique=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_tenants_slug", "tenants", ["slug"])
# users
op.create_table(
"users",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("email", sa.String(255), nullable=False),
sa.Column("name", sa.String(200), nullable=False),
sa.Column("password_hash", sa.String(255), nullable=False),
sa.Column("role", sa.String(50), nullable=False, server_default="viewer"),
sa.Column("is_active", sa.Boolean, nullable=False, server_default=sa.true()),
sa.Column("preferences", postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.UniqueConstraint("tenant_id", "email", name="uq_users_tenant_email"),
)
op.create_index("ix_users_tenant_id", "users", ["tenant_id"])
op.create_index("ix_users_email", "users", ["email"])
# user_tenants
op.create_table(
"user_tenants",
sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), primary_key=True),
sa.Column("is_default", sa.Boolean, nullable=False, server_default=sa.false()),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
# roles
op.create_table(
"roles",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("name", sa.String(100), nullable=False),
sa.Column("permissions", postgresql.JSONB, nullable=False),
sa.Column("field_permissions", postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_roles_tenant_id", "roles", ["tenant_id"])
# sessions
op.create_table(
"sessions",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
sa.Column("csrf_token", sa.String(255), nullable=False),
sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_sessions_tenant_id", "sessions", ["tenant_id"])
op.create_index("ix_sessions_user_id", "sessions", ["user_id"])
# audit_log
op.create_table(
"audit_log",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
sa.Column("action", sa.String(50), nullable=False),
sa.Column("entity_type", sa.String(50), nullable=False),
sa.Column("entity_id", postgresql.UUID(as_uuid=True), nullable=True),
sa.Column("changes", postgresql.JSONB, nullable=True),
sa.Column("timestamp", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_audit_log_tenant_id", "audit_log", ["tenant_id"])
op.create_index("ix_audit_log_entity_type", "audit_log", ["entity_type"])
op.create_index("ix_audit_log_user_id", "audit_log", ["user_id"])
op.create_index("ix_audit_log_timestamp", "audit_log", ["timestamp"])
# deletion_log
op.create_table(
"deletion_log",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
sa.Column("entity_type", sa.String(50), nullable=False),
sa.Column("entity_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("entity_snapshot", postgresql.JSONB, nullable=False),
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
# notifications
op.create_table(
"notifications",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
sa.Column("type", sa.String(20), nullable=False),
sa.Column("title", sa.String(200), nullable=False),
sa.Column("body", sa.Text, nullable=True),
sa.Column("read_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_notifications_tenant_id", "notifications", ["tenant_id"])
op.create_index("ix_notifications_user_id", "notifications", ["user_id"])
op.create_index("ix_notifications_tenant_user_read", "notifications", ["tenant_id", "user_id", "read_at"])
# password_reset_tokens
op.create_table(
"password_reset_tokens",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
sa.Column("token_hash", sa.String(255), nullable=False),
sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("used_at", sa.DateTime(timezone=True), nullable=True),
)
op.create_index("ix_password_reset_tokens_tenant_id", "password_reset_tokens", ["tenant_id"])
op.create_index("ix_password_reset_tokens_user_id", "password_reset_tokens", ["user_id"])
op.create_index("ix_password_reset_tokens_token_hash", "password_reset_tokens", ["token_hash"])
# api_tokens
op.create_table(
"api_tokens",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
sa.Column("token_hash", sa.String(255), nullable=False),
sa.Column("name", sa.String(200), nullable=False),
sa.Column("scopes", postgresql.JSONB, nullable=False),
sa.Column("expires_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("last_used_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("revoked_at", sa.DateTime(timezone=True), nullable=True),
)
op.create_index("ix_api_tokens_tenant_id", "api_tokens", ["tenant_id"])
op.create_index("ix_api_tokens_token_hash", "api_tokens", ["token_hash"])
op.create_index("ix_api_tokens_tenant_user", "api_tokens", ["tenant_id", "user_id"])
# companies
op.create_table(
"companies",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("tenant_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False),
sa.Column("name", sa.String(100), nullable=False),
sa.Column("account_number", sa.String(40), nullable=True),
sa.Column("industry", sa.String(50), nullable=True),
sa.Column("phone", sa.String(30), nullable=True),
sa.Column("email", sa.String(255), nullable=True),
sa.Column("website", sa.String(500), nullable=True),
sa.Column("description", sa.Text, nullable=True),
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_by", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
sa.Column("updated_by", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_companies_tenant_id", "companies", ["tenant_id"])
op.create_index("ix_companies_tenant_deleted", "companies", ["tenant_id", "deleted_at"])
op.create_index("ix_companies_tenant_name", "companies", ["tenant_id", "name"])
# Enable RLS on tenant-scoped tables
for table in ["companies", "users", "roles", "sessions", "audit_log", "notifications", "api_tokens"]:
op.execute(f"ALTER TABLE {table} ENABLE ROW LEVEL SECURITY;")
op.execute(
f"CREATE POLICY tenant_isolation ON {table} "
f"USING (tenant_id = current_setting('app.current_tenant_id')::uuid);"
)
def downgrade() -> None:
for table in ["companies", "api_tokens", "password_reset_tokens", "notifications",
"deletion_log", "audit_log", "sessions", "roles", "user_tenants",
"users", "tenants"]:
op.drop_table(table)
-285
View File
@@ -1,285 +0,0 @@
"""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")