""" Initial tenant management migration. Revision ID: 001 Revises: Create Date: 2026-07-03 03:00:00.000000 """ from typing import Sequence, Union from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. revision: str = "001" down_revision: Union[str, Sequence[str], None] = None branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: # ─── Enums ────────────────────────────────────────────────────────────── tenant_type_enum = postgresql.ENUM( 'municipality', 'company', 'organization', 'region', 'association', name='tenant_type_enum', create_type=True ) tenant_type_enum.create(op.get_bind(), checkfirst=True) tenant_status_enum = postgresql.ENUM( 'pending', 'active', 'suspended', 'trial', 'cancelled', name='tenant_status_enum', create_type=True ) tenant_status_enum.create(op.get_bind(), checkfirst=True) audit_action_enum = postgresql.ENUM( 'CREATE', 'READ', 'UPDATE', 'DELETE', 'ACTIVATE', 'SUSPEND', 'CANCEL', 'LOGIN', 'LOGOUT', 'EXPORT', 'IMPORT', 'SCHEMA_CREATE', 'SCHEMA_DROP', name='audit_action_enum', create_type=True ) audit_action_enum.create(op.get_bind(), checkfirst=True) user_role_enum = postgresql.ENUM( 'admin', 'manager', 'analyst', 'viewer', name='userrole', create_type=True ) user_role_enum.create(op.get_bind(), checkfirst=True) user_status_enum = postgresql.ENUM( 'active', 'inactive', 'pending', name='userstatus', create_type=True ) user_status_enum.create(op.get_bind(), checkfirst=True) # ─── Tenants table ────────────────────────────────────────────────────── op.create_table( 'tenants', sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('created_at', sa.DateTime(timezone=True), nullable=True), sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), sa.Column('name', sa.String(length=255), nullable=False), sa.Column('slug', sa.String(length=100), nullable=False), sa.Column('org_number', sa.String(length=20), nullable=True), sa.Column('tenant_type', sa.Enum('municipality', 'company', 'organization', 'region', 'association', name='tenant_type_enum'), nullable=False), sa.Column('status', sa.Enum('pending', 'active', 'suspended', 'trial', 'cancelled', name='tenant_status_enum'), nullable=False), sa.Column('schema_name', sa.String(length=100), nullable=False), sa.Column('contact_email', sa.String(length=255), nullable=False), sa.Column('contact_phone', sa.String(length=50), nullable=True), sa.Column('billing_email', sa.String(length=255), nullable=True), sa.Column('address', sa.String(length=255), nullable=True), sa.Column('postal_code', sa.String(length=20), nullable=True), sa.Column('city', sa.String(length=100), nullable=True), sa.Column('country', sa.String(length=2), nullable=False), sa.Column('municipality_code', sa.String(length=4), nullable=True), sa.Column('county_code', sa.String(length=2), nullable=True), sa.Column('latitude', sa.String(length=20), nullable=True), sa.Column('longitude', sa.String(length=20), nullable=True), sa.Column('plan', sa.String(length=50), nullable=False), sa.Column('plan_started_at', sa.DateTime(timezone=True), nullable=True), sa.Column('plan_expires_at', sa.DateTime(timezone=True), nullable=True), sa.Column('max_users', sa.Integer(), nullable=False), sa.Column('max_projects', sa.Integer(), nullable=False), sa.Column('storage_quota_mb', sa.Integer(), nullable=False), sa.Column('features', postgresql.JSONB(astext_type=sa.Text()), nullable=True), sa.Column('settings', postgresql.JSONB(astext_type=sa.Text()), nullable=True), sa.Column('branding', postgresql.JSONB(astext_type=sa.Text()), nullable=True), sa.Column('is_trial', sa.Boolean(), nullable=True), sa.Column('trial_ends_at', sa.DateTime(timezone=True), nullable=True), sa.Column('activated_at', sa.DateTime(timezone=True), nullable=True), sa.Column('activated_by', sa.String(length=255), nullable=True), sa.Column('suspended_at', sa.DateTime(timezone=True), nullable=True), sa.Column('suspended_reason', sa.Text(), nullable=True), sa.Column('suspended_by', sa.String(length=255), nullable=True), sa.Column('cancelled_at', sa.DateTime(timezone=True), nullable=True), sa.Column('cancellation_reason', sa.Text(), nullable=True), sa.Column('description', sa.Text(), nullable=True), sa.Column('website', sa.String(length=255), nullable=True), sa.Column('is_deleted', sa.String(length=1), nullable=True), sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True), sa.Column('deleted_by', sa.String(length=255), nullable=True), sa.Column('created_by', sa.String(length=255), nullable=True), sa.Column('updated_by', sa.String(length=255), nullable=True), sa.PrimaryKeyConstraint('id'), sa.CheckConstraint("status IN ('pending', 'active', 'suspended', 'trial', 'cancelled')", name='ck_tenant_status'), sa.CheckConstraint("tenant_type IN ('municipality', 'company', 'organization', 'region', 'association')", name='ck_tenant_type'), ) op.create_index('ix_tenants_country_city', 'tenants', ['country', 'city'], unique=False) op.create_index('ix_tenants_name', 'tenants', ['name'], unique=False) op.create_index('ix_tenants_org_number', 'tenants', ['org_number'], unique=True) op.create_index('ix_tenants_schema_name', 'tenants', ['schema_name'], unique=True) op.create_index('ix_tenants_slug', 'tenants', ['slug'], unique=True) op.create_index('ix_tenants_status_type', 'tenants', ['status', 'tenant_type'], unique=False) op.create_index('ix_tenants_municipality_code', 'tenants', ['municipality_code'], unique=False) # ─── Users table ──────────────────────────────────────────────────────── op.create_table( 'users', sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('email', sa.String(length=255), nullable=False), sa.Column('hashed_password', sa.String(length=255), nullable=True), sa.Column('first_name', sa.String(length=100), nullable=False), sa.Column('last_name', sa.String(length=100), nullable=False), sa.Column('role', sa.Enum('admin', 'manager', 'analyst', 'viewer', name='userrole'), nullable=False), sa.Column('status', sa.Enum('active', 'inactive', 'pending', name='userstatus'), nullable=False), sa.Column('is_superuser', sa.Boolean(), nullable=True), sa.Column('last_login_at', sa.DateTime(timezone=True), nullable=True), sa.Column('created_at', sa.DateTime(timezone=True), nullable=True), sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=True), sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ), sa.PrimaryKeyConstraint('id'), ) op.create_index('ix_users_email', 'users', ['email'], unique=True) # ─── Invitations table ────────────────────────────────────────────────── op.create_table( 'invitations', sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('email', sa.String(length=255), nullable=False), sa.Column('token', sa.String(length=255), nullable=False), sa.Column('role', sa.Enum('admin', 'manager', 'analyst', 'viewer', name='userrole'), nullable=False), sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=True), sa.Column('invited_by_id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False), sa.Column('accepted_at', sa.DateTime(timezone=True), nullable=True), sa.Column('created_at', sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(['invited_by_id'], ['users.id'], ), sa.ForeignKeyConstraint(['tenant_id'], ['tenants.id'], ), sa.PrimaryKeyConstraint('id'), ) op.create_index('ix_invitations_email', 'invitations', ['email'], unique=False) op.create_index('ix_invitations_token', 'invitations', ['token'], unique=True) # ─── Audit Logs table ─────────────────────────────────────────────────── op.create_table( 'audit_logs', sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('timestamp', sa.DateTime(timezone=True), nullable=False), sa.Column('action', sa.Enum('CREATE', 'READ', 'UPDATE', 'DELETE', 'ACTIVATE', 'SUSPEND', 'CANCEL', 'LOGIN', 'LOGOUT', 'EXPORT', 'IMPORT', 'SCHEMA_CREATE', 'SCHEMA_DROP', name='audit_action_enum'), nullable=False), sa.Column('entity_type', sa.String(length=50), nullable=False), sa.Column('entity_id', sa.String(length=36), nullable=False), sa.Column('actor_id', sa.String(length=255), nullable=True), sa.Column('actor_type', sa.String(length=50), nullable=False), sa.Column('actor_ip', sa.String(length=45), nullable=True), sa.Column('actor_user_agent', sa.Text(), nullable=True), sa.Column('previous_values', postgresql.JSONB(astext_type=sa.Text()), nullable=True), sa.Column('new_values', postgresql.JSONB(astext_type=sa.Text()), nullable=True), sa.Column('changed_fields', postgresql.JSONB(astext_type=sa.Text()), nullable=True), sa.Column('request_id', sa.String(length=100), nullable=True), sa.Column('request_method', sa.String(length=10), nullable=True), sa.Column('request_path', sa.Text(), nullable=True), sa.Column('request_body', sa.Text(), nullable=True), sa.Column('success', sa.String(length=1), nullable=False), sa.Column('error_message', sa.Text(), nullable=True), sa.Column('tenant_id', sa.String(length=36), nullable=True), sa.Column('tenant_schema', sa.String(length=100), nullable=True), sa.Column('metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True), sa.PrimaryKeyConstraint('id'), ) op.create_index('ix_audit_logs_action', 'audit_logs', ['action'], unique=False) op.create_index('ix_audit_logs_actor_id', 'audit_logs', ['actor_id'], unique=False) op.create_index('ix_audit_logs_actor_tenant', 'audit_logs', ['actor_id', 'tenant_id'], unique=False) op.create_index('ix_audit_logs_entity', 'audit_logs', ['entity_type', 'entity_id', 'timestamp'], unique=False) op.create_index('ix_audit_logs_entity_id', 'audit_logs', ['entity_id'], unique=False) op.create_index('ix_audit_logs_entity_type', 'audit_logs', ['entity_type'], unique=False) op.create_index('ix_audit_logs_request_id', 'audit_logs', ['request_id'], unique=False) op.create_index('ix_audit_logs_tenant_id', 'audit_logs', ['tenant_id'], unique=False) op.create_index('ix_audit_logs_timestamp', 'audit_logs', ['timestamp'], unique=False) op.create_index('ix_audit_logs_timestamp_action', 'audit_logs', ['timestamp', 'action'], unique=False) def downgrade() -> None: op.drop_index('ix_audit_logs_timestamp_action', table_name='audit_logs') op.drop_index('ix_audit_logs_timestamp', table_name='audit_logs') op.drop_index('ix_audit_logs_tenant_id', table_name='audit_logs') op.drop_index('ix_audit_logs_request_id', table_name='audit_logs') op.drop_index('ix_audit_logs_entity_type', table_name='audit_logs') op.drop_index('ix_audit_logs_entity_id', table_name='audit_logs') op.drop_index('ix_audit_logs_entity', table_name='audit_logs') op.drop_index('ix_audit_logs_actor_tenant', table_name='audit_logs') op.drop_index('ix_audit_logs_actor_id', table_name='audit_logs') op.drop_index('ix_audit_logs_action', table_name='audit_logs') op.drop_table('audit_logs') op.drop_index('ix_invitations_token', table_name='invitations') op.drop_index('ix_invitations_email', table_name='invitations') op.drop_table('invitations') op.drop_index('ix_users_email', table_name='users') op.drop_table('users') op.drop_index('ix_tenants_municipality_code', table_name='tenants') op.drop_index('ix_tenants_status_type', table_name='tenants') op.drop_index('ix_tenants_slug', table_name='tenants') op.drop_index('ix_tenants_schema_name', table_name='tenants') op.drop_index('ix_tenants_org_number', table_name='tenants') op.drop_index('ix_tenants_name', table_name='tenants') op.drop_index('ix_tenants_country_city', table_name='tenants') op.drop_table('tenants') # Drop enums op.execute("DROP TYPE IF EXISTS audit_action_enum CASCADE") op.execute("DROP TYPE IF EXISTS tenant_status_enum CASCADE") op.execute("DROP TYPE IF EXISTS tenant_type_enum CASCADE") op.execute("DROP TYPE IF EXISTS userstatus CASCADE") op.execute("DROP TYPE IF EXISTS userrole CASCADE")