Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| from sqlalchemy import text | |
| from database import engine | |
| def run_migrations(): | |
| print("Connecting to database...") | |
| with engine.connect() as conn: | |
| trans = conn.begin() | |
| try: | |
| # 1. Add estado_academico column to fact_rendimiento_academico | |
| print("Adding column estado_academico to fact_rendimiento_academico...") | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_rendimiento_academico | |
| ADD COLUMN IF NOT EXISTS estado_academico VARCHAR(50); | |
| """)) | |
| # 2. De-duplicate fact_marketing | |
| print("De-duplicating public.fact_marketing...") | |
| conn.execute(text(""" | |
| DELETE FROM public.fact_marketing a USING public.fact_marketing b | |
| WHERE a.id_hecho_mkt < b.id_hecho_mkt | |
| AND a.id_modulo = b.id_modulo | |
| AND a.id_tiempo = b.id_tiempo; | |
| """)) | |
| # Add constraint if not exists | |
| print("Adding constraint uq_fact_marketing_modulo_tiempo...") | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_marketing | |
| DROP CONSTRAINT IF EXISTS unique_modulo_tiempo; | |
| """)) | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_marketing | |
| DROP CONSTRAINT IF EXISTS uq_fact_marketing_modulo_tiempo; | |
| """)) | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_marketing | |
| ADD CONSTRAINT uq_fact_marketing_modulo_tiempo UNIQUE (id_modulo, id_tiempo); | |
| """)) | |
| # 3. De-duplicate fact_rentabilidad (public.fact_rentabilidad) | |
| print("De-duplicating public.fact_rentabilidad...") | |
| conn.execute(text(""" | |
| DELETE FROM public.fact_rentabilidad a USING public.fact_rentabilidad b | |
| WHERE a.id_hecho_rent < b.id_hecho_rent | |
| AND a.id_categoria = b.id_categoria | |
| AND a.id_tiempo = b.id_tiempo; | |
| """)) | |
| print("Adding constraint uq_fact_rentabilidad_categoria_tiempo...") | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_rentabilidad | |
| DROP CONSTRAINT IF EXISTS uq_fact_rentabilidad_categoria_tiempo; | |
| """)) | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_rentabilidad | |
| ADD CONSTRAINT uq_fact_rentabilidad_categoria_tiempo UNIQUE (id_categoria, id_tiempo); | |
| """)) | |
| # 4. De-duplicate fact_rendimiento_academico | |
| print("De-duplicating public.fact_rendimiento_academico...") | |
| conn.execute(text(""" | |
| DELETE FROM public.fact_rendimiento_academico a USING public.fact_rendimiento_academico b | |
| WHERE a.id_hecho_aca < b.id_hecho_aca | |
| AND a.id_estudiante = b.id_estudiante | |
| AND a.id_modulo = b.id_modulo; | |
| """)) | |
| print("Adding constraint uq_fact_rendimiento_estudiante_modulo...") | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_rendimiento_academico | |
| DROP CONSTRAINT IF EXISTS uq_fact_rendimiento_estudiante_modulo; | |
| """)) | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_rendimiento_academico | |
| ADD CONSTRAINT uq_fact_rendimiento_estudiante_modulo UNIQUE (id_estudiante, id_modulo); | |
| """)) | |
| # 5. De-duplicate fact_situacion_financiera | |
| print("De-duplicating public.fact_situacion_financiera...") | |
| conn.execute(text(""" | |
| DELETE FROM public.fact_situacion_financiera a USING public.fact_situacion_financiera b | |
| WHERE a.id_hecho_fin < b.id_hecho_fin | |
| AND a.id_estudiante = b.id_estudiante | |
| AND a.id_tiempo = b.id_tiempo; | |
| """)) | |
| print("Adding constraint uq_fact_situacion_financiera_estudiante_tiempo...") | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_situacion_financiera | |
| DROP CONSTRAINT IF EXISTS uq_fact_situacion_financiera_estudiante_tiempo; | |
| """)) | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_situacion_financiera | |
| ADD CONSTRAINT uq_fact_situacion_financiera_estudiante_tiempo UNIQUE (id_estudiante, id_tiempo); | |
| """)) | |
| # 6. De-duplicate fact_cobranzas_proyectadas | |
| print("De-duplicating public.fact_cobranzas_proyectadas...") | |
| conn.execute(text(""" | |
| DELETE FROM public.fact_cobranzas_proyectadas a USING public.fact_cobranzas_proyectadas b | |
| WHERE a.id_hecho_cobro < b.id_hecho_cobro | |
| AND a.id_estudiante = b.id_estudiante | |
| AND a.id_tiempo = b.id_tiempo; | |
| """)) | |
| print("Adding constraint uq_fact_cobranzas_proyectadas_estudiante_tiempo...") | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_cobranzas_proyectadas | |
| DROP CONSTRAINT IF EXISTS uq_fact_cobranzas_proyectadas_estudiante_tiempo; | |
| """)) | |
| conn.execute(text(""" | |
| ALTER TABLE public.fact_cobranzas_proyectadas | |
| ADD CONSTRAINT uq_fact_cobranzas_proyectadas_estudiante_tiempo UNIQUE (id_estudiante, id_tiempo); | |
| """)) | |
| trans.commit() | |
| print("Migrations successfully applied!") | |
| except Exception as e: | |
| trans.rollback() | |
| print(f"Error during migration: {e}") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| run_migrations() | |