feat: Implement multi-tenancy support in middleware and security layers

- Enhanced TenantMiddleware to validate tenant information from JWT tokens.
- Added LicenseValidationMiddleware to check tenant licenses before processing requests.
- Updated security utilities to extract tenant information from tokens and validate company access.
- Introduced CompanyStore to manage active company state and handle company switching in the frontend.
- Modified API routes to include company_id in requests for better resource management.
- Improved logging and error handling throughout the middleware and API layers.
- Updated frontend components to reflect changes in company management and selection.
- Added new API route for fetching user's companies with proper authentication handling.
This commit is contained in:
2025-11-11 14:00:56 -06:00
parent e1eb6bbd01
commit 52b8fcd434
242 changed files with 7067 additions and 3274 deletions

View File

@@ -13,6 +13,7 @@ logger = logging.getLogger(__name__)
# access to the values within the .ini file in use.
config = context.config
def get_database_url():
"""Obtiene la URL de la base de datos (PostgreSQL) desde variables de entorno o alembic.ini."""
# Intentar construir desde variables de entorno primero
@@ -41,6 +42,7 @@ def get_database_url():
return url
# Configurar la URL de la base de datos
database_url = get_database_url()
@@ -54,7 +56,7 @@ if os.environ.get("ALEMBIC_DEBUG"):
debug_url = before + "@" + after
except Exception:
debug_url = "postgresql://***:***@***"
logger.error("Error al ocultar la contraseña en la URL para debug.")
logger.error("Error al ocultar la contraseña en la URL para debug.")
config.set_main_option("sqlalchemy.url", database_url)
@@ -77,8 +79,9 @@ config = context.config
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def import_models_from_dir(dir_path: str):
"""Importa recursivamente cualquier archivo models.py desde dir_path y archivos en directorios models/"""
"""Importa recursivamente cualquier archivo models.py desde dir_path y archivos en directorios models/"""
for root, dirs, files in os.walk(dir_path):
# Importar archivos models.py directos
if "models.py" in files:
@@ -90,7 +93,7 @@ def import_models_from_dir(dir_path: str):
spec = importlib.util.spec_from_file_location(module_name, module_path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
# Importar todos los archivos .py en directorios llamados "models"
if os.path.basename(root) == "models":
for file in files:
@@ -99,18 +102,20 @@ def import_models_from_dir(dir_path: str):
rel_path = os.path.relpath(module_path, BASE_DIR)
module_name = rel_path.replace(os.sep, ".").replace(".py", "")
try:
spec = importlib.util.spec_from_file_location(module_name, module_path)
spec = importlib.util.spec_from_file_location(
module_name, module_path
)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
except Exception as e:
logger.warning(f"No se pudo importar {module_path}: {e}")
# Importar todos los models dentro de api/v1/modules y api/v1/modules/uploads
modules_dir = os.path.join(BASE_DIR, "api", "v1", "modules")
import_models_from_dir(modules_dir)
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
@@ -147,10 +152,7 @@ def run_migrations_online() -> None:
)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata
)
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
@@ -159,4 +161,4 @@ def run_migrations_online() -> None:
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
run_migrations_online()

View File

@@ -1,10 +1,11 @@
"""create material_types table
Revision ID: 531bf8cdae06
Revises:
Revises:
Create Date: 2025-10-19 18:23:39.613953
"""
from typing import Sequence, Union
from alembic import op
@@ -12,7 +13,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '531bf8cdae06'
revision: str = "531bf8cdae06"
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
@@ -21,124 +22,147 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('containers',
sa.Column('key', sa.String(length=3), nullable=False),
sa.Column('description', sa.String(length=500), nullable=False),
sa.PrimaryKeyConstraint('key', name='containers_pkey'),
schema='public'
op.create_table(
"containers",
sa.Column("key", sa.String(length=3), nullable=False),
sa.Column("description", sa.String(length=500), nullable=False),
sa.PrimaryKeyConstraint("key", name="containers_pkey"),
schema="public",
)
op.create_table('countries',
sa.Column('m3_key', sa.String(length=3), nullable=False),
sa.Column('mex_key', sa.String(length=2), nullable=False),
sa.Column('ame_key', sa.String(length=2), nullable=False),
sa.Column('description_es', sa.String(length=50), nullable=False),
sa.Column('description_en', sa.String(length=50), nullable=False),
sa.PrimaryKeyConstraint('m3_key', name='countries_pkey'),
schema='public'
op.create_table(
"countries",
sa.Column("m3_key", sa.String(length=3), nullable=False),
sa.Column("mex_key", sa.String(length=2), nullable=False),
sa.Column("ame_key", sa.String(length=2), nullable=False),
sa.Column("description_es", sa.String(length=50), nullable=False),
sa.Column("description_en", sa.String(length=50), nullable=False),
sa.PrimaryKeyConstraint("m3_key", name="countries_pkey"),
schema="public",
)
op.create_index('ak_country_ame', 'countries', ['ame_key'], unique=True, schema='public')
op.create_table('currency_types',
sa.Column('code', sa.String(length=3), nullable=False),
sa.Column('currency_name', sa.String(length=15), nullable=False),
sa.Column('country_description', sa.String(length=50), nullable=False),
sa.PrimaryKeyConstraint('code', name='currency_types_pkey'),
schema='public'
op.create_index(
"ak_country_ame", "countries", ["ame_key"], unique=True, schema="public"
)
op.create_table('customs_sections',
sa.Column('customs_code', sa.String(length=3), nullable=False),
sa.Column('section_name', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('customs_code', name='customs_code_pkey'),
schema='public'
op.create_table(
"currency_types",
sa.Column("code", sa.String(length=3), nullable=False),
sa.Column("currency_name", sa.String(length=15), nullable=False),
sa.Column("country_description", sa.String(length=50), nullable=False),
sa.PrimaryKeyConstraint("code", name="currency_types_pkey"),
schema="public",
)
op.create_table('customs_warehouses',
sa.Column('key', sa.String(length=3), nullable=False),
sa.Column('customs', sa.String(length=100), nullable=False),
sa.Column('fiscalized_warehouse', sa.String(length=1000), nullable=False),
sa.PrimaryKeyConstraint('key', 'customs', name='pk_customs_warehouse'),
schema='public'
op.create_table(
"customs_sections",
sa.Column("customs_code", sa.String(length=3), nullable=False),
sa.Column("section_name", sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint("customs_code", name="customs_code_pkey"),
schema="public",
)
op.create_table('incoterms',
sa.Column('code', sa.String(length=5), nullable=False),
sa.Column('description_es', sa.String(length=256), nullable=False),
sa.Column('description_en', sa.String(length=256), nullable=False),
sa.PrimaryKeyConstraint('code', name='incoterms_pkey'),
schema='public'
op.create_table(
"customs_warehouses",
sa.Column("key", sa.String(length=3), nullable=False),
sa.Column("customs", sa.String(length=100), nullable=False),
sa.Column("fiscalized_warehouse", sa.String(length=1000), nullable=False),
sa.PrimaryKeyConstraint("key", "customs", name="pk_customs_warehouse"),
schema="public",
)
op.create_table('invoice_types',
sa.Column('key', sa.String(length=5), nullable=False),
sa.Column('description', sa.String(length=50), nullable=False),
sa.Column('note', sa.String(length=500), nullable=False),
sa.Column('type', sa.String(length=15), nullable=False),
sa.PrimaryKeyConstraint('key', name='invoice_types_pkey'),
schema='public'
op.create_table(
"incoterms",
sa.Column("code", sa.String(length=5), nullable=False),
sa.Column("description_es", sa.String(length=256), nullable=False),
sa.Column("description_en", sa.String(length=256), nullable=False),
sa.PrimaryKeyConstraint("code", name="incoterms_pkey"),
schema="public",
)
op.create_table('material_types',
sa.Column('key', sa.String(length=10), nullable=False),
sa.Column('type', sa.String(length=15), nullable=False),
sa.Column('description', sa.String(length=256), nullable=False),
sa.PrimaryKeyConstraint('key', name='material_types_pkey'),
schema='public'
op.create_table(
"invoice_types",
sa.Column("key", sa.String(length=5), nullable=False),
sa.Column("description", sa.String(length=50), nullable=False),
sa.Column("note", sa.String(length=500), nullable=False),
sa.Column("type", sa.String(length=15), nullable=False),
sa.PrimaryKeyConstraint("key", name="invoice_types_pkey"),
schema="public",
)
op.create_table('payment_methods',
sa.Column('key', sa.String(length=2), nullable=False),
sa.Column('description', sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint('key', name='payment_methods_pkey'),
schema='public'
op.create_table(
"material_types",
sa.Column("key", sa.String(length=10), nullable=False),
sa.Column("type", sa.String(length=15), nullable=False),
sa.Column("description", sa.String(length=256), nullable=False),
sa.PrimaryKeyConstraint("key", name="material_types_pkey"),
schema="public",
)
op.create_table('pedimento_codes',
sa.Column('code', sa.String(length=3), nullable=False),
sa.Column('description', sa.String(length=250), nullable=False),
sa.PrimaryKeyConstraint('code', name='pedimento_codes_pkey'),
schema='public'
op.create_table(
"payment_methods",
sa.Column("key", sa.String(length=2), nullable=False),
sa.Column("description", sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint("key", name="payment_methods_pkey"),
schema="public",
)
op.create_table('pedimento_regimens',
sa.Column('code', sa.String(length=3), nullable=False),
sa.Column('description', sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint('code', name='pedimento_regimens_pkey'),
schema='public'
op.create_table(
"pedimento_codes",
sa.Column("code", sa.String(length=3), nullable=False),
sa.Column("description", sa.String(length=250), nullable=False),
sa.PrimaryKeyConstraint("code", name="pedimento_codes_pkey"),
schema="public",
)
op.create_table('sectors',
sa.Column('key', sa.String(length=8), nullable=False),
sa.Column('description', sa.String(length=150), nullable=False),
sa.Column('authorized', sa.SmallInteger(), nullable=False),
sa.PrimaryKeyConstraint('key', name='sectors_pkey'),
schema='public'
op.create_table(
"pedimento_regimens",
sa.Column("code", sa.String(length=3), nullable=False),
sa.Column("description", sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint("code", name="pedimento_regimens_pkey"),
schema="public",
)
op.create_table('states',
sa.Column('m3_key', sa.String(length=3), nullable=False),
sa.Column('description', sa.String(length=50), nullable=False),
sa.Column('mex_key', sa.String(length=3), nullable=True),
sa.Column('ame_key', sa.String(length=2), nullable=True),
sa.PrimaryKeyConstraint('m3_key', 'description', name='states_pkey'),
schema='public'
op.create_table(
"sectors",
sa.Column("key", sa.String(length=8), nullable=False),
sa.Column("description", sa.String(length=150), nullable=False),
sa.Column("authorized", sa.SmallInteger(), nullable=False),
sa.PrimaryKeyConstraint("key", name="sectors_pkey"),
schema="public",
)
op.create_table('transport_modes',
sa.Column('key', sa.String(length=3), nullable=False),
sa.Column('name', sa.String(length=30), nullable=False),
sa.PrimaryKeyConstraint('key', name='transport_modes_pkey'),
schema='public'
op.create_table(
"states",
sa.Column("m3_key", sa.String(length=3), nullable=False),
sa.Column("description", sa.String(length=50), nullable=False),
sa.Column("mex_key", sa.String(length=3), nullable=True),
sa.Column("ame_key", sa.String(length=2), nullable=True),
sa.PrimaryKeyConstraint("m3_key", "description", name="states_pkey"),
schema="public",
)
op.create_table('transport_types',
sa.Column('transport_code', sa.String(length=2), nullable=False),
sa.Column('description', sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint('transport_code', name='transport_types_pkey'),
schema='public'
op.create_table(
"transport_modes",
sa.Column("key", sa.String(length=3), nullable=False),
sa.Column("name", sa.String(length=30), nullable=False),
sa.PrimaryKeyConstraint("key", name="transport_modes_pkey"),
schema="public",
)
op.create_table('valuation_methods',
sa.Column('key', sa.String(length=2), nullable=False),
sa.Column('description', sa.String(length=200), nullable=False),
sa.PrimaryKeyConstraint('key', name='valuation_methods_pkey'),
schema='public'
)
op.create_table('code_pedimento_regimens',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('pedimento_code', sa.String(length=3), nullable=False),
sa.Column('regimen_code', sa.String(length=3), nullable=False),
sa.Column('type_code', sa.String(length=1), nullable=True),
sa.ForeignKeyConstraint(['pedimento_code'], ['public.pedimento_codes.code'], name='fk_codeped'),
sa.ForeignKeyConstraint(['regimen_code'], ['public.pedimento_regimens.code'], name='fk_regimenped'),
sa.PrimaryKeyConstraint('id', name='clave_pedimento_regimens_pkey'),
schema='public'
op.create_table(
"transport_types",
sa.Column("transport_code", sa.String(length=2), nullable=False),
sa.Column("description", sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint("transport_code", name="transport_types_pkey"),
schema="public",
)
op.create_table(
"valuation_methods",
sa.Column("key", sa.String(length=2), nullable=False),
sa.Column("description", sa.String(length=200), nullable=False),
sa.PrimaryKeyConstraint("key", name="valuation_methods_pkey"),
schema="public",
)
op.create_table(
"code_pedimento_regimens",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("pedimento_code", sa.String(length=3), nullable=False),
sa.Column("regimen_code", sa.String(length=3), nullable=False),
sa.Column("type_code", sa.String(length=1), nullable=True),
sa.ForeignKeyConstraint(
["pedimento_code"], ["public.pedimento_codes.code"], name="fk_codeped"
),
sa.ForeignKeyConstraint(
["regimen_code"], ["public.pedimento_regimens.code"], name="fk_regimenped"
),
sa.PrimaryKeyConstraint("id", name="clave_pedimento_regimens_pkey"),
schema="public",
)
# ### end Alembic commands ###
@@ -146,22 +170,22 @@ def upgrade() -> None:
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('code_pedimento_regimens', schema='public')
op.drop_table('valuation_methods', schema='public')
op.drop_table('transport_types', schema='public')
op.drop_table('transport_modes', schema='public')
op.drop_table('states', schema='public')
op.drop_table('sectors', schema='public')
op.drop_table('pedimento_regimens', schema='public')
op.drop_table('pedimento_codes', schema='public')
op.drop_table('payment_methods', schema='public')
op.drop_table('material_types', schema='public')
op.drop_table('invoice_types', schema='public')
op.drop_table('incoterms', schema='public')
op.drop_table('customs_warehouses', schema='public')
op.drop_table('customs_sections', schema='public')
op.drop_table('currency_types', schema='public')
op.drop_index('ak_country_ame', table_name='countries', schema='public')
op.drop_table('countries', schema='public')
op.drop_table('containers', schema='public')
op.drop_table("code_pedimento_regimens", schema="public")
op.drop_table("valuation_methods", schema="public")
op.drop_table("transport_types", schema="public")
op.drop_table("transport_modes", schema="public")
op.drop_table("states", schema="public")
op.drop_table("sectors", schema="public")
op.drop_table("pedimento_regimens", schema="public")
op.drop_table("pedimento_codes", schema="public")
op.drop_table("payment_methods", schema="public")
op.drop_table("material_types", schema="public")
op.drop_table("invoice_types", schema="public")
op.drop_table("incoterms", schema="public")
op.drop_table("customs_warehouses", schema="public")
op.drop_table("customs_sections", schema="public")
op.drop_table("currency_types", schema="public")
op.drop_index("ak_country_ame", table_name="countries", schema="public")
op.drop_table("countries", schema="public")
op.drop_table("containers", schema="public")
# ### end Alembic commands ###

View File

@@ -5,152 +5,293 @@ 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.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.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.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
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'
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'
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"""
# 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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
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"""
"""
)
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"""
"""
)
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"""
"""
)
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"""
"""
)
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;")