"""remove special fee modules Revision ID: 20260512_02 Revises: 20260512_01 Create Date: 2026-05-12 10:15:00.000000 """ from typing import Sequence, Union from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql revision: str = "20260512_02" down_revision: Union[str, None] = "20260512_01" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def _table_exists(inspector: sa.Inspector, table_name: str) -> bool: return table_name in inspector.get_table_names() def upgrade() -> None: bind = op.get_bind() inspector = sa.inspect(bind) if _table_exists(inspector, "attachments"): op.execute( """ DELETE FROM attachments WHERE entity_type IN ( 'finance_special', 'special_expense', 'special_expense_voucher', 'special_expense_invoice', 'special_expense_other' ) """ ) if _table_exists(inspector, "fee_attachments"): op.execute( """ DELETE FROM fee_attachments WHERE entity_type = 'special_expense' """ ) if _table_exists(inspector, "audit_logs"): op.execute( """ DELETE FROM audit_logs WHERE entity_type IN ('finance_special', 'special_expense') """ ) if _table_exists(inspector, "finance_specials"): op.drop_table("finance_specials") if _table_exists(inspector, "special_expenses"): op.drop_table("special_expenses") def downgrade() -> None: bind = op.get_bind() inspector = sa.inspect(bind) if not _table_exists(inspector, "special_expenses"): op.create_table( "special_expenses", sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("center_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("category", sa.String(length=50), nullable=False), sa.Column("amount", sa.Numeric(12, 2), nullable=False), sa.Column("happen_date", sa.Date(), nullable=True), sa.Column("description", sa.Text(), nullable=True), sa.Column("is_paid", sa.Boolean(), server_default="false", nullable=False), sa.Column("paid_date", sa.Date(), nullable=True), sa.Column("is_verified", sa.Boolean(), server_default="false", nullable=False), sa.Column("verified_date", sa.Date(), nullable=True), sa.Column("created_by", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.ForeignKeyConstraint(["center_id"], ["sites.id"]), sa.ForeignKeyConstraint(["created_by"], ["users.id"]), sa.ForeignKeyConstraint(["project_id"], ["studies.id"]), sa.PrimaryKeyConstraint("id"), ) op.create_index("ix_special_expenses_project_id", "special_expenses", ["project_id"]) op.create_index("ix_special_expenses_center_id", "special_expenses", ["center_id"]) if not _table_exists(inspector, "finance_specials"): op.create_table( "finance_specials", sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("study_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("site_name", sa.String(length=255), nullable=False), sa.Column("fee_type", sa.String(length=50), nullable=False), sa.Column("amount", sa.Numeric(12, 2), nullable=False), sa.Column("occur_date", sa.Date(), nullable=True), sa.Column("staff_name", sa.String(length=100), nullable=True), sa.Column("remark", sa.Text(), nullable=True), sa.Column("created_by", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.ForeignKeyConstraint(["created_by"], ["users.id"]), sa.ForeignKeyConstraint(["study_id"], ["studies.id"]), sa.PrimaryKeyConstraint("id"), ) op.create_index("ix_finance_specials_study_id", "finance_specials", ["study_id"])