125 lines
5.3 KiB
Python
125 lines
5.3 KiB
Python
"""remove legacy finance contracts
|
|
|
|
Revision ID: 20260527_03
|
|
Revises: 20260527_02
|
|
Create Date: 2026-05-27 10:25:00.000000
|
|
|
|
"""
|
|
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
|
|
revision: str = "20260527_03"
|
|
down_revision: Union[str, None] = "20260527_02"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
LEGACY_KEYS = (
|
|
"finance_contracts:create",
|
|
"finance_contracts:list",
|
|
"finance_contracts:read",
|
|
"finance_contracts:update",
|
|
"finance_contracts:delete",
|
|
"fees_payments:create",
|
|
"fees_payments:update",
|
|
"fees_payments:delete",
|
|
"fees_attachments:create",
|
|
"fees_attachments:read",
|
|
"fees_attachments:delete",
|
|
)
|
|
|
|
|
|
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, "finance_contracts") and _table_exists(inspector, "contract_fees"):
|
|
op.execute(
|
|
"""
|
|
UPDATE contract_fees cf
|
|
SET
|
|
contract_no = COALESCE(cf.contract_no, legacy.contract_no),
|
|
signed_date = COALESCE(cf.signed_date, legacy.signed_date),
|
|
currency = COALESCE(NULLIF(cf.currency, ''), legacy.currency, 'CNY'),
|
|
remark = COALESCE(cf.remark, legacy.remark)
|
|
FROM (
|
|
SELECT DISTINCT ON (fc.study_id, s.id)
|
|
fc.study_id,
|
|
s.id AS center_id,
|
|
fc.contract_no,
|
|
fc.signed_date,
|
|
fc.currency,
|
|
fc.remark
|
|
FROM finance_contracts fc
|
|
JOIN sites s
|
|
ON s.study_id = fc.study_id
|
|
AND s.name = fc.site_name
|
|
ORDER BY fc.study_id, s.id, fc.updated_at DESC, fc.created_at DESC
|
|
) legacy
|
|
WHERE cf.project_id = legacy.study_id
|
|
AND cf.center_id = legacy.center_id
|
|
"""
|
|
)
|
|
|
|
if _table_exists(inspector, "api_endpoint_permissions"):
|
|
quoted_keys = ", ".join(f"'{key}'" for key in LEGACY_KEYS)
|
|
op.execute(f"DELETE FROM api_endpoint_permissions WHERE endpoint_key IN ({quoted_keys})")
|
|
|
|
if _table_exists(inspector, "permission_templates"):
|
|
for key in LEGACY_KEYS:
|
|
op.execute(f"UPDATE permission_templates SET permissions = permissions::jsonb #- '{{PM,{key}}}'")
|
|
op.execute(f"UPDATE permission_templates SET permissions = permissions::jsonb #- '{{CRA,{key}}}'")
|
|
op.execute(f"UPDATE permission_templates SET permissions = permissions::jsonb #- '{{PV,{key}}}'")
|
|
op.execute(f"UPDATE permission_templates SET permissions = permissions::jsonb #- '{{QA,{key}}}'")
|
|
op.execute(f"UPDATE permission_templates SET permissions = permissions::jsonb #- '{{CTA,{key}}}'")
|
|
|
|
if _table_exists(inspector, "permission_template_versions"):
|
|
for key in LEGACY_KEYS:
|
|
op.execute(f"UPDATE permission_template_versions SET permissions = permissions::jsonb #- '{{PM,{key}}}'")
|
|
op.execute(f"UPDATE permission_template_versions SET permissions = permissions::jsonb #- '{{CRA,{key}}}'")
|
|
op.execute(f"UPDATE permission_template_versions SET permissions = permissions::jsonb #- '{{PV,{key}}}'")
|
|
op.execute(f"UPDATE permission_template_versions SET permissions = permissions::jsonb #- '{{QA,{key}}}'")
|
|
op.execute(f"UPDATE permission_template_versions SET permissions = permissions::jsonb #- '{{CTA,{key}}}'")
|
|
|
|
if _table_exists(inspector, "attachments"):
|
|
op.execute("DELETE FROM attachments WHERE entity_type = 'finance_contract'")
|
|
|
|
if _table_exists(inspector, "audit_logs"):
|
|
op.execute("DELETE FROM audit_logs WHERE entity_type = 'finance_contract'")
|
|
|
|
if _table_exists(inspector, "finance_contracts"):
|
|
op.drop_table("finance_contracts")
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
|
|
if not _table_exists(inspector, "finance_contracts"):
|
|
op.create_table(
|
|
"finance_contracts",
|
|
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("contract_no", sa.String(length=100), nullable=False),
|
|
sa.Column("signed_date", sa.Date(), nullable=True),
|
|
sa.Column("amount", sa.Numeric(12, 2), nullable=False),
|
|
sa.Column("currency", sa.String(length=10), nullable=False),
|
|
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_contracts_study_id", "finance_contracts", ["study_id"])
|