Production
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
from app.config.settings import settings
|
||||
from app.database.all_models import Base # noqa: F401 - registers every model
|
||||
|
||||
config = context.config
|
||||
config.set_main_option("sqlalchemy.url", settings.sync_database_url)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=settings.sync_database_url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
compare_type=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata, compare_type=True
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,24 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
from collections.abc import Sequence
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: str | None = ${repr(down_revision)}
|
||||
branch_labels: str | Sequence[str] | None = ${repr(branch_labels)}
|
||||
depends_on: str | Sequence[str] | None = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,241 @@
|
||||
"""initial MVP schema
|
||||
|
||||
Revision ID: a40386dcf76c
|
||||
Revises:
|
||||
Create Date: 2026-09-10 13:54:30.431676
|
||||
"""
|
||||
from collections.abc import Sequence
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
revision: str = 'a40386dcf76c'
|
||||
down_revision: str | None = None
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('company',
|
||||
sa.Column('name', sa.String(length=200), nullable=False),
|
||||
sa.Column('address', sa.String(length=500), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_table('headquarters',
|
||||
sa.Column('company_id', sa.UUID(), nullable=False),
|
||||
sa.Column('code', sa.String(length=20), nullable=False),
|
||||
sa.Column('name', sa.String(length=200), nullable=False),
|
||||
sa.Column('district', sa.String(length=100), nullable=True),
|
||||
sa.Column('state', sa.String(length=100), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['company_id'], ['company.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('company_id', 'code', name='uq_hq_company_code')
|
||||
)
|
||||
op.create_table('chemist',
|
||||
sa.Column('headquarters_id', sa.UUID(), nullable=False),
|
||||
sa.Column('name', sa.String(length=200), nullable=False),
|
||||
sa.Column('shop_address', sa.String(length=500), nullable=False),
|
||||
sa.Column('contact_number', sa.String(length=20), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['headquarters_id'], ['headquarters.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('headquarters_id', 'name', 'shop_address', name='uq_chemist_hq_name_shop')
|
||||
)
|
||||
op.create_table('doctor',
|
||||
sa.Column('headquarters_id', sa.UUID(), nullable=False),
|
||||
sa.Column('name', sa.String(length=200), nullable=False),
|
||||
sa.Column('speciality', sa.String(length=100), nullable=True),
|
||||
sa.Column('qualification', sa.String(length=100), nullable=True),
|
||||
sa.Column('clinic_address', sa.String(length=500), nullable=False),
|
||||
sa.Column('contact_number', sa.String(length=20), nullable=True),
|
||||
sa.Column('is_core_doctor', sa.Boolean(), nullable=False),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['headquarters_id'], ['headquarters.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('headquarters_id', 'name', 'clinic_address', name='uq_doctor_hq_name_clinic')
|
||||
)
|
||||
op.create_table('user',
|
||||
sa.Column('company_id', sa.UUID(), 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=200), nullable=False),
|
||||
sa.Column('employee_code', sa.String(length=50), nullable=False),
|
||||
sa.Column('phone', sa.String(length=20), nullable=True),
|
||||
sa.Column('role', sa.Enum('ADMIN', 'GM', 'AM', 'MR', name='role_enum', native_enum=False, length=10), nullable=False),
|
||||
sa.Column('headquarters_id', sa.UUID(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.Column('failed_login_attempts', sa.Integer(), nullable=False),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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.CheckConstraint("(role = 'MR' AND headquarters_id IS NOT NULL) OR (role <> 'MR' AND headquarters_id IS NULL)", name='ck_user_mr_requires_hq'),
|
||||
sa.ForeignKeyConstraint(['company_id'], ['company.id'], ondelete='RESTRICT'),
|
||||
sa.ForeignKeyConstraint(['headquarters_id'], ['headquarters.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('company_id', 'email', name='uq_user_company_email'),
|
||||
sa.UniqueConstraint('company_id', 'employee_code', name='uq_user_company_empcode')
|
||||
)
|
||||
op.create_table('am_headquarters',
|
||||
sa.Column('area_manager_user_id', sa.UUID(), nullable=False),
|
||||
sa.Column('headquarters_id', sa.UUID(), nullable=False),
|
||||
sa.Column('effective_from', sa.Date(), nullable=False),
|
||||
sa.Column('effective_to', sa.Date(), nullable=True),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['area_manager_user_id'], ['user.id'], ondelete='RESTRICT'),
|
||||
sa.ForeignKeyConstraint(['headquarters_id'], ['headquarters.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('uq_active_am_per_hq', 'am_headquarters', ['headquarters_id'], unique=True, postgresql_where=sa.text('effective_to IS NULL'))
|
||||
op.create_table('audit_log',
|
||||
sa.Column('actor_user_id', sa.UUID(), nullable=False),
|
||||
sa.Column('entity_type', sa.Enum('USER', 'HEADQUARTERS', 'DOCTOR', 'CHEMIST', 'DCR', name='entity_type_enum', native_enum=False, length=30), nullable=False),
|
||||
sa.Column('entity_id', sa.UUID(), nullable=False),
|
||||
sa.Column('action', sa.Enum('CREATE', 'UPDATE', 'DELETE', 'SUBMIT', 'FORWARD', 'RETURN', 'APPROVE', 'ASSIGN', 'LOGIN', 'DEACTIVATE', name='audit_action_enum', native_enum=False, length=20), nullable=False),
|
||||
sa.Column('before_data', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('after_data', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('comment', sa.Text(), nullable=True),
|
||||
sa.Column('ip_address', sa.String(length=45), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['actor_user_id'], ['user.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_audit_log_created_at'), 'audit_log', ['created_at'], unique=False)
|
||||
op.create_index(op.f('ix_audit_log_entity_id'), 'audit_log', ['entity_id'], unique=False)
|
||||
op.create_index(op.f('ix_audit_log_entity_type'), 'audit_log', ['entity_type'], unique=False)
|
||||
op.create_table('chemist_assignment',
|
||||
sa.Column('chemist_id', sa.UUID(), nullable=False),
|
||||
sa.Column('mr_user_id', sa.UUID(), nullable=False),
|
||||
sa.Column('effective_from', sa.Date(), nullable=False),
|
||||
sa.Column('effective_to', sa.Date(), nullable=True),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['chemist_id'], ['chemist.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['mr_user_id'], ['user.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('uq_active_chemist_assignment', 'chemist_assignment', ['chemist_id'], unique=True, postgresql_where=sa.text('effective_to IS NULL'))
|
||||
op.create_table('daily_call_report',
|
||||
sa.Column('mr_user_id', sa.UUID(), nullable=False),
|
||||
sa.Column('headquarters_id', sa.UUID(), nullable=False),
|
||||
sa.Column('report_date', sa.Date(), nullable=False),
|
||||
sa.Column('status', sa.Enum('DRAFT', 'PENDING_AM', 'PENDING_GM', 'RETURNED', 'APPROVED', name='dcr_status_enum', native_enum=False, length=20), nullable=False),
|
||||
sa.Column('current_am_user_id', sa.UUID(), nullable=True),
|
||||
sa.Column('current_gm_user_id', sa.UUID(), nullable=True),
|
||||
sa.Column('mr_remarks', sa.Text(), nullable=True),
|
||||
sa.Column('return_reason', sa.Text(), nullable=True),
|
||||
sa.Column('returned_by_role', sa.Enum('ADMIN', 'GM', 'AM', 'MR', name='role_enum', native_enum=False, length=10), nullable=True),
|
||||
sa.Column('submitted_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('pdf_path', sa.String(length=500), nullable=True),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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.CheckConstraint("(status = 'APPROVED' AND approved_at IS NOT NULL) OR (status <> 'APPROVED' AND approved_at IS NULL)", name='ck_dcr_approved_at'),
|
||||
sa.ForeignKeyConstraint(['current_am_user_id'], ['user.id'], ondelete='RESTRICT'),
|
||||
sa.ForeignKeyConstraint(['current_gm_user_id'], ['user.id'], ondelete='RESTRICT'),
|
||||
sa.ForeignKeyConstraint(['headquarters_id'], ['headquarters.id'], ondelete='RESTRICT'),
|
||||
sa.ForeignKeyConstraint(['mr_user_id'], ['user.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('mr_user_id', 'report_date', name='uq_dcr_mr_date')
|
||||
)
|
||||
op.create_table('doctor_assignment',
|
||||
sa.Column('doctor_id', sa.UUID(), nullable=False),
|
||||
sa.Column('mr_user_id', sa.UUID(), nullable=False),
|
||||
sa.Column('effective_from', sa.Date(), nullable=False),
|
||||
sa.Column('effective_to', sa.Date(), nullable=True),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['doctor_id'], ['doctor.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['mr_user_id'], ['user.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index('uq_active_doctor_assignment', 'doctor_assignment', ['doctor_id'], unique=True, postgresql_where=sa.text('effective_to IS NULL'))
|
||||
op.create_table('refresh_token',
|
||||
sa.Column('user_id', sa.UUID(), nullable=False),
|
||||
sa.Column('token_hash', sa.String(length=64), nullable=False),
|
||||
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('revoked', sa.Boolean(), nullable=False),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['user_id'], ['user.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_refresh_token_token_hash'), 'refresh_token', ['token_hash'], unique=True)
|
||||
op.create_index(op.f('ix_refresh_token_user_id'), 'refresh_token', ['user_id'], unique=False)
|
||||
op.create_table('chemist_call',
|
||||
sa.Column('dcr_id', sa.UUID(), nullable=False),
|
||||
sa.Column('chemist_id', sa.UUID(), nullable=False),
|
||||
sa.Column('visit_time', sa.Time(), nullable=False),
|
||||
sa.Column('remarks', sa.Text(), nullable=True),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['chemist_id'], ['chemist.id'], ondelete='RESTRICT'),
|
||||
sa.ForeignKeyConstraint(['dcr_id'], ['daily_call_report.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('dcr_id', 'chemist_id', name='uq_call_dcr_chemist')
|
||||
)
|
||||
op.create_table('doctor_call',
|
||||
sa.Column('dcr_id', sa.UUID(), nullable=False),
|
||||
sa.Column('doctor_id', sa.UUID(), nullable=False),
|
||||
sa.Column('visit_time', sa.Time(), nullable=False),
|
||||
sa.Column('call_outcome', sa.Enum('MET', 'NOT_AVAILABLE', 'RESCHEDULED', name='call_outcome_enum', native_enum=False, length=20), nullable=False),
|
||||
sa.Column('conversion_status', sa.Enum('NOT_STARTED', 'IN_PROGRESS', 'CONVERTED', 'LOST', name='conversion_status_enum', native_enum=False, length=20), nullable=True),
|
||||
sa.Column('remarks', sa.Text(), nullable=True),
|
||||
sa.Column('id', sa.UUID(), nullable=False),
|
||||
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(['dcr_id'], ['daily_call_report.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['doctor_id'], ['doctor.id'], ondelete='RESTRICT'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('dcr_id', 'doctor_id', name='uq_call_dcr_doctor')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('doctor_call')
|
||||
op.drop_table('chemist_call')
|
||||
op.drop_index(op.f('ix_refresh_token_user_id'), table_name='refresh_token')
|
||||
op.drop_index(op.f('ix_refresh_token_token_hash'), table_name='refresh_token')
|
||||
op.drop_table('refresh_token')
|
||||
op.drop_index('uq_active_doctor_assignment', table_name='doctor_assignment', postgresql_where=sa.text('effective_to IS NULL'))
|
||||
op.drop_table('doctor_assignment')
|
||||
op.drop_table('daily_call_report')
|
||||
op.drop_index('uq_active_chemist_assignment', table_name='chemist_assignment', postgresql_where=sa.text('effective_to IS NULL'))
|
||||
op.drop_table('chemist_assignment')
|
||||
op.drop_index(op.f('ix_audit_log_entity_type'), table_name='audit_log')
|
||||
op.drop_index(op.f('ix_audit_log_entity_id'), table_name='audit_log')
|
||||
op.drop_index(op.f('ix_audit_log_created_at'), table_name='audit_log')
|
||||
op.drop_table('audit_log')
|
||||
op.drop_index('uq_active_am_per_hq', table_name='am_headquarters', postgresql_where=sa.text('effective_to IS NULL'))
|
||||
op.drop_table('am_headquarters')
|
||||
op.drop_table('user')
|
||||
op.drop_table('doctor')
|
||||
op.drop_table('chemist')
|
||||
op.drop_table('headquarters')
|
||||
op.drop_table('company')
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,54 @@
|
||||
"""make AM the final approver and add the product master
|
||||
|
||||
Revision ID: b71d4c22a917
|
||||
Revises: a40386dcf76c
|
||||
Create Date: 2026-09-17
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "b71d4c22a917"
|
||||
down_revision: str | None = "a40386dcf76c"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# Native enums are deliberately disabled in the initial schema. Existing reports
|
||||
# that were waiting for GM are returned to the AM queue for a final decision.
|
||||
op.execute("UPDATE daily_call_report SET status = 'PENDING_AM' WHERE status = 'PENDING_GM'")
|
||||
op.create_table(
|
||||
"product",
|
||||
sa.Column("company_id", sa.UUID(), nullable=False),
|
||||
sa.Column("name", sa.String(length=200), nullable=False),
|
||||
sa.Column("pack", sa.String(length=100), nullable=True),
|
||||
sa.Column("unit", sa.String(length=50), nullable=True),
|
||||
sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
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(["company_id"], ["company.id"], ondelete="RESTRICT"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("company_id", "name", name="uq_product_company_name"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("product")
|
||||
# A downgrade cannot safely reinstate the former GM queue assignment. Reopen the
|
||||
# reports in the old AM queue rather than fabricating a GM approval decision.
|
||||
op.execute("UPDATE daily_call_report SET status = 'PENDING_AM' WHERE status = 'PENDING_AM'")
|
||||
@@ -0,0 +1,33 @@
|
||||
"""store non-MVP CSV source rows
|
||||
|
||||
Revision ID: c2e7f5b1a9d4
|
||||
Revises: b71d4c22a917
|
||||
Create Date: 2026-09-18
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "c2e7f5b1a9d4"
|
||||
down_revision: str | None = "b71d4c22a917"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"csv_import_row",
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("source_file", sa.String(length=255), nullable=False),
|
||||
sa.Column("row_number", sa.Integer(), nullable=False),
|
||||
sa.Column("data", sa.JSON(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("source_file", "row_number", name="uq_csv_import_file_row"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("csv_import_row")
|
||||
@@ -0,0 +1,57 @@
|
||||
"""replace the old single CSV table with individual CSV tables
|
||||
|
||||
Revision ID: d4f8a6c2e1b0
|
||||
Revises: c2e7f5b1a9d4
|
||||
Create Date: 2026-09-18
|
||||
"""
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "d4f8a6c2e1b0"
|
||||
down_revision: str | None = "c2e7f5b1a9d4"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
TABLE_COLUMNS = {
|
||||
"weekly_report": ["employee_name", "report_date", "headquarter", "month", "no_of_days_worked", "no_of_doctor_called", "no_of_chemist_called", "pob_booked", "secondary_sales", "pob_objective", "secondary_objective", "product_offer_scheme_booked", "product_offer_scheme_objective"],
|
||||
"field_activity_report": ["name", "headquarter", "month", "report_date", "station_worked", "no_of_doctors_called", "no_of_chemists_pds_called", "doctor_call_avg", "chemist_call_avg"],
|
||||
"fare_chart": ["name", "headquarter", "serial_no", "report_date", "from_location", "to_location", "kilometers_km"],
|
||||
"daily_call_report_csv": ["name", "headquarter", "report_date", "month", "doctor_sr_no", "doctor_name", "qualification", "product_promote", "product_sample", "remarks", "chemist_sr_no", "chemist_name", "pob", "stockist_visit", "order_value"],
|
||||
"core_dr_call_conversion": ["name", "headquarter", "month", "doctor_name", "date_of_visit", "product_targeted", "status"],
|
||||
"area_covered": ["name", "headquarter", "month", "sale_status", "area_to_cover", "sale_objective", "status"],
|
||||
"sales_and_stock_statement": ["name", "headquarter", "report_date", "month", "serial_no", "product_name", "opening", "receipt", "sale", "closing", "value_in_pts"],
|
||||
"tour_plan": ["name", "headquarter", "serial_no", "report_date", "from_location", "to_location", "pob", "stockiest_visit"],
|
||||
"standard_tour_plan": ["name", "headquarter", "serial_no", "place"],
|
||||
"sales_projection": ["serial_no", "product_name", "pts", "secondary_qty", "value", "closing", "secondary_value", "aug_sales", "value_2"],
|
||||
}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.drop_table("csv_import_row")
|
||||
for table_name, columns in TABLE_COLUMNS.items():
|
||||
op.create_table(
|
||||
table_name,
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("source_row_number", sa.Integer(), nullable=False),
|
||||
*(sa.Column(column, sa.Text(), nullable=True) for column in columns),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("source_row_number", name=f"uq_{table_name}_source_row"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
for table_name in reversed(TABLE_COLUMNS):
|
||||
op.drop_table(table_name)
|
||||
op.create_table(
|
||||
"csv_import_row",
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("source_file", sa.String(length=255), nullable=False),
|
||||
sa.Column("row_number", sa.Integer(), nullable=False),
|
||||
sa.Column("data", sa.JSON(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("source_file", "row_number", name="uq_csv_import_file_row"),
|
||||
)
|
||||
@@ -0,0 +1,43 @@
|
||||
"""add the sales projection CSV table
|
||||
|
||||
Revision ID: e5a1c7d9f3b2
|
||||
Revises: d4f8a6c2e1b0
|
||||
Create Date: 2026-09-18
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "e5a1c7d9f3b2"
|
||||
down_revision: str | None = "d4f8a6c2e1b0"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# A pre-migration local table may exist with the old product-master schema.
|
||||
# The CSV is the canonical source, so recreate it using the shared CSV-table shape.
|
||||
op.execute("DROP TABLE IF EXISTS sales_projection")
|
||||
op.create_table(
|
||||
"sales_projection",
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column("source_row_number", sa.Integer(), nullable=False),
|
||||
sa.Column("serial_no", sa.Text(), nullable=True),
|
||||
sa.Column("product_name", sa.Text(), nullable=True),
|
||||
sa.Column("pts", sa.Text(), nullable=True),
|
||||
sa.Column("secondary_qty", sa.Text(), nullable=True),
|
||||
sa.Column("value", sa.Text(), nullable=True),
|
||||
sa.Column("closing", sa.Text(), nullable=True),
|
||||
sa.Column("secondary_value", sa.Text(), nullable=True),
|
||||
sa.Column("aug_sales", sa.Text(), nullable=True),
|
||||
sa.Column("value_2", sa.Text(), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("source_row_number", name="uq_sales_projection_source_row"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("sales_projection")
|
||||
@@ -0,0 +1,35 @@
|
||||
"""add account identity fields to sales projection
|
||||
|
||||
Revision ID: f6b2d8e4a1c3
|
||||
Revises: e5a1c7d9f3b2
|
||||
Create Date: 2026-09-18
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "f6b2d8e4a1c3"
|
||||
down_revision: str | None = "e5a1c7d9f3b2"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("sales_projection", sa.Column("name", sa.Text(), nullable=True))
|
||||
op.add_column("sales_projection", sa.Column("headquarter", sa.Text(), nullable=True))
|
||||
op.execute(
|
||||
'''UPDATE "sales_projection" AS projection
|
||||
SET name = account.full_name,
|
||||
headquarter = headquarters.name
|
||||
FROM "user" AS account
|
||||
LEFT JOIN headquarters ON headquarters.id = account.headquarters_id
|
||||
WHERE account.role = 'MR' '''
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("sales_projection", "headquarter")
|
||||
op.drop_column("sales_projection", "name")
|
||||
@@ -0,0 +1,27 @@
|
||||
"""add date and month fields to sales projection
|
||||
|
||||
Revision ID: g7c3e9f5b2d4
|
||||
Revises: f6b2d8e4a1c3
|
||||
Create Date: 2026-09-19
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "g7c3e9f5b2d4"
|
||||
down_revision: str | None = "f6b2d8e4a1c3"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("sales_projection", sa.Column("report_date", sa.Text(), nullable=True))
|
||||
op.add_column("sales_projection", sa.Column("month", sa.Text(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("sales_projection", "month")
|
||||
op.drop_column("sales_projection", "report_date")
|
||||
@@ -0,0 +1,82 @@
|
||||
"""add the stock inventory table behind the Products page and load stock.csv
|
||||
|
||||
The Products page (GET /products/stock-inventory) reads ``stock_inventory``, which no
|
||||
migration created — the endpoint returned 500 on any database built from migrations.
|
||||
The rows are the company's product / pack / quantity list (stock.csv). They are inlined
|
||||
here because the CSV files are not part of the Docker image.
|
||||
|
||||
Revision ID: h8d4f0a6c3e5
|
||||
Revises: g7c3e9f5b2d4
|
||||
Create Date: 2026-09-20
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "h8d4f0a6c3e5"
|
||||
down_revision: str | None = "g7c3e9f5b2d4"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
# (serial_no, product_name, pack, quantity, unit) — straight from stock.csv
|
||||
STOCK_ROWS = [
|
||||
(1, 'INTEZOLE-PD', '10X10 CAP', '10+2', None),
|
||||
(2, 'INTEZOLE-RD', '10X10 CAP', '10+2', None),
|
||||
(3, 'INTEZOLE-EL', '10X10 CAP', '10+2', None),
|
||||
(4, 'INTEZOLE-ED', '10X10 CAP', '10+2', None),
|
||||
(5, 'INTEZOLE-P40', '10X10 TAB', '10+1', None),
|
||||
(6, 'FENMONAC-P', '10X10 TAB', '10+1', None),
|
||||
(7, 'FENMONAC-SP', '10X10 TAB', '10+1', None),
|
||||
(8, 'FENMONAC-MR', '10X10 TAB', '10+1', None),
|
||||
(9, 'PENORENZ-MR 4', '10X10 TAB', '10+1', None),
|
||||
(10, 'CELVADOX-CV', '10X1X10 TAB', 'NA', None),
|
||||
(11, 'AMOCLADOV-CV 625', '10X1X10 TAB', '10+1', None),
|
||||
(12, 'INFEZOX-O', '10X10 TAB', '9+1', None),
|
||||
(13, 'FEBRIVANCE-PLUS', '10X10 TAB', '10+2', None),
|
||||
(14, 'HIFERGLOBIN-XT', '10X10 TAB', '9+1', None),
|
||||
(15, 'VIVACELLIX-360', '10X10 TAB', 'NA', None),
|
||||
(16, 'HEALTHOVANCE-SUPER', '10X10 TAB', '9+1', None),
|
||||
(17, 'HEALTHOVANCE-Q10', '10X10 TAB', 'NA', None),
|
||||
(18, 'MEGAVANCE-FORTE', '10X1X10CAP', '10+2', None),
|
||||
(19, 'BONETRA-STRONG', '10X1X10CAP', '9+1', None),
|
||||
(20, 'BONETRA-MAX+', '10X1X10CAP', '9+1', None),
|
||||
(21, 'WEAKOVER', '200ML SYRUP', '11+1', None),
|
||||
(22, 'BELLYFULLY', '200ML SYRUP', '11+1', None),
|
||||
(23, 'TUMMY SURE', '200ML SYRUP', '14+1', None),
|
||||
(24, 'VISCOSYNTH', '200ML SYRUP', '11+1', None),
|
||||
(25, 'HIFERGLOBIN-RED+', '200ML SYRUP', '14+1', None),
|
||||
(26, 'LYPOPINK-ADV', '200ML SYRUP', 'NA', None),
|
||||
]
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
stock = op.create_table(
|
||||
"stock_inventory",
|
||||
sa.Column("id", sa.Integer(), sa.Identity(), primary_key=True),
|
||||
sa.Column("serial_no", sa.Integer(), nullable=False),
|
||||
sa.Column("product_name", sa.Text(), nullable=False),
|
||||
sa.Column("pack", sa.Text(), nullable=True),
|
||||
sa.Column("quantity", sa.Text(), nullable=True),
|
||||
sa.Column("unit", sa.Text(), nullable=True),
|
||||
sa.UniqueConstraint("serial_no", name="uq_stock_inventory_serial_no"),
|
||||
)
|
||||
op.bulk_insert(
|
||||
stock,
|
||||
[
|
||||
{
|
||||
"serial_no": serial_no,
|
||||
"product_name": product_name,
|
||||
"pack": pack,
|
||||
"quantity": quantity,
|
||||
"unit": unit,
|
||||
}
|
||||
for serial_no, product_name, pack, quantity, unit in STOCK_ROWS
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("stock_inventory")
|
||||
@@ -0,0 +1,28 @@
|
||||
"""remove the General Manager approval stage
|
||||
|
||||
The Area Manager is the final approver: PENDING_AM -> APPROVED | RETURNED. Any report
|
||||
still sitting in the retired PENDING_GM status goes back to the Area Manager's queue.
|
||||
Historical audit rows (including FORWARD) are left untouched.
|
||||
|
||||
Revision ID: i9e5a1b7d4f6
|
||||
Revises: h8d4f0a6c3e5
|
||||
Create Date: 2026-09-21
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "i9e5a1b7d4f6"
|
||||
down_revision: str | None = "h8d4f0a6c3e5"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute("UPDATE daily_call_report SET status = 'PENDING_AM' WHERE status = 'PENDING_GM'")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# Nothing to restore: the GM stage no longer exists in the code.
|
||||
pass
|
||||
@@ -0,0 +1,37 @@
|
||||
"""capture the printed Daily Call Report's extra fields
|
||||
|
||||
Doctor visits gain PRODUCT PROMOTE / PRODUCT SAMPLE, chemist visits gain POB, and the
|
||||
report gains STOCKIST VISIT / ORDER, so the approved PDF can reproduce the paper form.
|
||||
All nullable: existing reports are unaffected.
|
||||
|
||||
Revision ID: j0f6b2c8e5a7
|
||||
Revises: i9e5a1b7d4f6
|
||||
Create Date: 2026-09-21
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "j0f6b2c8e5a7"
|
||||
down_revision: str | None = "i9e5a1b7d4f6"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("doctor_call", sa.Column("product_promote", sa.String(length=200), nullable=True))
|
||||
op.add_column("doctor_call", sa.Column("product_sample", sa.String(length=200), nullable=True))
|
||||
op.add_column("chemist_call", sa.Column("pob", sa.String(length=100), nullable=True))
|
||||
op.add_column("daily_call_report", sa.Column("stockist_visit", sa.String(length=200), nullable=True))
|
||||
op.add_column("daily_call_report", sa.Column("order_value", sa.String(length=100), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("daily_call_report", "order_value")
|
||||
op.drop_column("daily_call_report", "stockist_visit")
|
||||
op.drop_column("chemist_call", "pob")
|
||||
op.drop_column("doctor_call", "product_sample")
|
||||
op.drop_column("doctor_call", "product_promote")
|
||||
@@ -0,0 +1,37 @@
|
||||
"""store company-form approvals on the server
|
||||
|
||||
Until now the "approved / pending" state of the tour plan, standard tour plan and fare
|
||||
chart lived in each browser's local storage: an Area Manager's approval was invisible to
|
||||
the MR, and the PDF signature was whatever the browser sent. It is now a table.
|
||||
|
||||
Revision ID: k1a7c3d9f6b8
|
||||
Revises: j0f6b2c8e5a7
|
||||
Create Date: 2026-09-21
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "k1a7c3d9f6b8"
|
||||
down_revision: str | None = "j0f6b2c8e5a7"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"form_approval",
|
||||
sa.Column("table_name", sa.String(length=64), primary_key=True),
|
||||
sa.Column("status", sa.String(length=10), nullable=False, server_default="draft"),
|
||||
sa.Column("requested_by_user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="SET NULL"), nullable=True),
|
||||
sa.Column("approved_by_user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="SET NULL"), nullable=True),
|
||||
sa.Column("approved_by_name", sa.String(length=200), nullable=True),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("form_approval")
|
||||
@@ -0,0 +1,77 @@
|
||||
"""company forms belong to one MR, and so does their approval
|
||||
|
||||
The tour plan, standard tour plan and fare chart were one shared table with one shared
|
||||
approval, so any Area Manager could sign for any MR. Each row now records its owner (the MR),
|
||||
and an approval is kept per (form, MR) so it can only be requested from, and signed by, the
|
||||
Area Manager who manages that MR's headquarters.
|
||||
|
||||
Revision ID: l2b8d4e0a7c9
|
||||
Revises: k1a7c3d9f6b8
|
||||
Create Date: 2026-09-21
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "l2b8d4e0a7c9"
|
||||
down_revision: str | None = "k1a7c3d9f6b8"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
OWNED = ("tour_plan", "standard_tour_plan", "fare_chart")
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("form_approval", sa.Column("owner_user_id", sa.UUID(), nullable=True))
|
||||
# The MR who sent a form for approval is the one whose data it was.
|
||||
op.execute("UPDATE form_approval SET owner_user_id = requested_by_user_id")
|
||||
op.execute("DELETE FROM form_approval WHERE owner_user_id IS NULL")
|
||||
op.drop_constraint("form_approval_pkey", "form_approval", type_="primary")
|
||||
op.alter_column("form_approval", "owner_user_id", nullable=False)
|
||||
op.create_primary_key("form_approval_pkey", "form_approval", ["table_name", "owner_user_id"])
|
||||
op.create_foreign_key(
|
||||
"fk_form_approval_owner", "form_approval", "user", ["owner_user_id"], ["id"], ondelete="CASCADE"
|
||||
)
|
||||
|
||||
for table in OWNED:
|
||||
op.add_column(table, sa.Column("owner_user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="SET NULL"), nullable=True))
|
||||
op.create_index(f"ix_{table}_owner", table, ["owner_user_id"])
|
||||
# Rows that existed belong to the MR who put the form up for approval, with that MR's identity.
|
||||
op.execute(
|
||||
f'''UPDATE "{table}" t
|
||||
SET owner_user_id = a.owner_user_id, name = u.full_name, headquarter = h.name
|
||||
FROM form_approval a
|
||||
JOIN "user" u ON u.id = a.owner_user_id
|
||||
LEFT JOIN headquarters h ON h.id = u.headquarters_id
|
||||
WHERE a.table_name = '{table}' '''
|
||||
)
|
||||
|
||||
# An approval by an Area Manager who does not manage the MR's headquarters was never valid:
|
||||
# send it back to the right one.
|
||||
op.execute(
|
||||
'''UPDATE form_approval a
|
||||
SET status = 'pending', approved_by_user_id = NULL, approved_by_name = NULL
|
||||
FROM "user" u
|
||||
WHERE u.id = a.owner_user_id AND a.status = 'approved'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM am_headquarters m
|
||||
WHERE m.headquarters_id = u.headquarters_id AND m.effective_to IS NULL
|
||||
AND m.area_manager_user_id = a.approved_by_user_id)'''
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
for table in OWNED:
|
||||
op.drop_index(f"ix_{table}_owner", table_name=table)
|
||||
op.drop_column(table, "owner_user_id")
|
||||
op.drop_constraint("fk_form_approval_owner", "form_approval", type_="foreignkey")
|
||||
op.drop_constraint("form_approval_pkey", "form_approval", type_="primary")
|
||||
op.execute(
|
||||
"DELETE FROM form_approval a USING form_approval b "
|
||||
"WHERE a.table_name = b.table_name AND a.ctid < b.ctid"
|
||||
)
|
||||
op.drop_column("form_approval", "owner_user_id")
|
||||
op.create_primary_key("form_approval_pkey", "form_approval", ["table_name"])
|
||||
Reference in New Issue
Block a user