- Implemented SQLAlchemy models for incoterms, invoice types, material types, payment methods, pedimento codes, pedimento regimens, sectors, states, transport modes, transport types, and valuation methods. - Added seed data for each reference data model to populate the database with initial values. - Ensured primary key constraints and appropriate data types for each model. - Established relationships where necessary, particularly in pedimento codes and regimens.
170 lines
8.0 KiB
Python
170 lines
8.0 KiB
Python
"""seed_initial_data
|
|
|
|
Revision ID: 7937209f9718
|
|
Revises: 531bf8cdae06
|
|
Create Date: 2025-10-19 18:23:55.258800
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
from api.v1.modules.public.reference_data.pedimento_codes.seed import seed as pedimento_codes_seed
|
|
from api.v1.modules.public.reference_data.pedimento_regimens.seed import seed as pedimento_regimens_seed
|
|
from api.v1.modules.public.reference_data.code_pedimento_regimens.seed import seed as code_pedimento_regimens_seed
|
|
from api.v1.modules.public.reference_data.containers.seed import seed as container_types_seed
|
|
from api.v1.modules.public.reference_data.countries.seed import seed as countries_seed
|
|
from api.v1.modules.public.reference_data.currency_types.seed import seed as currency_types_seed
|
|
from api.v1.modules.public.reference_data.customs_sections.seed import seed as customs_sections_seed
|
|
from api.v1.modules.public.reference_data.customs_warehouses.seed import seed as customs_warehouses_seed
|
|
from api.v1.modules.public.reference_data.incoterms.seed import seed as incoterms_seed
|
|
from api.v1.modules.public.reference_data.invoice_types.seed import seed as invoice_types_seed
|
|
from api.v1.modules.public.reference_data.material_types.seed import seed as material_types_seed
|
|
from api.v1.modules.public.reference_data.payment_methods.seed import seed as payment_methods_seed
|
|
from api.v1.modules.public.reference_data.sectors.seed import seed as sectors_seed
|
|
from api.v1.modules.public.reference_data.transport_modes.seed import seed as transport_modes_seed
|
|
from api.v1.modules.public.reference_data.transport_types.seed import seed as transport_types_seed
|
|
from api.v1.modules.public.reference_data.valuation_methods.seed import seed as valuation_methods_seed
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = '7937209f9718'
|
|
down_revision: Union[str, Sequence[str], None] = '531bf8cdae06'
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = '531bf8cdae06'
|
|
|
|
def upgrade() -> None:
|
|
"""Upgrade schema."""
|
|
#Seeds
|
|
values_pc = ", ".join([f"('{code}', '{desc.replace(chr(39), chr(39)*2)}')" for code, desc in pedimento_codes_seed])
|
|
op.execute(f"""
|
|
INSERT INTO pedimento_codes (code, description) VALUES
|
|
{values_pc}
|
|
ON CONFLICT (code) DO NOTHING;
|
|
""")
|
|
|
|
values_pr = ", ".join([f"('{code}', '{desc.replace(chr(39), chr(39)*2)}')" for code, desc in pedimento_regimens_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.pedimento_regimens (code, description) VALUES
|
|
{values_pr}
|
|
ON CONFLICT (code) DO NOTHING;
|
|
""")
|
|
|
|
values_cpr = ", ".join([f"('{ped_code}', '{reg_code}', '{type_code}')" for ped_code, reg_code, type_code in code_pedimento_regimens_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.code_pedimento_regimens (pedimento_code, regimen_code, type_code) VALUES
|
|
{values_cpr}
|
|
""")
|
|
|
|
values_c = ", ".join([f"('{key}', '{desc.replace(chr(39), chr(39)*2)}')" for key, desc in container_types_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.containers (key, description) VALUES
|
|
{values_c}
|
|
ON CONFLICT (key) DO NOTHING;
|
|
""")
|
|
|
|
values_country = ", ".join([f"('{m3_key}', '{mex_key}', '{ame_key}', '{desc_es.replace(chr(39), chr(39)*2)}', '{desc_en.replace(chr(39), chr(39)*2)}')" for m3_key, mex_key, ame_key, desc_es, desc_en in countries_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.countries (m3_key, mex_key, ame_key, description_es, description_en) VALUES
|
|
{values_country}
|
|
ON CONFLICT (m3_key) DO NOTHING;
|
|
""")
|
|
|
|
values_ct = ", ".join([f"('{code}', '{currency_name.replace(chr(39), chr(39)*2)}', '{country_desc.replace(chr(39), chr(39)*2)}')" for code, currency_name, country_desc in currency_types_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.currency_types (code, currency_name, country_description) VALUES
|
|
{values_ct}
|
|
ON CONFLICT (code) DO NOTHING;
|
|
""")
|
|
|
|
values_cs = ", ".join([f"('{code}', '{name.replace(chr(39), chr(39)*2)}')" for code, name in customs_sections_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.customs_sections (customs_code, section_name) VALUES
|
|
{values_cs}
|
|
ON CONFLICT (customs_code) DO NOTHING;
|
|
""")
|
|
|
|
values_cw = ", ".join([f"('{key}', '{customs.replace(chr(39), chr(39)*2)}', '{fiscalized_warehouse.replace(chr(39), chr(39)*2)}')" for key, customs, fiscalized_warehouse in customs_warehouses_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.customs_warehouses (key, customs, fiscalized_warehouse)
|
|
VALUES
|
|
{values_cw}
|
|
ON CONFLICT (key, customs) DO NOTHING;
|
|
""")
|
|
|
|
values_incoterms = ", ".join([f"('{code}', '{desc_es.replace(chr(39), chr(39)*2)}', '{desc_en.replace(chr(39), chr(39)*2)}')" for code, desc_es, desc_en in incoterms_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.incoterms (code, description_es, description_en) VALUES
|
|
{values_incoterms}
|
|
ON CONFLICT (code) DO NOTHING;
|
|
""")
|
|
|
|
values_it = ", ".join([f"('{key}', '{desc.replace(chr(39), chr(39)*2)}', '{note.replace(chr(39), chr(39)*2)}', '{type.replace(chr(39), chr(39)*2)}')" for key, desc, note, type in invoice_types_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.invoice_types (key, description, note, type) VALUES
|
|
{values_it}
|
|
ON CONFLICT (key) DO NOTHING;
|
|
""")
|
|
|
|
values_mt = ", ".join([f"('{key}', '{desc.replace(chr(39), chr(39)*2)}', '{category.replace(chr(39), chr(39)*2)}')" for key, desc, category in material_types_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.material_types (key, description, type) VALUES
|
|
{values_mt}
|
|
ON CONFLICT (key) DO NOTHING;
|
|
""")
|
|
|
|
values_pm = ", ".join([f"('{key}', '{desc.replace(chr(39), chr(39)*2)}')" for key, desc in payment_methods_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.payment_methods (key, description) VALUES
|
|
{values_pm}
|
|
ON CONFLICT (key) DO NOTHING;
|
|
""")
|
|
|
|
values_sectors = ", ".join([f"('{key}', '{desc.replace(chr(39), chr(39)*2)}', '{authorized}')" for key, desc, authorized in sectors_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.sectors (key, description, authorized) VALUES
|
|
{values_sectors}
|
|
ON CONFLICT (key) DO NOTHING;
|
|
""")
|
|
|
|
values_tm = ", ".join([f"('{key}', '{name.replace(chr(39), chr(39)*2)}')" for key, name in transport_modes_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.transport_modes (key, name) VALUES
|
|
{values_tm}
|
|
ON CONFLICT (key) DO NOTHING;
|
|
""")
|
|
|
|
values_tt = ", ".join([f"('{code}', '{desc.replace(chr(39), chr(39)*2)}')" for code, desc in transport_types_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.transport_types (transport_code, description) VALUES
|
|
{values_tt}
|
|
ON CONFLICT (transport_code) DO NOTHING;
|
|
""")
|
|
|
|
values_vm = ", ".join([f"('{key}', '{desc.replace(chr(39), chr(39)*2)}')" for key, desc in valuation_methods_seed])
|
|
op.execute(f"""
|
|
INSERT INTO public.valuation_methods (key, description) VALUES
|
|
{values_vm}
|
|
ON CONFLICT (key) DO NOTHING;
|
|
""")
|
|
|
|
def downgrade() -> None:
|
|
"""Downgrade schema."""
|
|
|
|
op.execute("DELETE FROM public.valuation_methods;")
|
|
op.execute("DELETE FROM public.transport_types;")
|
|
op.execute("DELETE FROM public.transport_modes;")
|
|
op.execute("DELETE FROM public.sectors;")
|
|
op.execute("DELETE FROM public.payment_methods;")
|
|
op.execute("DELETE FROM public.material_types;")
|
|
op.execute("DELETE FROM public.invoice_types;")
|
|
op.execute("DELETE FROM public.incoterms;")
|
|
op.execute("DELETE FROM public.customs_warehouses;")
|
|
op.execute("DELETE FROM public.customs_sections;")
|
|
op.execute("DELETE FROM public.currency_types;")
|
|
op.execute("DELETE FROM public.countries;")
|
|
op.execute("DELETE FROM public.containers;")
|
|
op.execute("DELETE FROM public.code_pedimento_regimens;")
|
|
op.execute("DELETE FROM public.pedimento_regimens;")
|
|
op.execute("DELETE FROM public.pedimento_codes;")
|