2026-06-29 00:10:10 +02:00
|
|
|
"""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()),
|
|
|
|
|
)
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_tenants_slug ON tenants (slug)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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"),
|
|
|
|
|
)
|
2026-07-31 18:20:09 +02:00
|
|
|
op.execute("CREATE INDEX IF NOT EXISTS ix_users_tenant_id ON users (tenant_id)")
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_users_email ON users (email)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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()),
|
|
|
|
|
)
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_roles_tenant_id ON roles (tenant_id)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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()),
|
|
|
|
|
)
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_sessions_tenant_id ON sessions (tenant_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_sessions_user_id ON sessions (user_id)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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()),
|
|
|
|
|
)
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_audit_log_tenant_id ON audit_log (tenant_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_audit_log_entity_type ON audit_log (entity_type)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_audit_log_user_id ON audit_log (user_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_audit_log_timestamp ON audit_log (timestamp)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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()),
|
|
|
|
|
)
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_notifications_tenant_id ON notifications (tenant_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_notifications_user_id ON notifications (user_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_notifications_tenant_user_read ON notifications (tenant_id, user_id, read_at)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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),
|
|
|
|
|
)
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_password_reset_tokens_tenant_id ON password_reset_tokens (tenant_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_password_reset_tokens_user_id ON password_reset_tokens (user_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_password_reset_tokens_token_hash ON password_reset_tokens (token_hash)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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),
|
|
|
|
|
)
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_api_tokens_tenant_id ON api_tokens (tenant_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_api_tokens_token_hash ON api_tokens (token_hash)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_api_tokens_tenant_user ON api_tokens (tenant_id, user_id)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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()),
|
|
|
|
|
)
|
2026-07-31 19:16:11 +02:00
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_companies_tenant_id ON companies (tenant_id)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_companies_tenant_deleted ON companies (tenant_id, deleted_at)')
|
|
|
|
|
op.execute('CREATE INDEX IF NOT EXISTS ix_companies_tenant_name ON companies (tenant_id, name)')
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
# 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)
|