"""Precision_Schema_v1_0_9_Final

Revision ID: 4d69a44da00a
Revises: 062cfbbdd076
Create Date: 2026-02-25 08:41:01.664164

"""
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 = '4d69a44da00a'
down_revision: Union[str, Sequence[str], None] = '062cfbbdd076'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('asset_inspections',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('inspector_id', sa.Integer(), nullable=False),
    sa.Column('timestamp', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('checklist_results', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.Column('is_safe', sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.ForeignKeyConstraint(['inspector_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('vehicle_logbook',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('driver_id', sa.Integer(), nullable=False),
    sa.Column('trip_type', sa.String(length=30), nullable=False),
    sa.Column('is_reimbursable', sa.Boolean(), nullable=False),
    sa.Column('start_mileage', sa.Integer(), nullable=False),
    sa.Column('end_mileage', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.ForeignKeyConstraint(['driver_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_vehicle_logbook_trip_type'), 'vehicle_logbook', ['trip_type'], unique=False, schema='data')
    op.create_table('vehicle_ownership_history',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('acquired_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('disposed_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.drop_constraint(op.f('addresses_postal_code_id_fkey'), 'addresses', type_='foreignkey')
    op.create_foreign_key(None, 'addresses', 'geo_postal_codes', ['postal_code_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('asset_assignments_branch_id_fkey'), 'asset_assignments', type_='foreignkey')
    op.drop_constraint(op.f('asset_assignments_asset_id_fkey'), 'asset_assignments', type_='foreignkey')
    op.drop_constraint(op.f('asset_assignments_organization_id_fkey'), 'asset_assignments', type_='foreignkey')
    op.create_foreign_key(None, 'asset_assignments', 'organizations', ['organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'asset_assignments', 'assets', ['asset_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_column('asset_assignments', 'assigned_at')
    op.drop_column('asset_assignments', 'released_at')
    op.drop_column('asset_assignments', 'branch_id')
    op.add_column('asset_costs', sa.Column('cost_category', sa.String(length=50), nullable=False))
    op.add_column('asset_costs', sa.Column('amount_net', sa.Numeric(precision=18, scale=2), nullable=False))
    op.add_column('asset_costs', sa.Column('currency', sa.String(length=3), nullable=False))
    op.add_column('asset_costs', sa.Column('invoice_number', sa.String(length=100), nullable=True))
    op.drop_index(op.f('ix_data_asset_costs_registration_uuid'), table_name='asset_costs')
    op.create_index(op.f('ix_data_asset_costs_cost_category'), 'asset_costs', ['cost_category'], unique=False, schema='data')
    op.create_index(op.f('ix_data_asset_costs_invoice_number'), 'asset_costs', ['invoice_number'], unique=False, schema='data')
    op.drop_constraint(op.f('asset_costs_driver_id_fkey'), 'asset_costs', type_='foreignkey')
    op.drop_constraint(op.f('asset_costs_asset_id_fkey'), 'asset_costs', type_='foreignkey')
    op.drop_constraint(op.f('asset_costs_organization_id_fkey'), 'asset_costs', type_='foreignkey')
    op.create_foreign_key(None, 'asset_costs', 'assets', ['asset_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'asset_costs', 'organizations', ['organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_column('asset_costs', 'driver_id')
    op.drop_column('asset_costs', 'cost_type')
    op.drop_column('asset_costs', 'currency_local')
    op.drop_column('asset_costs', 'amount_local')
    op.drop_column('asset_costs', 'amount_eur')
    op.drop_column('asset_costs', 'vat_rate')
    op.drop_column('asset_costs', 'registration_uuid')
    op.drop_column('asset_costs', 'exchange_rate_used')
    op.drop_column('asset_costs', 'net_amount_local')
    op.drop_column('asset_costs', 'mileage_at_cost')
    op.drop_index(op.f('ix_data_asset_events_registration_uuid'), table_name='asset_events')
    op.drop_constraint(op.f('asset_events_asset_id_fkey'), 'asset_events', type_='foreignkey')
    op.create_foreign_key(None, 'asset_events', 'assets', ['asset_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_column('asset_events', 'registration_uuid')
    op.drop_column('asset_events', 'recorded_mileage')
    op.drop_column('asset_events', 'data')
    op.add_column('asset_financials', sa.Column('purchase_price_net', sa.Numeric(precision=18, scale=2), nullable=False))
    op.add_column('asset_financials', sa.Column('purchase_price_gross', sa.Numeric(precision=18, scale=2), nullable=False))
    op.add_column('asset_financials', sa.Column('vat_rate', sa.Numeric(precision=5, scale=2), nullable=False))
    op.add_column('asset_financials', sa.Column('activation_date', sa.DateTime(), nullable=True))
    op.add_column('asset_financials', sa.Column('accounting_details', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False))
    op.alter_column('asset_financials', 'financing_type',
               existing_type=sa.VARCHAR(),
               nullable=False)
    op.drop_constraint(op.f('asset_financials_asset_id_fkey'), 'asset_financials', type_='foreignkey')
    op.create_foreign_key(None, 'asset_financials', 'assets', ['asset_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_column('asset_financials', 'residual_value_estimate')
    op.drop_column('asset_financials', 'acquisition_price')
    op.drop_column('asset_financials', 'acquisition_date')
    op.drop_constraint(op.f('asset_reviews_user_id_fkey'), 'asset_reviews', type_='foreignkey')
    op.drop_constraint(op.f('asset_reviews_asset_id_fkey'), 'asset_reviews', type_='foreignkey')
    op.create_foreign_key(None, 'asset_reviews', 'users', ['user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'asset_reviews', 'assets', ['asset_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_column('asset_reviews', 'criteria_scores')
    op.drop_constraint(op.f('asset_telemetry_asset_id_fkey'), 'asset_telemetry', type_='foreignkey')
    op.create_foreign_key(None, 'asset_telemetry', 'assets', ['asset_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_column('asset_telemetry', 'dbs_score')
    op.drop_column('asset_telemetry', 'vqi_score')
    op.drop_column('asset_telemetry', 'mileage_unit')
    op.add_column('assets', sa.Column('first_registration_date', sa.DateTime(timezone=True), nullable=True))
    op.add_column('assets', sa.Column('current_mileage', sa.Integer(), nullable=False))
    op.add_column('assets', sa.Column('condition_score', sa.Integer(), nullable=False))
    op.add_column('assets', sa.Column('is_for_sale', sa.Boolean(), nullable=False))
    op.add_column('assets', sa.Column('price', sa.Numeric(precision=15, scale=2), nullable=True))
    op.add_column('assets', sa.Column('currency', sa.String(length=3), nullable=False))
    op.add_column('assets', sa.Column('individual_equipment', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False))
    op.drop_index(op.f('ix_data_assets_registration_uuid'), table_name='assets')
    op.create_index(op.f('ix_data_assets_current_mileage'), 'assets', ['current_mileage'], unique=False, schema='data')
    op.create_index(op.f('ix_data_assets_is_for_sale'), 'assets', ['is_for_sale'], unique=False, schema='data')
    op.create_index(op.f('ix_data_assets_year_of_manufacture'), 'assets', ['year_of_manufacture'], unique=False, schema='data')
    op.drop_constraint(op.f('assets_operator_org_id_fkey'), 'assets', type_='foreignkey')
    op.drop_constraint(op.f('assets_owner_person_id_fkey'), 'assets', type_='foreignkey')
    op.drop_constraint(op.f('assets_owner_org_id_fkey'), 'assets', type_='foreignkey')
    op.drop_constraint(op.f('assets_operator_person_id_fkey'), 'assets', type_='foreignkey')
    op.drop_constraint(op.f('assets_catalog_id_fkey'), 'assets', type_='foreignkey')
    op.drop_constraint(op.f('assets_current_organization_id_fkey'), 'assets', type_='foreignkey')
    op.create_foreign_key(None, 'assets', 'vehicle_catalog', ['catalog_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'assets', 'organizations', ['owner_org_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'assets', 'persons', ['owner_person_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'assets', 'organizations', ['current_organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'assets', 'persons', ['operator_person_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'assets', 'organizations', ['operator_org_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_column('assets', 'is_corporate')
    op.drop_column('assets', 'is_verified')
    op.drop_column('assets', 'catalog_match_score')
    op.drop_column('assets', 'verification_method')
    op.drop_column('assets', 'verification_notes')
    op.drop_column('assets', 'registration_uuid')
    op.alter_column('audit_logs', 'severity',
               existing_type=postgresql.ENUM('info', 'warning', 'critical', 'emergency', name='log_severity'),
               type_=postgresql.ENUM('info', 'warning', 'critical', 'emergency', name='log_severity', schema='data'),
               existing_nullable=False)
    op.drop_constraint(op.f('audit_logs_user_id_fkey'), 'audit_logs', type_='foreignkey')
    op.create_foreign_key(None, 'audit_logs', 'users', ['user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.drop_constraint(op.f('branches_address_id_fkey'), 'branches', type_='foreignkey')
    op.drop_constraint(op.f('branches_organization_id_fkey'), 'branches', type_='foreignkey')
    op.create_foreign_key(None, 'branches', 'organizations', ['organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'branches', 'addresses', ['address_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('_make_model_class_uc'), 'catalog_discovery', type_='unique')
    op.drop_index(op.f('ix_data_catalog_discovery_vehicle_class'), table_name='catalog_discovery')
    op.create_unique_constraint('_make_model_uc', 'catalog_discovery', ['make', 'model'], schema='data')
    op.drop_column('catalog_discovery', 'source')
    op.drop_column('catalog_discovery', 'vehicle_class')
    op.drop_column('catalog_discovery', 'last_attempt')
    op.drop_column('catalog_discovery', 'priority_score')
    op.drop_column('catalog_discovery', 'attempts')
    op.drop_column('catalog_discovery', 'created_at')
    op.drop_constraint(op.f('credit_logs_org_id_fkey'), 'credit_logs', type_='foreignkey')
    op.create_foreign_key(None, 'credit_logs', 'organizations', ['org_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('exchange_rates_target_currency_key'), 'exchange_rates', type_='unique')
    op.drop_column('exchange_rates', 'base_currency')
    op.drop_column('exchange_rates', 'target_currency')
    op.drop_constraint(op.f('feature_definitions_vehicle_type_id_fkey'), 'feature_definitions', type_='foreignkey')
    op.create_foreign_key(None, 'feature_definitions', 'vehicle_types', ['vehicle_type_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('geo_streets_postal_code_id_fkey'), 'geo_streets', type_='foreignkey')
    op.create_foreign_key(None, 'geo_streets', 'geo_postal_codes', ['postal_code_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('model_feature_maps_feature_id_fkey'), 'model_feature_maps', type_='foreignkey')
    op.drop_constraint(op.f('model_feature_maps_model_definition_id_fkey'), 'model_feature_maps', type_='foreignkey')
    op.create_foreign_key(None, 'model_feature_maps', 'feature_definitions', ['feature_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'model_feature_maps', 'vehicle_model_definitions', ['model_definition_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('org_sales_assignments_agent_user_id_fkey'), 'org_sales_assignments', type_='foreignkey')
    op.drop_constraint(op.f('org_sales_assignments_organization_id_fkey'), 'org_sales_assignments', type_='foreignkey')
    op.create_foreign_key(None, 'org_sales_assignments', 'organizations', ['organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'org_sales_assignments', 'users', ['agent_user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.drop_constraint(op.f('org_subscriptions_tier_id_fkey'), 'org_subscriptions', type_='foreignkey')
    op.drop_constraint(op.f('org_subscriptions_org_id_fkey'), 'org_subscriptions', type_='foreignkey')
    op.create_foreign_key(None, 'org_subscriptions', 'subscription_tiers', ['tier_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'org_subscriptions', 'organizations', ['org_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('organization_financials_organization_id_fkey'), 'organization_financials', type_='foreignkey')
    op.create_foreign_key(None, 'organization_financials', 'organizations', ['organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.alter_column('organization_members', 'role',
               existing_type=postgresql.ENUM('OWNER', 'ADMIN', 'FLEET_MANAGER', 'DRIVER', 'MECHANIC', 'RECEPTIONIST', name='orguserrole'),
               type_=postgresql.ENUM('OWNER', 'ADMIN', 'FLEET_MANAGER', 'DRIVER', 'MECHANIC', 'RECEPTIONIST', name='orguserrole', schema='data'),
               existing_nullable=False)
    op.drop_constraint(op.f('organization_members_user_id_fkey'), 'organization_members', type_='foreignkey')
    op.drop_constraint(op.f('organization_members_organization_id_fkey'), 'organization_members', type_='foreignkey')
    op.drop_constraint(op.f('organization_members_person_id_fkey'), 'organization_members', type_='foreignkey')
    op.create_foreign_key(None, 'organization_members', 'users', ['user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'organization_members', 'persons', ['person_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'organization_members', 'organizations', ['organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.alter_column('organizations', 'org_type',
               existing_type=postgresql.ENUM('individual', 'service', 'service_provider', 'fleet_owner', 'club', 'business', name='orgtype'),
               type_=postgresql.ENUM('individual', 'service', 'service_provider', 'fleet_owner', 'club', 'business', name='orgtype', schema='data'),
               existing_nullable=False)
    op.drop_constraint(op.f('organizations_owner_id_fkey'), 'organizations', type_='foreignkey')
    op.drop_constraint(op.f('organizations_address_id_fkey'), 'organizations', type_='foreignkey')
    op.create_foreign_key(None, 'organizations', 'addresses', ['address_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'organizations', 'users', ['owner_id'], ['id'], source_schema='data', referent_schema='identity')
    op.drop_constraint(op.f('points_ledger_user_id_fkey'), 'points_ledger', type_='foreignkey')
    op.create_foreign_key(None, 'points_ledger', 'users', ['user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.drop_constraint(op.f('ratings_target_user_id_fkey'), 'ratings', type_='foreignkey')
    op.drop_constraint(op.f('ratings_author_id_fkey'), 'ratings', type_='foreignkey')
    op.drop_constraint(op.f('ratings_target_organization_id_fkey'), 'ratings', type_='foreignkey')
    op.drop_constraint(op.f('ratings_target_branch_id_fkey'), 'ratings', type_='foreignkey')
    op.create_foreign_key(None, 'ratings', 'users', ['target_user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'ratings', 'branches', ['target_branch_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'ratings', 'users', ['author_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'ratings', 'organizations', ['target_organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('service_expertises_service_id_fkey'), 'service_expertises', type_='foreignkey')
    op.drop_constraint(op.f('service_expertises_expertise_id_fkey'), 'service_expertises', type_='foreignkey')
    op.create_foreign_key(None, 'service_expertises', 'service_profiles', ['service_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'service_expertises', 'expertise_tags', ['expertise_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_index(op.f('idx_service_profiles_location'), table_name='service_profiles', postgresql_using='gist')
    op.drop_constraint(op.f('service_profiles_parent_id_fkey'), 'service_profiles', type_='foreignkey')
    op.drop_constraint(op.f('service_profiles_organization_id_fkey'), 'service_profiles', type_='foreignkey')
    op.create_foreign_key(None, 'service_profiles', 'organizations', ['organization_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'service_profiles', 'service_profiles', ['parent_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('service_specialties_parent_id_fkey'), 'service_specialties', type_='foreignkey')
    op.create_foreign_key(None, 'service_specialties', 'service_specialties', ['parent_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('user_badges_user_id_fkey'), 'user_badges', type_='foreignkey')
    op.drop_constraint(op.f('user_badges_badge_id_fkey'), 'user_badges', type_='foreignkey')
    op.create_foreign_key(None, 'user_badges', 'badges', ['badge_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'user_badges', 'users', ['user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.drop_constraint(op.f('user_stats_user_id_fkey'), 'user_stats', type_='foreignkey')
    op.create_foreign_key(None, 'user_stats', 'users', ['user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.drop_index(op.f('ix_data_vehicle_catalog_engine_variant'), table_name='vehicle_catalog')
    op.drop_constraint(op.f('uix_vehicle_catalog_full'), 'vehicle_catalog', type_='unique')
    op.create_unique_constraint('uix_vehicle_catalog_full', 'vehicle_catalog', ['make', 'model', 'year_from', 'fuel_type'], schema='data')
    op.drop_constraint(op.f('vehicle_catalog_master_definition_id_fkey'), 'vehicle_catalog', type_='foreignkey')
    op.create_foreign_key(None, 'vehicle_catalog', 'vehicle_model_definitions', ['master_definition_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_column('vehicle_catalog', 'euro_class')
    op.drop_column('vehicle_catalog', 'vehicle_class')
    op.drop_column('vehicle_catalog', 'body_type')
    op.drop_column('vehicle_catalog', 'max_weight_kg')
    op.drop_column('vehicle_catalog', 'axle_count')
    op.drop_column('vehicle_catalog', 'engine_variant')
    op.drop_column('vehicle_catalog', 'engine_code')
    op.add_column('vehicle_model_definitions', sa.Column('normalized_name', sa.String(length=255), nullable=True), schema='data')
    op.add_column('vehicle_model_definitions', sa.Column('marketing_name_aliases', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'[]'::jsonb"), nullable=False))
    op.add_column('vehicle_model_definitions', sa.Column('variant_code', sa.String(length=100), nullable=True))
    op.add_column('vehicle_model_definitions', sa.Column('version_code', sa.String(length=100), nullable=True))
    op.add_column('vehicle_model_definitions', sa.Column('cylinders', sa.Integer(), nullable=True))
    op.add_column('vehicle_model_definitions', sa.Column('curb_weight', sa.Integer(), nullable=True))
    op.add_column('vehicle_model_definitions', sa.Column('max_weight', sa.Integer(), nullable=True))
    op.add_column('vehicle_model_definitions', sa.Column('euro_classification', sa.String(length=20), nullable=True))
    op.add_column('vehicle_model_definitions', sa.Column('doors', sa.Integer(), nullable=True))
    op.alter_column('vehicle_model_definitions', 'make',
               existing_type=sa.VARCHAR(length=50),
               type_=sa.String(length=100),
               existing_nullable=False)
    op.alter_column('vehicle_model_definitions', 'marketing_name',
               existing_type=sa.VARCHAR(length=100),
               type_=sa.String(length=255),
               nullable=False)
    op.alter_column('vehicle_model_definitions', 'technical_code',
               existing_type=sa.VARCHAR(length=50),
               type_=sa.String(length=100),
               existing_nullable=False)
    op.alter_column('vehicle_model_definitions', 'body_type',
               existing_type=sa.VARCHAR(length=50),
               type_=sa.String(length=100),
               existing_nullable=True)
    op.alter_column('vehicle_model_definitions', 'engine_capacity',
               existing_type=sa.INTEGER(),
               nullable=False)
    op.alter_column('vehicle_model_definitions', 'power_kw',
               existing_type=sa.INTEGER(),
               nullable=False)
    op.alter_column('vehicle_model_definitions', 'status',
               existing_type=sa.VARCHAR(length=30),
               type_=sa.String(length=50),
               existing_nullable=False,
               existing_server_default=sa.text("'active'::character varying"))
    op.alter_column('vehicle_model_definitions', 'source',
               existing_type=sa.VARCHAR(length=50),
               type_=sa.String(length=100),
               existing_nullable=True)
    op.drop_index(op.f('idx_vmd_lookup'), table_name='vehicle_model_definitions')
    op.drop_index(op.f('ix_vehicle_model_marketing_name'), table_name='vehicle_model_definitions')
    op.drop_constraint(op.f('uix_make_tech_type'), 'vehicle_model_definitions', type_='unique')
    op.create_index('idx_vmd_lookup_fast', 'vehicle_model_definitions', ['make', 'normalized_name'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_engine_capacity'), 'vehicle_model_definitions', ['engine_capacity'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_fuel_type'), 'vehicle_model_definitions', ['fuel_type'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_normalized_name'), 'vehicle_model_definitions', ['normalized_name'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_power_kw'), 'vehicle_model_definitions', ['power_kw'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_variant_code'), 'vehicle_model_definitions', ['variant_code'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_vehicle_class'), 'vehicle_model_definitions', ['vehicle_class'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_version_code'), 'vehicle_model_definitions', ['version_code'], unique=False, schema='data')
    op.create_unique_constraint('uix_vmd_precision', 'vehicle_model_definitions', ['make', 'normalized_name', 'variant_code', 'version_code', 'fuel_type'], schema='data')
    op.drop_constraint(op.f('vehicle_model_definitions_vehicle_type_id_fkey'), 'vehicle_model_definitions', type_='foreignkey')
    op.create_foreign_key(None, 'vehicle_model_definitions', 'vehicle_types', ['vehicle_type_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('persons_address_id_fkey'), 'persons', schema='identity', type_='foreignkey')
    op.create_foreign_key(None, 'persons', 'addresses', ['address_id'], ['id'], source_schema='identity', referent_schema='data')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'persons', schema='identity', type_='foreignkey')
    op.create_foreign_key(op.f('persons_address_id_fkey'), 'persons', 'addresses', ['address_id'], ['id'], source_schema='identity')
    op.drop_constraint(None, 'vehicle_model_definitions', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('vehicle_model_definitions_vehicle_type_id_fkey'), 'vehicle_model_definitions', 'vehicle_types', ['vehicle_type_id'], ['id'])
    op.drop_constraint('uix_vmd_precision', 'vehicle_model_definitions', schema='data', type_='unique')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_version_code'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_vehicle_class'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_variant_code'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_power_kw'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_normalized_name'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_fuel_type'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_engine_capacity'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index('idx_vmd_lookup_fast', table_name='vehicle_model_definitions', schema='data')
    op.create_unique_constraint(op.f('uix_make_tech_type'), 'vehicle_model_definitions', ['make', 'technical_code', 'vehicle_type_id'], postgresql_nulls_not_distinct=False)
    op.create_index(op.f('ix_vehicle_model_marketing_name'), 'vehicle_model_definitions', ['marketing_name'], unique=False)
    op.create_index(op.f('idx_vmd_lookup'), 'vehicle_model_definitions', ['make', 'technical_code'], unique=False)
    op.alter_column('vehicle_model_definitions', 'source',
               existing_type=sa.String(length=100),
               type_=sa.VARCHAR(length=50),
               existing_nullable=True)
    op.alter_column('vehicle_model_definitions', 'status',
               existing_type=sa.String(length=50),
               type_=sa.VARCHAR(length=30),
               existing_nullable=False,
               existing_server_default=sa.text("'active'::character varying"))
    op.alter_column('vehicle_model_definitions', 'power_kw',
               existing_type=sa.INTEGER(),
               nullable=True)
    op.alter_column('vehicle_model_definitions', 'engine_capacity',
               existing_type=sa.INTEGER(),
               nullable=True)
    op.alter_column('vehicle_model_definitions', 'body_type',
               existing_type=sa.String(length=100),
               type_=sa.VARCHAR(length=50),
               existing_nullable=True)
    op.alter_column('vehicle_model_definitions', 'technical_code',
               existing_type=sa.String(length=100),
               type_=sa.VARCHAR(length=50),
               existing_nullable=False)
    op.alter_column('vehicle_model_definitions', 'marketing_name',
               existing_type=sa.String(length=255),
               type_=sa.VARCHAR(length=100),
               nullable=True)
    op.alter_column('vehicle_model_definitions', 'make',
               existing_type=sa.String(length=100),
               type_=sa.VARCHAR(length=50),
               existing_nullable=False)
    op.drop_column('vehicle_model_definitions', 'doors')
    op.drop_column('vehicle_model_definitions', 'euro_classification')
    op.drop_column('vehicle_model_definitions', 'max_weight')
    op.drop_column('vehicle_model_definitions', 'curb_weight')
    op.drop_column('vehicle_model_definitions', 'cylinders')
    op.drop_column('vehicle_model_definitions', 'version_code')
    op.drop_column('vehicle_model_definitions', 'variant_code')
    op.drop_column('vehicle_model_definitions', 'marketing_name_aliases')
    op.drop_column('vehicle_model_definitions', 'normalized_name')
    op.add_column('vehicle_catalog', sa.Column('engine_code', sa.VARCHAR(), autoincrement=False, nullable=True))
    op.add_column('vehicle_catalog', sa.Column('engine_variant', sa.VARCHAR(), autoincrement=False, nullable=True))
    op.add_column('vehicle_catalog', sa.Column('axle_count', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('vehicle_catalog', sa.Column('max_weight_kg', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('vehicle_catalog', sa.Column('body_type', sa.VARCHAR(length=100), autoincrement=False, nullable=True))
    op.add_column('vehicle_catalog', sa.Column('vehicle_class', sa.VARCHAR(), autoincrement=False, nullable=True))
    op.add_column('vehicle_catalog', sa.Column('euro_class', sa.VARCHAR(length=20), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'vehicle_catalog', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('vehicle_catalog_master_definition_id_fkey'), 'vehicle_catalog', 'vehicle_model_definitions', ['master_definition_id'], ['id'])
    op.drop_constraint('uix_vehicle_catalog_full', 'vehicle_catalog', schema='data', type_='unique')
    op.create_unique_constraint(op.f('uix_vehicle_catalog_full'), 'vehicle_catalog', ['make', 'model', 'year_from', 'engine_variant', 'fuel_type'], postgresql_nulls_not_distinct=False)
    op.create_index(op.f('ix_data_vehicle_catalog_engine_variant'), 'vehicle_catalog', ['engine_variant'], unique=False)
    op.drop_constraint(None, 'user_stats', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('user_stats_user_id_fkey'), 'user_stats', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.drop_constraint(None, 'user_badges', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'user_badges', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('user_badges_badge_id_fkey'), 'user_badges', 'badges', ['badge_id'], ['id'])
    op.create_foreign_key(op.f('user_badges_user_id_fkey'), 'user_badges', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.drop_constraint(None, 'service_specialties', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('service_specialties_parent_id_fkey'), 'service_specialties', 'service_specialties', ['parent_id'], ['id'])
    op.drop_constraint(None, 'service_profiles', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'service_profiles', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('service_profiles_organization_id_fkey'), 'service_profiles', 'organizations', ['organization_id'], ['id'])
    op.create_foreign_key(op.f('service_profiles_parent_id_fkey'), 'service_profiles', 'service_profiles', ['parent_id'], ['id'])
    op.create_index(op.f('idx_service_profiles_location'), 'service_profiles', ['location'], unique=False, postgresql_using='gist')
    op.drop_constraint(None, 'service_expertises', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'service_expertises', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('service_expertises_expertise_id_fkey'), 'service_expertises', 'expertise_tags', ['expertise_id'], ['id'])
    op.create_foreign_key(op.f('service_expertises_service_id_fkey'), 'service_expertises', 'service_profiles', ['service_id'], ['id'])
    op.drop_constraint(None, 'ratings', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'ratings', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'ratings', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'ratings', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('ratings_target_branch_id_fkey'), 'ratings', 'branches', ['target_branch_id'], ['id'])
    op.create_foreign_key(op.f('ratings_target_organization_id_fkey'), 'ratings', 'organizations', ['target_organization_id'], ['id'])
    op.create_foreign_key(op.f('ratings_author_id_fkey'), 'ratings', 'users', ['author_id'], ['id'], referent_schema='identity')
    op.create_foreign_key(op.f('ratings_target_user_id_fkey'), 'ratings', 'users', ['target_user_id'], ['id'], referent_schema='identity')
    op.drop_constraint(None, 'points_ledger', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('points_ledger_user_id_fkey'), 'points_ledger', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.drop_constraint(None, 'organizations', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'organizations', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('organizations_address_id_fkey'), 'organizations', 'addresses', ['address_id'], ['id'])
    op.create_foreign_key(op.f('organizations_owner_id_fkey'), 'organizations', 'users', ['owner_id'], ['id'], referent_schema='identity')
    op.alter_column('organizations', 'org_type',
               existing_type=postgresql.ENUM('individual', 'service', 'service_provider', 'fleet_owner', 'club', 'business', name='orgtype', schema='data'),
               type_=postgresql.ENUM('individual', 'service', 'service_provider', 'fleet_owner', 'club', 'business', name='orgtype'),
               existing_nullable=False)
    op.drop_constraint(None, 'organization_members', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'organization_members', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'organization_members', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('organization_members_person_id_fkey'), 'organization_members', 'persons', ['person_id'], ['id'], referent_schema='identity')
    op.create_foreign_key(op.f('organization_members_organization_id_fkey'), 'organization_members', 'organizations', ['organization_id'], ['id'])
    op.create_foreign_key(op.f('organization_members_user_id_fkey'), 'organization_members', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.alter_column('organization_members', 'role',
               existing_type=postgresql.ENUM('OWNER', 'ADMIN', 'FLEET_MANAGER', 'DRIVER', 'MECHANIC', 'RECEPTIONIST', name='orguserrole', schema='data'),
               type_=postgresql.ENUM('OWNER', 'ADMIN', 'FLEET_MANAGER', 'DRIVER', 'MECHANIC', 'RECEPTIONIST', name='orguserrole'),
               existing_nullable=False)
    op.drop_constraint(None, 'organization_financials', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('organization_financials_organization_id_fkey'), 'organization_financials', 'organizations', ['organization_id'], ['id'])
    op.drop_constraint(None, 'org_subscriptions', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'org_subscriptions', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('org_subscriptions_org_id_fkey'), 'org_subscriptions', 'organizations', ['org_id'], ['id'])
    op.create_foreign_key(op.f('org_subscriptions_tier_id_fkey'), 'org_subscriptions', 'subscription_tiers', ['tier_id'], ['id'])
    op.drop_constraint(None, 'org_sales_assignments', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'org_sales_assignments', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('org_sales_assignments_organization_id_fkey'), 'org_sales_assignments', 'organizations', ['organization_id'], ['id'])
    op.create_foreign_key(op.f('org_sales_assignments_agent_user_id_fkey'), 'org_sales_assignments', 'users', ['agent_user_id'], ['id'], referent_schema='identity')
    op.drop_constraint(None, 'model_feature_maps', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'model_feature_maps', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('model_feature_maps_model_definition_id_fkey'), 'model_feature_maps', 'vehicle_model_definitions', ['model_definition_id'], ['id'])
    op.create_foreign_key(op.f('model_feature_maps_feature_id_fkey'), 'model_feature_maps', 'feature_definitions', ['feature_id'], ['id'])
    op.drop_constraint(None, 'geo_streets', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('geo_streets_postal_code_id_fkey'), 'geo_streets', 'geo_postal_codes', ['postal_code_id'], ['id'])
    op.drop_constraint(None, 'feature_definitions', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('feature_definitions_vehicle_type_id_fkey'), 'feature_definitions', 'vehicle_types', ['vehicle_type_id'], ['id'])
    op.add_column('exchange_rates', sa.Column('target_currency', sa.VARCHAR(length=3), autoincrement=False, nullable=True))
    op.add_column('exchange_rates', sa.Column('base_currency', sa.VARCHAR(length=3), autoincrement=False, nullable=False))
    op.create_unique_constraint(op.f('exchange_rates_target_currency_key'), 'exchange_rates', ['target_currency'], postgresql_nulls_not_distinct=False)
    op.drop_constraint(None, 'credit_logs', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('credit_logs_org_id_fkey'), 'credit_logs', 'organizations', ['org_id'], ['id'])
    op.add_column('catalog_discovery', sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False))
    op.add_column('catalog_discovery', sa.Column('attempts', sa.INTEGER(), autoincrement=False, nullable=False))
    op.add_column('catalog_discovery', sa.Column('priority_score', sa.INTEGER(), server_default=sa.text('0'), autoincrement=False, nullable=True))
    op.add_column('catalog_discovery', sa.Column('last_attempt', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True))
    op.add_column('catalog_discovery', sa.Column('vehicle_class', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
    op.add_column('catalog_discovery', sa.Column('source', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
    op.drop_constraint('_make_model_uc', 'catalog_discovery', schema='data', type_='unique')
    op.create_index(op.f('ix_data_catalog_discovery_vehicle_class'), 'catalog_discovery', ['vehicle_class'], unique=False)
    op.create_unique_constraint(op.f('_make_model_class_uc'), 'catalog_discovery', ['make', 'model', 'vehicle_class'], postgresql_nulls_not_distinct=False)
    op.drop_constraint(None, 'branches', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'branches', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('branches_organization_id_fkey'), 'branches', 'organizations', ['organization_id'], ['id'])
    op.create_foreign_key(op.f('branches_address_id_fkey'), 'branches', 'addresses', ['address_id'], ['id'])
    op.drop_constraint(None, 'audit_logs', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('audit_logs_user_id_fkey'), 'audit_logs', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.alter_column('audit_logs', 'severity',
               existing_type=postgresql.ENUM('info', 'warning', 'critical', 'emergency', name='log_severity', schema='data'),
               type_=postgresql.ENUM('info', 'warning', 'critical', 'emergency', name='log_severity'),
               existing_nullable=False)
    op.add_column('assets', sa.Column('registration_uuid', sa.UUID(), autoincrement=False, nullable=False))
    op.add_column('assets', sa.Column('verification_notes', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('assets', sa.Column('verification_method', sa.VARCHAR(length=20), autoincrement=False, nullable=True))
    op.add_column('assets', sa.Column('catalog_match_score', sa.NUMERIC(precision=5, scale=2), autoincrement=False, nullable=True))
    op.add_column('assets', sa.Column('is_verified', sa.BOOLEAN(), autoincrement=False, nullable=False))
    op.add_column('assets', sa.Column('is_corporate', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=False))
    op.drop_constraint(None, 'assets', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'assets', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'assets', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'assets', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'assets', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'assets', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('assets_current_organization_id_fkey'), 'assets', 'organizations', ['current_organization_id'], ['id'])
    op.create_foreign_key(op.f('assets_catalog_id_fkey'), 'assets', 'vehicle_catalog', ['catalog_id'], ['id'])
    op.create_foreign_key(op.f('assets_operator_person_id_fkey'), 'assets', 'persons', ['operator_person_id'], ['id'], referent_schema='identity')
    op.create_foreign_key(op.f('assets_owner_org_id_fkey'), 'assets', 'organizations', ['owner_org_id'], ['id'])
    op.create_foreign_key(op.f('assets_owner_person_id_fkey'), 'assets', 'persons', ['owner_person_id'], ['id'], referent_schema='identity')
    op.create_foreign_key(op.f('assets_operator_org_id_fkey'), 'assets', 'organizations', ['operator_org_id'], ['id'])
    op.drop_index(op.f('ix_data_assets_year_of_manufacture'), table_name='assets', schema='data')
    op.drop_index(op.f('ix_data_assets_is_for_sale'), table_name='assets', schema='data')
    op.drop_index(op.f('ix_data_assets_current_mileage'), table_name='assets', schema='data')
    op.create_index(op.f('ix_data_assets_registration_uuid'), 'assets', ['registration_uuid'], unique=False)
    op.drop_column('assets', 'individual_equipment')
    op.drop_column('assets', 'currency')
    op.drop_column('assets', 'price')
    op.drop_column('assets', 'is_for_sale')
    op.drop_column('assets', 'condition_score')
    op.drop_column('assets', 'current_mileage')
    op.drop_column('assets', 'first_registration_date')
    op.add_column('asset_telemetry', sa.Column('mileage_unit', sa.VARCHAR(length=10), autoincrement=False, nullable=False))
    op.add_column('asset_telemetry', sa.Column('vqi_score', sa.NUMERIC(precision=5, scale=2), autoincrement=False, nullable=False))
    op.add_column('asset_telemetry', sa.Column('dbs_score', sa.NUMERIC(precision=5, scale=2), autoincrement=False, nullable=False))
    op.drop_constraint(None, 'asset_telemetry', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('asset_telemetry_asset_id_fkey'), 'asset_telemetry', 'assets', ['asset_id'], ['id'])
    op.add_column('asset_reviews', sa.Column('criteria_scores', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), autoincrement=False, nullable=False))
    op.drop_constraint(None, 'asset_reviews', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'asset_reviews', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('asset_reviews_asset_id_fkey'), 'asset_reviews', 'assets', ['asset_id'], ['id'])
    op.create_foreign_key(op.f('asset_reviews_user_id_fkey'), 'asset_reviews', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.add_column('asset_financials', sa.Column('acquisition_date', postgresql.TIMESTAMP(), autoincrement=False, nullable=True))
    op.add_column('asset_financials', sa.Column('acquisition_price', sa.NUMERIC(precision=18, scale=2), autoincrement=False, nullable=True))
    op.add_column('asset_financials', sa.Column('residual_value_estimate', sa.NUMERIC(precision=18, scale=2), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'asset_financials', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('asset_financials_asset_id_fkey'), 'asset_financials', 'assets', ['asset_id'], ['id'])
    op.alter_column('asset_financials', 'financing_type',
               existing_type=sa.VARCHAR(),
               nullable=True)
    op.drop_column('asset_financials', 'accounting_details')
    op.drop_column('asset_financials', 'activation_date')
    op.drop_column('asset_financials', 'vat_rate')
    op.drop_column('asset_financials', 'purchase_price_gross')
    op.drop_column('asset_financials', 'purchase_price_net')
    op.add_column('asset_events', sa.Column('data', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), autoincrement=False, nullable=False))
    op.add_column('asset_events', sa.Column('recorded_mileage', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('asset_events', sa.Column('registration_uuid', sa.UUID(), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'asset_events', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('asset_events_asset_id_fkey'), 'asset_events', 'assets', ['asset_id'], ['id'])
    op.create_index(op.f('ix_data_asset_events_registration_uuid'), 'asset_events', ['registration_uuid'], unique=False)
    op.add_column('asset_costs', sa.Column('mileage_at_cost', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('asset_costs', sa.Column('net_amount_local', sa.NUMERIC(precision=18, scale=2), autoincrement=False, nullable=True))
    op.add_column('asset_costs', sa.Column('exchange_rate_used', sa.NUMERIC(precision=18, scale=6), autoincrement=False, nullable=True))
    op.add_column('asset_costs', sa.Column('registration_uuid', sa.UUID(), autoincrement=False, nullable=True))
    op.add_column('asset_costs', sa.Column('vat_rate', sa.NUMERIC(precision=5, scale=2), autoincrement=False, nullable=True))
    op.add_column('asset_costs', sa.Column('amount_eur', sa.NUMERIC(precision=18, scale=2), autoincrement=False, nullable=True))
    op.add_column('asset_costs', sa.Column('amount_local', sa.NUMERIC(precision=18, scale=2), autoincrement=False, nullable=False))
    op.add_column('asset_costs', sa.Column('currency_local', sa.VARCHAR(length=3), autoincrement=False, nullable=False))
    op.add_column('asset_costs', sa.Column('cost_type', sa.VARCHAR(length=50), autoincrement=False, nullable=False))
    op.add_column('asset_costs', sa.Column('driver_id', sa.INTEGER(), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'asset_costs', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'asset_costs', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('asset_costs_organization_id_fkey'), 'asset_costs', 'organizations', ['organization_id'], ['id'])
    op.create_foreign_key(op.f('asset_costs_asset_id_fkey'), 'asset_costs', 'assets', ['asset_id'], ['id'])
    op.create_foreign_key(op.f('asset_costs_driver_id_fkey'), 'asset_costs', 'users', ['driver_id'], ['id'], referent_schema='identity')
    op.drop_index(op.f('ix_data_asset_costs_invoice_number'), table_name='asset_costs', schema='data')
    op.drop_index(op.f('ix_data_asset_costs_cost_category'), table_name='asset_costs', schema='data')
    op.create_index(op.f('ix_data_asset_costs_registration_uuid'), 'asset_costs', ['registration_uuid'], unique=False)
    op.drop_column('asset_costs', 'invoice_number')
    op.drop_column('asset_costs', 'currency')
    op.drop_column('asset_costs', 'amount_net')
    op.drop_column('asset_costs', 'cost_category')
    op.add_column('asset_assignments', sa.Column('branch_id', sa.UUID(), autoincrement=False, nullable=True))
    op.add_column('asset_assignments', sa.Column('released_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True))
    op.add_column('asset_assignments', sa.Column('assigned_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False))
    op.drop_constraint(None, 'asset_assignments', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'asset_assignments', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('asset_assignments_organization_id_fkey'), 'asset_assignments', 'organizations', ['organization_id'], ['id'])
    op.create_foreign_key(op.f('asset_assignments_asset_id_fkey'), 'asset_assignments', 'assets', ['asset_id'], ['id'])
    op.create_foreign_key(op.f('asset_assignments_branch_id_fkey'), 'asset_assignments', 'branches', ['branch_id'], ['id'])
    op.drop_constraint(None, 'addresses', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('addresses_postal_code_id_fkey'), 'addresses', 'geo_postal_codes', ['postal_code_id'], ['id'])
    op.drop_table('vehicle_ownership_history', schema='data')
    op.drop_index(op.f('ix_data_vehicle_logbook_trip_type'), table_name='vehicle_logbook', schema='data')
    op.drop_table('vehicle_logbook', schema='data')
    op.drop_table('asset_inspections', schema='data')
    # ### end Alembic commands ###
