From 7ad519968a3df73f7fe5da6aa2be09eb1b3bd58f Mon Sep 17 00:00:00 2001 From: AlexeerCT Date: Tue, 3 Mar 2026 09:11:06 -0600 Subject: [PATCH] refactor: Remove create_transportation_tables function and related logic --- backend/main.py | 135 +----------------------------------------------- 1 file changed, 1 insertion(+), 134 deletions(-) diff --git a/backend/main.py b/backend/main.py index 8b468c74..c8fffaa3 100644 --- a/backend/main.py +++ b/backend/main.py @@ -150,146 +150,13 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE def run_migrations(): subprocess.run(["alembic", "upgrade", "head"], check=True) - -def create_transportation_tables(): - """Crea las tablas de transporte directamente si no existen. - Se usa en lugar de una migración Alembic para evitar gestionar versiones. - """ - from sqlalchemy import text - from core.database import core_engine - - ddl_statements = [ - """ - CREATE TABLE IF NOT EXISTS a76.transporter ( - transporter_key VARCHAR(23) PRIMARY KEY, - name VARCHAR(256), - short_name VARCHAR(10), - responsible VARCHAR(100), - rfc VARCHAR(30), - streets VARCHAR(100), - postal_code VARCHAR(15), - city VARCHAR(30), - state VARCHAR(30), - country VARCHAR(3), - loader_code VARCHAR(9), - caat_code VARCHAR(49), - transport_code VARCHAR(8), - transport_interface_type VARCHAR(20), - ftp_server VARCHAR(200), - ftp_user VARCHAR(200), - ftp_password VARCHAR(100), - ftp_directory VARCHAR(1000), - filler_code VARCHAR(20), - tenant_id INTEGER NOT NULL REFERENCES core.tenants(id), - company_id INTEGER NOT NULL REFERENCES a76.company(id), - created_at TIMESTAMP NOT NULL DEFAULT now(), - updated_at TIMESTAMP NOT NULL DEFAULT now(), - deleted_at TIMESTAMP - ); - """, - """ - CREATE TABLE IF NOT EXISTS a76.trailer ( - trailer_number VARCHAR(20) PRIMARY KEY, - ace_trailer_number VARCHAR(10), - trailer_type_key VARCHAR(2), - seal VARCHAR(15), - entity_code VARCHAR(1), - plate_number VARCHAR(17), - state VARCHAR(30), - country VARCHAR(3), - container_key VARCHAR(3), - tenant_id INTEGER NOT NULL REFERENCES core.tenants(id), - company_id INTEGER NOT NULL REFERENCES a76.company(id), - created_at TIMESTAMP NOT NULL DEFAULT now(), - updated_at TIMESTAMP NOT NULL DEFAULT now(), - deleted_at TIMESTAMP - ); - """, - """ - CREATE TABLE IF NOT EXISTS a76.vehicle ( - vehicle_key VARCHAR(14) PRIMARY KEY, - ace_vehicle_key VARCHAR(10), - transporter_key VARCHAR(23), - transport_identifier VARCHAR(30), - transport_type VARCHAR(2), - entity_code VARCHAR(1), - transponder_number VARCHAR(16), - dot_number VARCHAR(8), - plate_number VARCHAR(17), - city VARCHAR(30), - state VARCHAR(30), - country VARCHAR(3), - seal VARCHAR(49), - insurance_company_name VARCHAR(30), - insurance_number VARCHAR(20), - insurance_amount NUMERIC(13, 2), - insurance_date INTEGER, - box_number VARCHAR(300), - brand VARCHAR(20), - year VARCHAR(4), - series VARCHAR(30), - description VARCHAR(100), - engine_number VARCHAR(50), - sct_permission VARCHAR(40), - color VARCHAR(20), - container_key VARCHAR(3), - tenant_id INTEGER NOT NULL REFERENCES core.tenants(id), - company_id INTEGER NOT NULL REFERENCES a76.company(id), - created_at TIMESTAMP NOT NULL DEFAULT now(), - updated_at TIMESTAMP NOT NULL DEFAULT now(), - deleted_at TIMESTAMP - ); - """, - ] - - with core_engine.connect() as conn: - for stmt in ddl_statements: - conn.execute(text(stmt)) - # Ampliar columnas que pudieron haberse creado con tamaño incorrecto - conn.execute(text(""" - DO $$ - BEGIN - -- Fix transporter_key si fue creada como VARCHAR(5) - IF EXISTS ( - SELECT 1 FROM information_schema.columns - WHERE table_schema='a76' AND table_name='transporter' - AND column_name='transporter_key' - AND character_maximum_length < 23 - ) THEN - ALTER TABLE a76.transporter ALTER COLUMN transporter_key TYPE VARCHAR(23); - END IF; - -- Fix filler_code si fue creada como VARCHAR(4) - IF EXISTS ( - SELECT 1 FROM information_schema.columns - WHERE table_schema='a76' AND table_name='transporter' - AND column_name='filler_code' - AND character_maximum_length < 20 - ) THEN - ALTER TABLE a76.transporter ALTER COLUMN filler_code TYPE VARCHAR(20); - END IF; - - -- Eliminar constraint de trailer_type_key en trailer si existe - IF EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE constraint_name='trailer_trailer_type_key_fkey' - AND table_schema='a76' AND table_name='trailer' - ) THEN - ALTER TABLE a76.trailer DROP CONSTRAINT trailer_trailer_type_key_fkey; - END IF; - END$$; - """)) - conn.commit() - logger.info("Tablas de transporte verificadas/creadas correctamente.") - - # Inicializar la base de datos @app.on_event("startup") async def on_startup(): """Evento de inicio de la aplicación""" logger.info("Iniciando la aplicación Anexo76...") init_db() - run_migrations() - create_transportation_tables() + run_migrations() logger.info("Base de datos inicializada correctamente.")