药品流向管理内容优化-初步
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.base import Base
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def _get_database_url() -> str:
|
||||
url = os.getenv("DATABASE_URL", settings.DATABASE_URL)
|
||||
if url and url.startswith("postgresql+asyncpg://"):
|
||||
return url
|
||||
return url
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
url = _get_database_url()
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection) -> None:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_migrations_online() -> None:
|
||||
configuration = config.get_section(config.config_ini_section, {})
|
||||
configuration["sqlalchemy.url"] = _get_database_url()
|
||||
|
||||
connectable = async_engine_from_config(
|
||||
configuration,
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
asyncio.run(run_migrations_online())
|
||||
@@ -1,50 +0,0 @@
|
||||
"""create users table with review status"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "20240501_000001"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
user_role_enum = sa.Enum("ADMIN", "PM", "CRA", "PV", "IMP", name="user_role")
|
||||
user_status_enum = sa.Enum("PENDING", "ACTIVE", "REJECTED", "DISABLED", name="user_status")
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
user_role_enum.create(bind, checkfirst=True)
|
||||
user_status_enum.create(bind, checkfirst=True)
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
|
||||
sa.Column("email", sa.String(length=255), nullable=False),
|
||||
sa.Column("password_hash", sa.String(length=255), nullable=False),
|
||||
sa.Column("full_name", sa.String(length=255), nullable=False),
|
||||
sa.Column("role", user_role_enum, nullable=False),
|
||||
sa.Column("department", sa.String(length=255), nullable=False),
|
||||
sa.Column("status", user_status_enum, nullable=False, server_default="PENDING"),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
|
||||
sa.Column(
|
||||
"updated_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sa.text("now()"),
|
||||
),
|
||||
sa.Column("approved_by", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id"), nullable=True),
|
||||
sa.Column("approved_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
op.create_index("ix_users_email", "users", ["email"], unique=True)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_users_email", table_name="users")
|
||||
op.drop_table("users")
|
||||
bind = op.get_bind()
|
||||
user_role_enum.drop(bind, checkfirst=True)
|
||||
user_status_enum.drop(bind, checkfirst=True)
|
||||
@@ -1,19 +0,0 @@
|
||||
"""add avatar_url to users"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "20240501_000002"
|
||||
down_revision = "20240501_000001"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("users", sa.Column("avatar_url", sa.String(length=500), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("users", "avatar_url")
|
||||
@@ -1,22 +0,0 @@
|
||||
"""drop visit_name from visits"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "20240501_000003"
|
||||
down_revision = "20240501_000002"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.drop_column("visits", "visit_name")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.add_column(
|
||||
"visits",
|
||||
sa.Column("visit_name", sa.String(length=200), nullable=False, server_default=""),
|
||||
)
|
||||
@@ -1,25 +0,0 @@
|
||||
"""add visit template fields to studies"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "20240501_000004"
|
||||
down_revision = "20240501_000003"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("studies", sa.Column("visit_interval_days", sa.Integer(), nullable=True))
|
||||
op.add_column("studies", sa.Column("visit_total", sa.Integer(), nullable=True))
|
||||
op.add_column("studies", sa.Column("visit_window_start_offset", sa.Integer(), nullable=True))
|
||||
op.add_column("studies", sa.Column("visit_window_end_offset", sa.Integer(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("studies", "visit_window_end_offset")
|
||||
op.drop_column("studies", "visit_window_start_offset")
|
||||
op.drop_column("studies", "visit_total")
|
||||
op.drop_column("studies", "visit_interval_days")
|
||||
@@ -1,19 +0,0 @@
|
||||
"""add consent_date to subjects"""
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "20240501_000005"
|
||||
down_revision = "20240501_000004"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("subjects", sa.Column("consent_date", sa.Date(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("subjects", "consent_date")
|
||||
@@ -1,104 +0,0 @@
|
||||
"""create fee tables
|
||||
|
||||
Revision ID: 20240501_000006
|
||||
Revises: 20240501_000005
|
||||
Create Date: 2025-02-14 00:00:00
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "20240501_000006"
|
||||
down_revision = "20240501_000005"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"contract_fees",
|
||||
sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("project_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column("center_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column("contract_amount", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("contract_cases", sa.Integer(), nullable=False),
|
||||
sa.Column("actual_cases", sa.Integer(), nullable=True),
|
||||
sa.Column("settlement_amount", sa.Numeric(12, 2), nullable=True),
|
||||
sa.Column("final_payment_amount", sa.Numeric(12, 2), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
|
||||
sa.ForeignKeyConstraint(["project_id"], ["studies.id"], ondelete="RESTRICT"),
|
||||
sa.ForeignKeyConstraint(["center_id"], ["sites.id"], ondelete="RESTRICT"),
|
||||
sa.UniqueConstraint("project_id", "center_id", name="uq_contract_fees_project_center"),
|
||||
)
|
||||
op.create_index("ix_contract_fees_project_id", "contract_fees", ["project_id"])
|
||||
op.create_index("ix_contract_fees_center_id", "contract_fees", ["center_id"])
|
||||
|
||||
op.create_table(
|
||||
"contract_fee_payments",
|
||||
sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("contract_fee_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column("seq", sa.Integer(), nullable=False),
|
||||
sa.Column("amount", sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column("paid_date", sa.Date(), nullable=True),
|
||||
sa.Column("verified_date", sa.Date(), nullable=True),
|
||||
sa.Column("is_paid", sa.Boolean(), server_default=sa.text("false"), nullable=False),
|
||||
sa.Column("is_verified", sa.Boolean(), server_default=sa.text("false"), nullable=False),
|
||||
sa.Column("remark", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
|
||||
sa.ForeignKeyConstraint(["contract_fee_id"], ["contract_fees.id"], ondelete="CASCADE"),
|
||||
)
|
||||
op.create_index("ix_contract_fee_payments_contract_fee_id", "contract_fee_payments", ["contract_fee_id"])
|
||||
|
||||
op.create_table(
|
||||
"special_expenses",
|
||||
sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("project_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column("center_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=True),
|
||||
sa.Column("category", sa.String(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=sa.text("false"), nullable=False),
|
||||
sa.Column("paid_date", sa.Date(), nullable=True),
|
||||
sa.Column("is_verified", sa.Boolean(), server_default=sa.text("false"), nullable=False),
|
||||
sa.Column("verified_date", sa.Date(), nullable=True),
|
||||
sa.Column("created_by", sa.dialects.postgresql.UUID(as_uuid=True), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
|
||||
sa.ForeignKeyConstraint(["project_id"], ["studies.id"], ondelete="RESTRICT"),
|
||||
sa.ForeignKeyConstraint(["center_id"], ["sites.id"], ondelete="RESTRICT"),
|
||||
sa.ForeignKeyConstraint(["created_by"], ["users.id"], ondelete="RESTRICT"),
|
||||
)
|
||||
op.create_index("ix_special_expenses_project_id", "special_expenses", ["project_id"])
|
||||
op.create_index("ix_special_expenses_center_id", "special_expenses", ["center_id"])
|
||||
|
||||
op.create_table(
|
||||
"fee_attachments",
|
||||
sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), primary_key=True),
|
||||
sa.Column("entity_type", sa.String(50), nullable=False),
|
||||
sa.Column("entity_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column("file_type", sa.String(30), nullable=False),
|
||||
sa.Column("filename", sa.String(255), nullable=False),
|
||||
sa.Column("mime_type", sa.String(100), nullable=True),
|
||||
sa.Column("size", sa.Integer(), nullable=False),
|
||||
sa.Column("storage_key", sa.String(500), nullable=True),
|
||||
sa.Column("url", sa.Text(), nullable=True),
|
||||
sa.Column("uploaded_by", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column("uploaded_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
|
||||
sa.Column("is_deleted", sa.Boolean(), server_default=sa.text("false"), nullable=False),
|
||||
sa.ForeignKeyConstraint(["uploaded_by"], ["users.id"], ondelete="RESTRICT"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("fee_attachments")
|
||||
op.drop_index("ix_special_expenses_center_id", table_name="special_expenses")
|
||||
op.drop_index("ix_special_expenses_project_id", table_name="special_expenses")
|
||||
op.drop_table("special_expenses")
|
||||
op.drop_index("ix_contract_fee_payments_contract_fee_id", table_name="contract_fee_payments")
|
||||
op.drop_table("contract_fee_payments")
|
||||
op.drop_index("ix_contract_fees_center_id", table_name="contract_fees")
|
||||
op.drop_index("ix_contract_fees_project_id", table_name="contract_fees")
|
||||
op.drop_table("contract_fees")
|
||||
Reference in New Issue
Block a user