landvex: Fixar och tester klara för alla komponenter
- Datafabrik: Dockerfile fix, agentorkestrering fungerar - Vision: Identify-modell, FAISS, OCR alla testade - API: Alla 7 integrationstester passerade - Upplösare: Entitetsupplösning verifierad
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
Alembic environment configuration för LandveX Admin Backend.
|
||||
"""
|
||||
import asyncio
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
from alembic import context
|
||||
|
||||
from app.config import get_settings
|
||||
from app.database import Base
|
||||
from app.models import Tenant, User, Invitation, AuditLog # noqa: F401
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
target_metadata = Base.metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def get_url():
|
||||
return settings.DATABASE_URL
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = get_url()
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
"""In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
configuration = config.get_section(config.config_ini_section, {})
|
||||
configuration["sqlalchemy.url"] = get_url()
|
||||
connectable = async_engine_from_config(
|
||||
configuration,
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,232 @@
|
||||
"""
|
||||
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")
|
||||
Reference in New Issue
Block a user