启动与授权-初步优化
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
"""Add site_id to kickoff meetings.
|
||||
|
||||
Revision ID: 20250310_01
|
||||
Revises: 20250218_01
|
||||
Create Date: 2025-03-10
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
revision = "20250310_01"
|
||||
down_revision = "20250218_01"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = inspect(bind)
|
||||
|
||||
columns = {col["name"] for col in inspector.get_columns("kickoff_meetings")}
|
||||
if "site_id" not in columns:
|
||||
op.add_column("kickoff_meetings", sa.Column("site_id", postgresql.UUID(as_uuid=True), nullable=True))
|
||||
|
||||
fks = {fk["name"] for fk in inspector.get_foreign_keys("kickoff_meetings")}
|
||||
if "fk_kickoff_meetings_site_id" not in fks:
|
||||
op.create_foreign_key(
|
||||
"fk_kickoff_meetings_site_id",
|
||||
"kickoff_meetings",
|
||||
"sites",
|
||||
["site_id"],
|
||||
["id"],
|
||||
ondelete="RESTRICT",
|
||||
)
|
||||
|
||||
indexes = {index["name"] for index in inspector.get_indexes("kickoff_meetings")}
|
||||
if "ix_kickoff_meetings_site_id" not in indexes:
|
||||
op.create_index("ix_kickoff_meetings_site_id", "kickoff_meetings", ["site_id"])
|
||||
|
||||
uniques = {uc["name"] for uc in inspector.get_unique_constraints("kickoff_meetings")}
|
||||
if "uq_kickoff_meetings_study_site" not in uniques:
|
||||
op.create_unique_constraint(
|
||||
"uq_kickoff_meetings_study_site",
|
||||
"kickoff_meetings",
|
||||
["study_id", "site_id"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = inspect(bind)
|
||||
|
||||
uniques = {uc["name"] for uc in inspector.get_unique_constraints("kickoff_meetings")}
|
||||
if "uq_kickoff_meetings_study_site" in uniques:
|
||||
op.drop_constraint("uq_kickoff_meetings_study_site", "kickoff_meetings", type_="unique")
|
||||
|
||||
indexes = {index["name"] for index in inspector.get_indexes("kickoff_meetings")}
|
||||
if "ix_kickoff_meetings_site_id" in indexes:
|
||||
op.drop_index("ix_kickoff_meetings_site_id", table_name="kickoff_meetings")
|
||||
|
||||
fks = {fk["name"] for fk in inspector.get_foreign_keys("kickoff_meetings")}
|
||||
if "fk_kickoff_meetings_site_id" in fks:
|
||||
op.drop_constraint("fk_kickoff_meetings_site_id", "kickoff_meetings", type_="foreignkey")
|
||||
|
||||
columns = {col["name"] for col in inspector.get_columns("kickoff_meetings")}
|
||||
if "site_id" in columns:
|
||||
op.drop_column("kickoff_meetings", "site_id")
|
||||
@@ -0,0 +1,138 @@
|
||||
"""Migrate fee_attachments into attachments.
|
||||
|
||||
Revision ID: 20250310_02
|
||||
Revises: 20250310_01
|
||||
Create Date: 2025-03-10
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = "20250310_02"
|
||||
down_revision = "20250310_01"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute(
|
||||
"""
|
||||
INSERT INTO attachments (
|
||||
id,
|
||||
study_id,
|
||||
entity_type,
|
||||
entity_id,
|
||||
filename,
|
||||
file_path,
|
||||
file_size,
|
||||
content_type,
|
||||
uploaded_by,
|
||||
uploaded_at,
|
||||
is_deleted
|
||||
)
|
||||
SELECT
|
||||
fa.id,
|
||||
cf.project_id,
|
||||
'contract_fee_' || fa.file_type,
|
||||
fa.entity_id,
|
||||
fa.filename,
|
||||
fa.storage_key,
|
||||
fa.size,
|
||||
fa.mime_type,
|
||||
fa.uploaded_by,
|
||||
fa.uploaded_at,
|
||||
fa.is_deleted
|
||||
FROM fee_attachments fa
|
||||
JOIN contract_fees cf ON fa.entity_type = 'contract_fee' AND fa.entity_id = cf.id
|
||||
WHERE fa.storage_key IS NOT NULL
|
||||
AND NOT EXISTS (SELECT 1 FROM attachments a WHERE a.id = fa.id)
|
||||
"""
|
||||
)
|
||||
|
||||
op.execute(
|
||||
"""
|
||||
INSERT INTO attachments (
|
||||
id,
|
||||
study_id,
|
||||
entity_type,
|
||||
entity_id,
|
||||
filename,
|
||||
file_path,
|
||||
file_size,
|
||||
content_type,
|
||||
uploaded_by,
|
||||
uploaded_at,
|
||||
is_deleted
|
||||
)
|
||||
SELECT
|
||||
fa.id,
|
||||
cf.project_id,
|
||||
'contract_payment_' || fa.file_type,
|
||||
fa.entity_id,
|
||||
fa.filename,
|
||||
fa.storage_key,
|
||||
fa.size,
|
||||
fa.mime_type,
|
||||
fa.uploaded_by,
|
||||
fa.uploaded_at,
|
||||
fa.is_deleted
|
||||
FROM fee_attachments fa
|
||||
JOIN contract_fee_payments cp ON fa.entity_type = 'contract_payment' AND fa.entity_id = cp.id
|
||||
JOIN contract_fees cf ON cp.contract_fee_id = cf.id
|
||||
WHERE fa.storage_key IS NOT NULL
|
||||
AND NOT EXISTS (SELECT 1 FROM attachments a WHERE a.id = fa.id)
|
||||
"""
|
||||
)
|
||||
|
||||
op.execute(
|
||||
"""
|
||||
INSERT INTO attachments (
|
||||
id,
|
||||
study_id,
|
||||
entity_type,
|
||||
entity_id,
|
||||
filename,
|
||||
file_path,
|
||||
file_size,
|
||||
content_type,
|
||||
uploaded_by,
|
||||
uploaded_at,
|
||||
is_deleted
|
||||
)
|
||||
SELECT
|
||||
fa.id,
|
||||
se.project_id,
|
||||
'special_expense_' || fa.file_type,
|
||||
fa.entity_id,
|
||||
fa.filename,
|
||||
fa.storage_key,
|
||||
fa.size,
|
||||
fa.mime_type,
|
||||
fa.uploaded_by,
|
||||
fa.uploaded_at,
|
||||
fa.is_deleted
|
||||
FROM fee_attachments fa
|
||||
JOIN special_expenses se ON fa.entity_type = 'special_expense' AND fa.entity_id = se.id
|
||||
WHERE fa.storage_key IS NOT NULL
|
||||
AND NOT EXISTS (SELECT 1 FROM attachments a WHERE a.id = fa.id)
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute(
|
||||
"""
|
||||
DELETE FROM attachments a
|
||||
USING fee_attachments fa
|
||||
WHERE a.id = fa.id
|
||||
AND a.entity_type IN (
|
||||
'contract_fee_contract',
|
||||
'contract_fee_voucher',
|
||||
'contract_fee_invoice',
|
||||
'contract_payment_voucher',
|
||||
'contract_payment_invoice',
|
||||
'special_expense_voucher',
|
||||
'special_expense_invoice',
|
||||
'special_expense_other'
|
||||
)
|
||||
"""
|
||||
)
|
||||
Reference in New Issue
Block a user