"""Add_missing_system_and_catalog_tables

Revision ID: f7505332b1c8
Revises: 78f5b29d0714
Create Date: 2026-02-24 00:44:31.612591

"""
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 = 'f7505332b1c8'
down_revision: Union[str, Sequence[str], None] = '78f5b29d0714'
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('pending_actions',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('requester_id', sa.Integer(), nullable=False),
    sa.Column('approver_id', sa.Integer(), nullable=True),
    sa.Column('status', sa.Enum('pending', 'approved', 'rejected', 'expired', name='actionstatus', schema='system'), nullable=False),
    sa.Column('action_type', sa.String(length=50), nullable=False),
    sa.Column('payload', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.Column('reason', sa.String(length=255), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('expires_at', sa.DateTime(timezone=True), server_default=sa.text("now() + interval '24 hours'"), nullable=False),
    sa.Column('processed_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['approver_id'], ['identity.users.id'], ),
    sa.ForeignKeyConstraint(['requester_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='system'
    )
    op.create_index(op.f('ix_system_pending_actions_id'), 'pending_actions', ['id'], unique=False, schema='system')
    # op.drop_table('spatial_ref_sys', schema='public')
    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_organization_id_fkey'), 'asset_assignments', type_='foreignkey')
    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.create_foreign_key(None, 'asset_assignments', 'branches', ['branch_id'], ['id'], source_schema='data', referent_schema='data')
    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_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.drop_constraint(op.f('asset_costs_driver_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.create_foreign_key(None, 'asset_costs', 'users', ['driver_id'], ['id'], source_schema='data', referent_schema='identity')
    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_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_constraint(op.f('asset_reviews_asset_id_fkey'), 'asset_reviews', type_='foreignkey')
    op.drop_constraint(op.f('asset_reviews_user_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_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_constraint(op.f('assets_catalog_id_fkey'), 'assets', type_='foreignkey')
    op.drop_constraint(op.f('assets_operator_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_current_organization_id_fkey'), 'assets', type_='foreignkey')
    op.drop_constraint(op.f('assets_owner_person_id_fkey'), 'assets', type_='foreignkey')
    op.drop_constraint(op.f('assets_operator_org_id_fkey'), 'assets', type_='foreignkey')
    op.create_foreign_key(None, 'assets', 'organizations', ['current_organization_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', 'persons', ['operator_person_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'assets', 'organizations', ['owner_org_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'assets', 'vehicle_catalog', ['catalog_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'assets', 'organizations', ['operator_org_id'], ['id'], source_schema='data', referent_schema='data')
    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', 'addresses', ['address_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'branches', 'organizations', ['organization_id'], ['id'], source_schema='data', referent_schema='data')
    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('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_model_definition_id_fkey'), 'model_feature_maps', type_='foreignkey')
    op.drop_constraint(op.f('model_feature_maps_feature_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_org_id_fkey'), 'org_subscriptions', type_='foreignkey')
    op.drop_constraint(op.f('org_subscriptions_tier_id_fkey'), 'org_subscriptions', type_='foreignkey')
    op.create_foreign_key(None, 'org_subscriptions', 'organizations', ['org_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'org_subscriptions', 'subscription_tiers', ['tier_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_person_id_fkey'), 'organization_members', type_='foreignkey')
    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.create_foreign_key(None, 'organization_members', 'users', ['user_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.create_foreign_key(None, 'organization_members', 'persons', ['person_id'], ['id'], source_schema='data', referent_schema='identity')
    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_address_id_fkey'), 'organizations', type_='foreignkey')
    op.drop_constraint(op.f('organizations_owner_id_fkey'), 'organizations', type_='foreignkey')
    op.create_foreign_key(None, 'organizations', 'users', ['owner_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'organizations', 'addresses', ['address_id'], ['id'], source_schema='data', referent_schema='data')
    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_branch_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_user_id_fkey'), 'ratings', type_='foreignkey')
    op.create_foreign_key(None, 'ratings', 'users', ['author_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'ratings', 'users', ['target_user_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.create_foreign_key(None, 'ratings', 'branches', ['target_branch_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_constraint(op.f('service_expertises_expertise_id_fkey'), 'service_expertises', type_='foreignkey')
    op.drop_constraint(op.f('service_expertises_service_id_fkey'), 'service_expertises', type_='foreignkey')
    op.create_foreign_key(None, 'service_expertises', 'expertise_tags', ['expertise_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'service_expertises', 'service_profiles', ['service_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_badge_id_fkey'), 'user_badges', type_='foreignkey')
    op.drop_constraint(op.f('user_badges_user_id_fkey'), 'user_badges', type_='foreignkey')
    op.create_foreign_key(None, 'user_badges', 'users', ['user_id'], ['id'], source_schema='data', referent_schema='identity')
    op.create_foreign_key(None, 'user_badges', 'badges', ['badge_id'], ['id'], source_schema='data', referent_schema='data')
    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_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_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('vehicle_ownerships_vehicle_id_fkey'), 'vehicle_ownerships', type_='foreignkey')
    op.drop_constraint(op.f('vehicle_ownerships_user_id_fkey'), 'vehicle_ownerships', type_='foreignkey')
    op.create_foreign_key(None, 'vehicle_ownerships', 'assets', ['vehicle_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'vehicle_ownerships', 'users', ['user_id'], ['id'], source_schema='data', referent_schema='identity')
    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_ownerships', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'vehicle_ownerships', schema='data', type_='foreignkey')
    op.create_foreign_key(op.f('vehicle_ownerships_user_id_fkey'), 'vehicle_ownerships', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.create_foreign_key(op.f('vehicle_ownerships_vehicle_id_fkey'), 'vehicle_ownerships', 'assets', ['vehicle_id'], ['id'])
    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(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(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_user_id_fkey'), 'user_badges', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.create_foreign_key(op.f('user_badges_badge_id_fkey'), 'user_badges', 'badges', ['badge_id'], ['id'])
    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_service_id_fkey'), 'service_expertises', 'service_profiles', ['service_id'], ['id'])
    op.create_foreign_key(op.f('service_expertises_expertise_id_fkey'), 'service_expertises', 'expertise_tags', ['expertise_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_user_id_fkey'), 'ratings', 'users', ['target_user_id'], ['id'], referent_schema='identity')
    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_branch_id_fkey'), 'ratings', 'branches', ['target_branch_id'], ['id'])
    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_owner_id_fkey'), 'organizations', 'users', ['owner_id'], ['id'], referent_schema='identity')
    op.create_foreign_key(op.f('organizations_address_id_fkey'), 'organizations', 'addresses', ['address_id'], ['id'])
    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_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.create_foreign_key(op.f('organization_members_person_id_fkey'), 'organization_members', 'persons', ['person_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_tier_id_fkey'), 'org_subscriptions', 'subscription_tiers', ['tier_id'], ['id'])
    op.create_foreign_key(op.f('org_subscriptions_org_id_fkey'), 'org_subscriptions', 'organizations', ['org_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_feature_id_fkey'), 'model_feature_maps', 'feature_definitions', ['feature_id'], ['id'])
    op.create_foreign_key(op.f('model_feature_maps_model_definition_id_fkey'), 'model_feature_maps', 'vehicle_model_definitions', ['model_definition_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.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.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.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_operator_org_id_fkey'), 'assets', 'organizations', ['operator_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_current_organization_id_fkey'), 'assets', 'organizations', ['current_organization_id'], ['id'])
    op.create_foreign_key(op.f('assets_owner_org_id_fkey'), 'assets', 'organizations', ['owner_org_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_catalog_id_fkey'), 'assets', 'vehicle_catalog', ['catalog_id'], ['id'])
    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.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_user_id_fkey'), 'asset_reviews', 'users', ['user_id'], ['id'], referent_schema='identity')
    op.create_foreign_key(op.f('asset_reviews_asset_id_fkey'), 'asset_reviews', 'assets', ['asset_id'], ['id'])
    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.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.drop_constraint(None, 'asset_costs', schema='data', type_='foreignkey')
    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_driver_id_fkey'), 'asset_costs', 'users', ['driver_id'], ['id'], referent_schema='identity')
    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.drop_constraint(None, 'asset_assignments', schema='data', type_='foreignkey')
    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_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.create_foreign_key(op.f('asset_assignments_organization_id_fkey'), 'asset_assignments', 'organizations', ['organization_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.create_table('spatial_ref_sys',
    #sa.Column('srid', sa.INTEGER(), autoincrement=False, nullable=False),
    #sa.Column('auth_name', sa.VARCHAR(length=256), autoincrement=False, nullable=True),
    #sa.Column('auth_srid', sa.INTEGER(), autoincrement=False, nullable=True),
    #sa.Column('srtext', sa.VARCHAR(length=2048), autoincrement=False, nullable=True),
    #sa.Column('proj4text', sa.VARCHAR(length=2048), autoincrement=False, nullable=True),
    #sa.CheckConstraint('srid > 0 AND srid <= 998999', name=op.f('spatial_ref_sys_srid_check')),
    #sa.PrimaryKeyConstraint('srid', name=op.f('spatial_ref_sys_pkey')),
    #schema='public'
    #)
    op.drop_index(op.f('ix_system_pending_actions_id'), table_name='pending_actions', schema='system')
    op.drop_table('pending_actions', schema='system')
    # ### end Alembic commands ###
