190 lines
6.4 KiB
Python
190 lines
6.4 KiB
Python
"""company certification refactor + legacy ui fields
|
|
|
|
Revision ID: 6a7b8c9d0e1f
|
|
Revises: 1f4ba75eaa35
|
|
Create Date: 2026-04-23 11:10:00.000000
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "6a7b8c9d0e1f"
|
|
down_revision: Union[str, Sequence[str], None] = "1f4ba75eaa35"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
SCHEMA = "a76"
|
|
TABLE = "company_certification"
|
|
|
|
|
|
def upgrade() -> None:
|
|
# Rename Annex 31 columns to Annex 30 names.
|
|
op.alter_column(TABLE, "annex31_certification_date", new_column_name="annex30_certification_date", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex31_certification_number", new_column_name="annex30_certification_number", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex31_modality", new_column_name="annex30_modality", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex31_company_type", new_column_name="annex30_company_type", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex31_renewal_date", new_column_name="annex30_renewal_date", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex31_final_certification_date", new_column_name="annex30_final_certification_date", schema=SCHEMA)
|
|
|
|
op.alter_column(
|
|
TABLE,
|
|
"annex30_modality",
|
|
existing_type=sa.String(length=50),
|
|
type_=sa.String(length=3),
|
|
schema=SCHEMA,
|
|
)
|
|
|
|
# Convert legacy integer dates (YYYYMMDD or 0) to DATE.
|
|
date_columns = [
|
|
"certified_company_start_date",
|
|
"certified_company_end_date",
|
|
"annex30_certification_date",
|
|
"annex30_renewal_date",
|
|
"annex30_final_certification_date",
|
|
]
|
|
for col in date_columns:
|
|
op.alter_column(
|
|
TABLE,
|
|
col,
|
|
existing_type=sa.Integer(),
|
|
type_=sa.Date(),
|
|
schema=SCHEMA,
|
|
postgresql_using=(
|
|
f"CASE "
|
|
f"WHEN {col} IS NULL OR {col} = 0 THEN NULL "
|
|
f"WHEN length({col}::text) = 8 THEN to_date({col}::text, 'YYYYMMDD') "
|
|
f"ELSE NULL END"
|
|
),
|
|
)
|
|
|
|
# Convert legacy S/N and 0/1 flags to native booleans.
|
|
op.alter_column(
|
|
TABLE,
|
|
"is_certified_company",
|
|
existing_type=sa.String(length=1),
|
|
type_=sa.Boolean(),
|
|
schema=SCHEMA,
|
|
postgresql_using=(
|
|
"CASE "
|
|
"WHEN is_certified_company IS NULL THEN NULL "
|
|
"WHEN upper(trim(is_certified_company)) IN ('S','SI','1','T','TRUE','Y','YES') THEN true "
|
|
"WHEN upper(trim(is_certified_company)) IN ('N','NO','0','F','FALSE') THEN false "
|
|
"ELSE NULL END"
|
|
),
|
|
)
|
|
|
|
op.alter_column(
|
|
TABLE,
|
|
"is_oea_company",
|
|
existing_type=sa.SmallInteger(),
|
|
type_=sa.Boolean(),
|
|
schema=SCHEMA,
|
|
postgresql_using="CASE WHEN is_oea_company IS NULL THEN NULL WHEN is_oea_company = 1 THEN true ELSE false END",
|
|
)
|
|
|
|
op.alter_column(
|
|
TABLE,
|
|
"neec_company",
|
|
existing_type=sa.Integer(),
|
|
type_=sa.Boolean(),
|
|
schema=SCHEMA,
|
|
postgresql_using="CASE WHEN neec_company IS NULL THEN NULL WHEN neec_company = 1 THEN true ELSE false END",
|
|
)
|
|
|
|
# Additional legacy UI fields.
|
|
op.add_column(
|
|
"company",
|
|
sa.Column("fiscal_deposit", sa.Boolean(), nullable=True, server_default=sa.text("false")),
|
|
schema=SCHEMA,
|
|
)
|
|
op.add_column(
|
|
"company",
|
|
sa.Column(
|
|
"generate_barcodes_with_fiel",
|
|
sa.Boolean(),
|
|
nullable=True,
|
|
server_default=sa.text("false"),
|
|
),
|
|
schema=SCHEMA,
|
|
)
|
|
|
|
op.add_column(
|
|
"company_certification",
|
|
sa.Column("is_seciit_company", sa.Boolean(), nullable=True, server_default=sa.text("false")),
|
|
schema=SCHEMA,
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
# Remove additional legacy UI fields.
|
|
op.drop_column("company_certification", "is_seciit_company", schema=SCHEMA)
|
|
op.drop_column("company", "generate_barcodes_with_fiel", schema=SCHEMA)
|
|
op.drop_column("company", "fiscal_deposit", schema=SCHEMA)
|
|
|
|
# Restore booleans to legacy flag formats.
|
|
op.alter_column(
|
|
TABLE,
|
|
"is_certified_company",
|
|
existing_type=sa.Boolean(),
|
|
type_=sa.String(length=1),
|
|
schema=SCHEMA,
|
|
postgresql_using="CASE WHEN is_certified_company IS NULL THEN NULL WHEN is_certified_company THEN 'S' ELSE 'N' END",
|
|
)
|
|
|
|
op.alter_column(
|
|
TABLE,
|
|
"is_oea_company",
|
|
existing_type=sa.Boolean(),
|
|
type_=sa.SmallInteger(),
|
|
schema=SCHEMA,
|
|
postgresql_using="CASE WHEN is_oea_company IS NULL THEN NULL WHEN is_oea_company THEN 1 ELSE 0 END",
|
|
)
|
|
|
|
op.alter_column(
|
|
TABLE,
|
|
"neec_company",
|
|
existing_type=sa.Boolean(),
|
|
type_=sa.Integer(),
|
|
schema=SCHEMA,
|
|
postgresql_using="CASE WHEN neec_company IS NULL THEN NULL WHEN neec_company THEN 1 ELSE 0 END",
|
|
)
|
|
|
|
# Restore DATE columns to integer YYYYMMDD format.
|
|
date_columns = [
|
|
"certified_company_start_date",
|
|
"certified_company_end_date",
|
|
"annex30_certification_date",
|
|
"annex30_renewal_date",
|
|
"annex30_final_certification_date",
|
|
]
|
|
for col in date_columns:
|
|
op.alter_column(
|
|
TABLE,
|
|
col,
|
|
existing_type=sa.Date(),
|
|
type_=sa.Integer(),
|
|
schema=SCHEMA,
|
|
postgresql_using=f"CASE WHEN {col} IS NULL THEN NULL ELSE to_char({col}, 'YYYYMMDD')::integer END",
|
|
)
|
|
|
|
op.alter_column(
|
|
TABLE,
|
|
"annex30_modality",
|
|
existing_type=sa.String(length=3),
|
|
type_=sa.String(length=50),
|
|
schema=SCHEMA,
|
|
)
|
|
|
|
# Rename Annex 30 columns back to Annex 31.
|
|
op.alter_column(TABLE, "annex30_certification_date", new_column_name="annex31_certification_date", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex30_certification_number", new_column_name="annex31_certification_number", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex30_modality", new_column_name="annex31_modality", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex30_company_type", new_column_name="annex31_company_type", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex30_renewal_date", new_column_name="annex31_renewal_date", schema=SCHEMA)
|
|
op.alter_column(TABLE, "annex30_final_certification_date", new_column_name="annex31_final_certification_date", schema=SCHEMA)
|