"""replace the old single CSV table with individual CSV tables Revision ID: d4f8a6c2e1b0 Revises: c2e7f5b1a9d4 Create Date: 2026-09-18 """ import sqlalchemy as sa from collections.abc import Sequence from alembic import op revision: str = "d4f8a6c2e1b0" down_revision: str | None = "c2e7f5b1a9d4" branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None TABLE_COLUMNS = { "weekly_report": ["employee_name", "report_date", "headquarter", "month", "no_of_days_worked", "no_of_doctor_called", "no_of_chemist_called", "pob_booked", "secondary_sales", "pob_objective", "secondary_objective", "product_offer_scheme_booked", "product_offer_scheme_objective"], "field_activity_report": ["name", "headquarter", "month", "report_date", "station_worked", "no_of_doctors_called", "no_of_chemists_pds_called", "doctor_call_avg", "chemist_call_avg"], "fare_chart": ["name", "headquarter", "serial_no", "report_date", "from_location", "to_location", "kilometers_km"], "daily_call_report_csv": ["name", "headquarter", "report_date", "month", "doctor_sr_no", "doctor_name", "qualification", "product_promote", "product_sample", "remarks", "chemist_sr_no", "chemist_name", "pob", "stockist_visit", "order_value"], "core_dr_call_conversion": ["name", "headquarter", "month", "doctor_name", "date_of_visit", "product_targeted", "status"], "area_covered": ["name", "headquarter", "month", "sale_status", "area_to_cover", "sale_objective", "status"], "sales_and_stock_statement": ["name", "headquarter", "report_date", "month", "serial_no", "product_name", "opening", "receipt", "sale", "closing", "value_in_pts"], "tour_plan": ["name", "headquarter", "serial_no", "report_date", "from_location", "to_location", "pob", "stockiest_visit"], "standard_tour_plan": ["name", "headquarter", "serial_no", "place"], "sales_projection": ["serial_no", "product_name", "pts", "secondary_qty", "value", "closing", "secondary_value", "aug_sales", "value_2"], } def upgrade() -> None: op.drop_table("csv_import_row") for table_name, columns in TABLE_COLUMNS.items(): op.create_table( table_name, sa.Column("id", sa.UUID(), nullable=False), sa.Column("source_row_number", sa.Integer(), nullable=False), *(sa.Column(column, sa.Text(), nullable=True) for column in columns), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("source_row_number", name=f"uq_{table_name}_source_row"), ) def downgrade() -> None: for table_name in reversed(TABLE_COLUMNS): op.drop_table(table_name) op.create_table( "csv_import_row", sa.Column("id", sa.UUID(), nullable=False), sa.Column("source_file", sa.String(length=255), nullable=False), sa.Column("row_number", sa.Integer(), nullable=False), sa.Column("data", sa.JSON(), nullable=False), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("source_file", "row_number", name="uq_csv_import_file_row"), )