This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""promote requested admin user
|
||||
"""legacy admin bootstrap placeholder
|
||||
|
||||
Revision ID: 202605150001
|
||||
Revises: 202605140002
|
||||
@@ -7,33 +7,15 @@ Create Date: 2026-05-15 05:00:00.000000
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "202605150001"
|
||||
down_revision: str | None = "202605140002"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
ADMIN_TELEGRAM_ID = 556399210
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.execute(
|
||||
f"""
|
||||
insert into users (telegram_id, username, platform_role)
|
||||
values ({ADMIN_TELEGRAM_ID}, '{ADMIN_TELEGRAM_ID}', 'admin')
|
||||
on conflict (telegram_id) do update
|
||||
set platform_role = 'admin'
|
||||
"""
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute(
|
||||
f"""
|
||||
update users
|
||||
set platform_role = 'user'
|
||||
where telegram_id = {ADMIN_TELEGRAM_ID}
|
||||
and platform_role = 'admin'
|
||||
"""
|
||||
)
|
||||
return None
|
||||
|
||||
210
alembic/versions/202605150003_production_work_orders.py
Normal file
210
alembic/versions/202605150003_production_work_orders.py
Normal file
@@ -0,0 +1,210 @@
|
||||
"""production work orders, employee invites, notifications
|
||||
|
||||
Revision ID: 202605150003
|
||||
Revises: 202605150002
|
||||
Create Date: 2026-05-15 12:00:00.000000
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "202605150003"
|
||||
down_revision: str | None = "202605150002"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("service_entries", sa.Column("service_visit_id", sa.Integer(), nullable=True))
|
||||
op.create_index("ix_service_entries_service_visit_id", "service_entries", ["service_visit_id"])
|
||||
op.create_foreign_key(
|
||||
"fk_service_entries_service_visit_id_service_visits",
|
||||
"service_entries",
|
||||
"service_visits",
|
||||
["service_visit_id"],
|
||||
["id"],
|
||||
ondelete="SET NULL",
|
||||
)
|
||||
op.add_column("expense_entries", sa.Column("service_visit_id", sa.Integer(), nullable=True))
|
||||
op.create_index("ix_expense_entries_service_visit_id", "expense_entries", ["service_visit_id"])
|
||||
op.create_foreign_key(
|
||||
"fk_expense_entries_service_visit_id_service_visits",
|
||||
"expense_entries",
|
||||
"service_visits",
|
||||
["service_visit_id"],
|
||||
["id"],
|
||||
ondelete="SET NULL",
|
||||
)
|
||||
|
||||
op.add_column("service_employees", sa.Column("invite_token", sa.String(length=96), nullable=True))
|
||||
op.add_column("service_employees", sa.Column("invite_expires_at", sa.DateTime(timezone=True), nullable=True))
|
||||
op.add_column("service_employees", sa.Column("invite_revoked_at", sa.DateTime(timezone=True), nullable=True))
|
||||
op.add_column("service_employees", sa.Column("activated_at", sa.DateTime(timezone=True), nullable=True))
|
||||
op.create_index("ix_service_employees_invite_token", "service_employees", ["invite_token"], unique=True)
|
||||
|
||||
op.add_column("service_visits", sa.Column("work_order_number", sa.String(length=40), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("owner_id", sa.Integer(), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("assigned_employee_id", sa.Integer(), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("customer_complaint", sa.Text(), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("diagnosis", sa.Text(), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("service_comment", sa.Text(), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("owner_comment", sa.Text(), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("recommendations_text", sa.Text(), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("attachment_urls", sa.JSON(), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("labor_total", sa.Numeric(12, 2), server_default="0", nullable=False))
|
||||
op.add_column("service_visits", sa.Column("product_total", sa.Numeric(12, 2), server_default="0", nullable=False))
|
||||
op.add_column("service_visits", sa.Column("discount_total", sa.Numeric(12, 2), server_default="0", nullable=False))
|
||||
op.add_column("service_visits", sa.Column("final_total", sa.Numeric(12, 2), server_default="0", nullable=False))
|
||||
op.add_column("service_visits", sa.Column("price_approved_total", sa.Numeric(12, 2), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("approval_required", sa.Boolean(), server_default=sa.text("false"), nullable=False))
|
||||
op.add_column("service_visits", sa.Column("opened_at", sa.DateTime(timezone=True), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("approved_at", sa.DateTime(timezone=True), nullable=True))
|
||||
op.add_column("service_visits", sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True))
|
||||
op.create_index("ix_service_visits_work_order_number", "service_visits", ["work_order_number"], unique=True)
|
||||
op.create_index("ix_service_visits_owner_id", "service_visits", ["owner_id"])
|
||||
op.create_index("ix_service_visits_assigned_employee_id", "service_visits", ["assigned_employee_id"])
|
||||
op.create_foreign_key("fk_service_visits_owner_id_users", "service_visits", "users", ["owner_id"], ["id"], ondelete="SET NULL")
|
||||
op.create_foreign_key(
|
||||
"fk_service_visits_assigned_employee_id_service_employees",
|
||||
"service_visits",
|
||||
"service_employees",
|
||||
["assigned_employee_id"],
|
||||
["id"],
|
||||
ondelete="SET NULL",
|
||||
)
|
||||
|
||||
op.add_column("service_work_items", sa.Column("category", sa.String(length=80), nullable=True))
|
||||
op.add_column("service_work_items", sa.Column("quantity", sa.Numeric(10, 3), server_default="1", nullable=False))
|
||||
op.add_column("service_work_items", sa.Column("unit", sa.String(length=24), server_default="pcs", nullable=False))
|
||||
op.add_column("service_work_items", sa.Column("unit_price", sa.Numeric(12, 2), nullable=True))
|
||||
op.add_column("service_work_items", sa.Column("discount", sa.Numeric(12, 2), server_default="0", nullable=False))
|
||||
op.add_column("service_work_items", sa.Column("total", sa.Numeric(12, 2), nullable=True))
|
||||
op.add_column("service_work_items", sa.Column("warranty_days", sa.Integer(), nullable=True))
|
||||
op.add_column("service_work_items", sa.Column("warranty_odometer_km", sa.Integer(), nullable=True))
|
||||
|
||||
op.add_column("service_notifications", sa.Column("retry_count", sa.Integer(), server_default="0", nullable=False))
|
||||
op.add_column("service_notifications", sa.Column("last_error", sa.Text(), nullable=True))
|
||||
op.add_column("service_notifications", sa.Column("idempotency_key", sa.String(length=160), nullable=True))
|
||||
op.add_column("service_notifications", sa.Column("sent_at", sa.DateTime(timezone=True), nullable=True))
|
||||
op.add_column("service_notifications", sa.Column("read_at", sa.DateTime(timezone=True), nullable=True))
|
||||
op.create_index("ix_service_notifications_idempotency_key", "service_notifications", ["idempotency_key"], unique=True)
|
||||
op.execute("update service_notifications set status = 'pending' where status = 'unread'")
|
||||
|
||||
op.create_table(
|
||||
"service_product_items",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("service_visit_id", sa.Integer(), nullable=False),
|
||||
sa.Column("title", sa.String(length=180), nullable=False),
|
||||
sa.Column("category", sa.String(length=80), nullable=True),
|
||||
sa.Column("product_type", sa.String(length=40), server_default="other", nullable=False),
|
||||
sa.Column("brand", sa.String(length=80), nullable=True),
|
||||
sa.Column("sku", sa.String(length=120), nullable=True),
|
||||
sa.Column("quantity", sa.Numeric(10, 3), server_default="1", nullable=False),
|
||||
sa.Column("unit", sa.String(length=24), server_default="pcs", nullable=False),
|
||||
sa.Column("unit_price", sa.Numeric(12, 2), server_default="0", nullable=False),
|
||||
sa.Column("discount", sa.Numeric(12, 2), server_default="0", nullable=False),
|
||||
sa.Column("total", sa.Numeric(12, 2), server_default="0", nullable=False),
|
||||
sa.Column("volume", sa.Numeric(8, 3), nullable=True),
|
||||
sa.Column("viscosity", sa.String(length=40), nullable=True),
|
||||
sa.Column("specification", sa.String(length=120), nullable=True),
|
||||
sa.Column("used_volume", sa.Numeric(8, 3), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["service_visit_id"], ["service_visits.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index("ix_service_product_items_category", "service_product_items", ["category"])
|
||||
op.create_index("ix_service_product_items_product_type", "service_product_items", ["product_type"])
|
||||
op.create_index("ix_service_product_items_service_visit_id", "service_product_items", ["service_visit_id"])
|
||||
|
||||
op.create_table(
|
||||
"work_order_status_history",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("service_visit_id", sa.Integer(), nullable=False),
|
||||
sa.Column("from_status", sa.String(length=40), nullable=True),
|
||||
sa.Column("to_status", sa.String(length=40), nullable=False),
|
||||
sa.Column("changed_by_user_id", sa.Integer(), nullable=True),
|
||||
sa.Column("comment", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["changed_by_user_id"], ["users.id"], ondelete="SET NULL"),
|
||||
sa.ForeignKeyConstraint(["service_visit_id"], ["service_visits.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index("ix_work_order_status_history_changed_by_user_id", "work_order_status_history", ["changed_by_user_id"])
|
||||
op.create_index("ix_work_order_status_history_created_at", "work_order_status_history", ["created_at"])
|
||||
op.create_index("ix_work_order_status_history_service_visit_id", "work_order_status_history", ["service_visit_id"])
|
||||
op.create_index("ix_work_order_status_history_to_status", "work_order_status_history", ["to_status"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_constraint("fk_expense_entries_service_visit_id_service_visits", "expense_entries", type_="foreignkey")
|
||||
op.drop_index("ix_expense_entries_service_visit_id", table_name="expense_entries")
|
||||
op.drop_column("expense_entries", "service_visit_id")
|
||||
op.drop_constraint("fk_service_entries_service_visit_id_service_visits", "service_entries", type_="foreignkey")
|
||||
op.drop_index("ix_service_entries_service_visit_id", table_name="service_entries")
|
||||
op.drop_column("service_entries", "service_visit_id")
|
||||
|
||||
op.drop_index("ix_work_order_status_history_to_status", table_name="work_order_status_history")
|
||||
op.drop_index("ix_work_order_status_history_service_visit_id", table_name="work_order_status_history")
|
||||
op.drop_index("ix_work_order_status_history_created_at", table_name="work_order_status_history")
|
||||
op.drop_index("ix_work_order_status_history_changed_by_user_id", table_name="work_order_status_history")
|
||||
op.drop_table("work_order_status_history")
|
||||
|
||||
op.drop_index("ix_service_product_items_service_visit_id", table_name="service_product_items")
|
||||
op.drop_index("ix_service_product_items_product_type", table_name="service_product_items")
|
||||
op.drop_index("ix_service_product_items_category", table_name="service_product_items")
|
||||
op.drop_table("service_product_items")
|
||||
|
||||
op.drop_index("ix_service_notifications_idempotency_key", table_name="service_notifications")
|
||||
op.drop_column("service_notifications", "read_at")
|
||||
op.drop_column("service_notifications", "sent_at")
|
||||
op.drop_column("service_notifications", "idempotency_key")
|
||||
op.drop_column("service_notifications", "last_error")
|
||||
op.drop_column("service_notifications", "retry_count")
|
||||
|
||||
for column_name in (
|
||||
"warranty_odometer_km",
|
||||
"warranty_days",
|
||||
"total",
|
||||
"discount",
|
||||
"unit_price",
|
||||
"unit",
|
||||
"quantity",
|
||||
"category",
|
||||
):
|
||||
op.drop_column("service_work_items", column_name)
|
||||
|
||||
op.drop_constraint("fk_service_visits_assigned_employee_id_service_employees", "service_visits", type_="foreignkey")
|
||||
op.drop_constraint("fk_service_visits_owner_id_users", "service_visits", type_="foreignkey")
|
||||
op.drop_index("ix_service_visits_assigned_employee_id", table_name="service_visits")
|
||||
op.drop_index("ix_service_visits_owner_id", table_name="service_visits")
|
||||
op.drop_index("ix_service_visits_work_order_number", table_name="service_visits")
|
||||
for column_name in (
|
||||
"completed_at",
|
||||
"approved_at",
|
||||
"opened_at",
|
||||
"approval_required",
|
||||
"price_approved_total",
|
||||
"final_total",
|
||||
"discount_total",
|
||||
"product_total",
|
||||
"labor_total",
|
||||
"attachment_urls",
|
||||
"recommendations_text",
|
||||
"owner_comment",
|
||||
"service_comment",
|
||||
"diagnosis",
|
||||
"customer_complaint",
|
||||
"assigned_employee_id",
|
||||
"owner_id",
|
||||
"work_order_number",
|
||||
):
|
||||
op.drop_column("service_visits", column_name)
|
||||
|
||||
op.drop_index("ix_service_employees_invite_token", table_name="service_employees")
|
||||
op.drop_column("service_employees", "activated_at")
|
||||
op.drop_column("service_employees", "invite_revoked_at")
|
||||
op.drop_column("service_employees", "invite_expires_at")
|
||||
op.drop_column("service_employees", "invite_token")
|
||||
114
alembic/versions/202605150004_production_guards.py
Normal file
114
alembic/versions/202605150004_production_guards.py
Normal file
@@ -0,0 +1,114 @@
|
||||
"""production idempotency, corrections and slot guards
|
||||
|
||||
Revision ID: 202605150004
|
||||
Revises: 202605150003
|
||||
Create Date: 2026-05-15 16:00:00.000000
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "202605150004"
|
||||
down_revision: str | None = "202605150003"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("service_visits", sa.Column("version", sa.Integer(), server_default="1", nullable=False))
|
||||
op.add_column("service_visits", sa.Column("completed_snapshot", sa.JSON(), nullable=True))
|
||||
|
||||
op.create_index(
|
||||
"uq_service_entries_service_visit_id_not_null",
|
||||
"service_entries",
|
||||
["service_visit_id"],
|
||||
unique=True,
|
||||
postgresql_where=sa.text("service_visit_id is not null"),
|
||||
)
|
||||
op.create_index(
|
||||
"uq_expense_entries_service_visit_id_not_null",
|
||||
"expense_entries",
|
||||
["service_visit_id"],
|
||||
unique=True,
|
||||
postgresql_where=sa.text("service_visit_id is not null"),
|
||||
)
|
||||
op.create_index(
|
||||
"uq_active_service_appointment_slot",
|
||||
"service_appointments",
|
||||
["service_center_id", "requested_start_at", "requested_end_at"],
|
||||
unique=True,
|
||||
postgresql_where=sa.text("status in ('requested','confirmed','confirmed_by_sto','proposed_new_time')"),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"work_order_corrections",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("service_visit_id", sa.Integer(), nullable=False),
|
||||
sa.Column("requested_by_user_id", sa.Integer(), nullable=True),
|
||||
sa.Column("reason", sa.Text(), nullable=False),
|
||||
sa.Column("proposed_changes", sa.JSON(), nullable=True),
|
||||
sa.Column("status", sa.String(length=24), server_default="pending", nullable=False),
|
||||
sa.Column("owner_approval_required", sa.Boolean(), server_default=sa.text("true"), nullable=False),
|
||||
sa.Column("created_version", sa.Integer(), server_default="1", nullable=False),
|
||||
sa.Column("resolved_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["requested_by_user_id"], ["users.id"], ondelete="SET NULL"),
|
||||
sa.ForeignKeyConstraint(["service_visit_id"], ["service_visits.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index("ix_work_order_corrections_created_at", "work_order_corrections", ["created_at"])
|
||||
op.create_index("ix_work_order_corrections_service_visit_id", "work_order_corrections", ["service_visit_id"])
|
||||
op.create_index("ix_work_order_corrections_status", "work_order_corrections", ["status"])
|
||||
|
||||
op.create_table(
|
||||
"inventory_transactions",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("service_center_id", sa.Integer(), nullable=False),
|
||||
sa.Column("service_visit_id", sa.Integer(), nullable=True),
|
||||
sa.Column("product_item_id", sa.Integer(), nullable=True),
|
||||
sa.Column("transaction_type", sa.String(length=32), nullable=False),
|
||||
sa.Column("sku", sa.String(length=120), nullable=True),
|
||||
sa.Column("title", sa.String(length=180), nullable=True),
|
||||
sa.Column("quantity", sa.Numeric(10, 3), server_default="0", nullable=False),
|
||||
sa.Column("unit", sa.String(length=24), server_default="pcs", nullable=False),
|
||||
sa.Column("actor_user_id", sa.Integer(), nullable=True),
|
||||
sa.Column("metadata_json", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["actor_user_id"], ["users.id"], ondelete="SET NULL"),
|
||||
sa.ForeignKeyConstraint(["product_item_id"], ["service_product_items.id"], ondelete="SET NULL"),
|
||||
sa.ForeignKeyConstraint(["service_center_id"], ["service_centers.id"], ondelete="CASCADE"),
|
||||
sa.ForeignKeyConstraint(["service_visit_id"], ["service_visits.id"], ondelete="SET NULL"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index("ix_inventory_transactions_actor_user_id", "inventory_transactions", ["actor_user_id"])
|
||||
op.create_index("ix_inventory_transactions_created_at", "inventory_transactions", ["created_at"])
|
||||
op.create_index("ix_inventory_transactions_product_item_id", "inventory_transactions", ["product_item_id"])
|
||||
op.create_index("ix_inventory_transactions_service_center_id", "inventory_transactions", ["service_center_id"])
|
||||
op.create_index("ix_inventory_transactions_service_visit_id", "inventory_transactions", ["service_visit_id"])
|
||||
op.create_index("ix_inventory_transactions_sku", "inventory_transactions", ["sku"])
|
||||
op.create_index("ix_inventory_transactions_transaction_type", "inventory_transactions", ["transaction_type"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_inventory_transactions_transaction_type", table_name="inventory_transactions")
|
||||
op.drop_index("ix_inventory_transactions_sku", table_name="inventory_transactions")
|
||||
op.drop_index("ix_inventory_transactions_service_visit_id", table_name="inventory_transactions")
|
||||
op.drop_index("ix_inventory_transactions_service_center_id", table_name="inventory_transactions")
|
||||
op.drop_index("ix_inventory_transactions_product_item_id", table_name="inventory_transactions")
|
||||
op.drop_index("ix_inventory_transactions_created_at", table_name="inventory_transactions")
|
||||
op.drop_index("ix_inventory_transactions_actor_user_id", table_name="inventory_transactions")
|
||||
op.drop_table("inventory_transactions")
|
||||
|
||||
op.drop_index("ix_work_order_corrections_status", table_name="work_order_corrections")
|
||||
op.drop_index("ix_work_order_corrections_service_visit_id", table_name="work_order_corrections")
|
||||
op.drop_index("ix_work_order_corrections_created_at", table_name="work_order_corrections")
|
||||
op.drop_table("work_order_corrections")
|
||||
|
||||
op.drop_index("uq_active_service_appointment_slot", table_name="service_appointments")
|
||||
op.drop_index("uq_expense_entries_service_visit_id_not_null", table_name="expense_entries")
|
||||
op.drop_index("uq_service_entries_service_visit_id_not_null", table_name="service_entries")
|
||||
op.drop_column("service_visits", "completed_snapshot")
|
||||
op.drop_column("service_visits", "version")
|
||||
Reference in New Issue
Block a user