Files
drivers_bot/alembic/versions/202605150002_sto_booking_automation.py
2026-05-15 05:17:54 +09:00

203 lines
14 KiB
Python

"""sto booking automation
Revision ID: 202605150002
Revises: 202605150001
Create Date: 2026-05-15 08:00:00.000000
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "202605150002"
down_revision: str | None = "202605150001"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.create_table(
"service_center_booking_settings",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("service_center_id", sa.Integer(), nullable=False),
sa.Column("working_days", sa.JSON(), server_default=sa.text("'[0,1,2,3,4]'"), nullable=False),
sa.Column("open_time", sa.Time(), server_default="09:00:00", nullable=False),
sa.Column("close_time", sa.Time(), server_default="18:00:00", nullable=False),
sa.Column("lunch_break_start", sa.Time(), nullable=True),
sa.Column("lunch_break_end", sa.Time(), nullable=True),
sa.Column("timezone", sa.String(length=64), server_default="Asia/Seoul", nullable=False),
sa.Column("slot_duration_minutes", sa.Integer(), server_default="30", nullable=False),
sa.Column("booking_buffer_minutes", sa.Integer(), server_default="0", nullable=False),
sa.Column("max_parallel_bookings", sa.Integer(), server_default="1", nullable=False),
sa.Column("accepts_online_booking", sa.Boolean(), server_default=sa.text("true"), nullable=False),
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(["service_center_id"], ["service_centers.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("service_center_id"),
)
op.create_index("ix_service_center_booking_settings_accepts_online_booking", "service_center_booking_settings", ["accepts_online_booking"])
op.create_index("ix_service_center_booking_settings_service_center_id", "service_center_booking_settings", ["service_center_id"])
op.create_table(
"service_center_holidays",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("service_center_id", sa.Integer(), nullable=False),
sa.Column("holiday_date", sa.Date(), nullable=False),
sa.Column("reason", sa.String(length=240), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
sa.ForeignKeyConstraint(["service_center_id"], ["service_centers.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("service_center_id", "holiday_date", name="uq_service_center_holiday"),
)
op.create_index("ix_service_center_holidays_holiday_date", "service_center_holidays", ["holiday_date"])
op.create_index("ix_service_center_holidays_service_center_id", "service_center_holidays", ["service_center_id"])
op.create_table(
"maintenance_recommendations",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("vehicle_id", sa.Integer(), nullable=False),
sa.Column("recommendation_type", sa.String(length=64), nullable=False),
sa.Column("title", sa.String(length=180), nullable=False),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("due_odometer_km", sa.Integer(), nullable=True),
sa.Column("due_date", sa.Date(), nullable=True),
sa.Column("priority", sa.String(length=24), server_default="medium", nullable=False),
sa.Column("status", sa.String(length=24), server_default="active", nullable=False),
sa.Column("source", sa.String(length=40), server_default="system_analysis", nullable=False),
sa.Column("source_service_center_id", sa.Integer(), nullable=True),
sa.Column("source_appointment_id", sa.Integer(), 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(["source_service_center_id"], ["service_centers.id"], ondelete="SET NULL"),
sa.ForeignKeyConstraint(["vehicle_id"], ["cars.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_maintenance_recommendations_created_at", "maintenance_recommendations", ["created_at"])
op.create_index("ix_maintenance_recommendations_due_date", "maintenance_recommendations", ["due_date"])
op.create_index("ix_maintenance_recommendations_due_odometer_km", "maintenance_recommendations", ["due_odometer_km"])
op.create_index("ix_maintenance_recommendations_priority", "maintenance_recommendations", ["priority"])
op.create_index("ix_maintenance_recommendations_recommendation_type", "maintenance_recommendations", ["recommendation_type"])
op.create_index("ix_maintenance_recommendations_source", "maintenance_recommendations", ["source"])
op.create_index("ix_maintenance_recommendations_source_appointment_id", "maintenance_recommendations", ["source_appointment_id"])
op.create_index("ix_maintenance_recommendations_source_service_center_id", "maintenance_recommendations", ["source_service_center_id"])
op.create_index("ix_maintenance_recommendations_status", "maintenance_recommendations", ["status"])
op.create_index("ix_maintenance_recommendations_vehicle_id", "maintenance_recommendations", ["vehicle_id"])
op.create_table(
"service_appointments",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("service_center_id", sa.Integer(), nullable=False),
sa.Column("vehicle_id", sa.Integer(), nullable=False),
sa.Column("owner_id", sa.Integer(), nullable=False),
sa.Column("created_by", sa.Integer(), nullable=False),
sa.Column("service_type", sa.String(length=64), nullable=False),
sa.Column("service_name", sa.String(length=180), nullable=False),
sa.Column("requested_start_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("requested_end_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("confirmed_start_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("confirmed_end_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("proposed_start_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("proposed_end_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("estimated_duration_minutes", sa.Integer(), server_default="60", nullable=False),
sa.Column("status", sa.String(length=40), server_default="requested", nullable=False),
sa.Column("customer_comment", sa.Text(), nullable=True),
sa.Column("service_center_comment", sa.Text(), nullable=True),
sa.Column("source_recommendation_id", sa.Integer(), nullable=True),
sa.Column("linked_work_order_id", sa.Integer(), nullable=True),
sa.Column("cancellation_reason", sa.Text(), nullable=True),
sa.Column("cancelled_at", sa.DateTime(timezone=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"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["linked_work_order_id"], ["service_visits.id"], ondelete="SET NULL"),
sa.ForeignKeyConstraint(["owner_id"], ["users.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["service_center_id"], ["service_centers.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["source_recommendation_id"], ["maintenance_recommendations.id"], ondelete="SET NULL"),
sa.ForeignKeyConstraint(["vehicle_id"], ["cars.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_service_appointments_confirmed_start_at", "service_appointments", ["confirmed_start_at"])
op.create_index("ix_service_appointments_created_at", "service_appointments", ["created_at"])
op.create_index("ix_service_appointments_created_by", "service_appointments", ["created_by"])
op.create_index("ix_service_appointments_linked_work_order_id", "service_appointments", ["linked_work_order_id"])
op.create_index("ix_service_appointments_owner_id", "service_appointments", ["owner_id"])
op.create_index("ix_service_appointments_proposed_start_at", "service_appointments", ["proposed_start_at"])
op.create_index("ix_service_appointments_requested_end_at", "service_appointments", ["requested_end_at"])
op.create_index("ix_service_appointments_requested_start_at", "service_appointments", ["requested_start_at"])
op.create_index("ix_service_appointments_service_center_id", "service_appointments", ["service_center_id"])
op.create_index("ix_service_appointments_service_type", "service_appointments", ["service_type"])
op.create_index("ix_service_appointments_source_recommendation_id", "service_appointments", ["source_recommendation_id"])
op.create_index("ix_service_appointments_status", "service_appointments", ["status"])
op.create_index("ix_service_appointments_vehicle_id", "service_appointments", ["vehicle_id"])
op.create_table(
"service_notifications",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("recipient_user_id", sa.Integer(), nullable=False),
sa.Column("service_center_id", sa.Integer(), nullable=True),
sa.Column("appointment_id", sa.Integer(), nullable=True),
sa.Column("notification_type", sa.String(length=80), nullable=False),
sa.Column("title", sa.String(length=180), nullable=False),
sa.Column("body", sa.Text(), nullable=True),
sa.Column("status", sa.String(length=24), server_default="unread", nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
sa.ForeignKeyConstraint(["appointment_id"], ["service_appointments.id"], ondelete="SET NULL"),
sa.ForeignKeyConstraint(["recipient_user_id"], ["users.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["service_center_id"], ["service_centers.id"], ondelete="SET NULL"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_service_notifications_appointment_id", "service_notifications", ["appointment_id"])
op.create_index("ix_service_notifications_created_at", "service_notifications", ["created_at"])
op.create_index("ix_service_notifications_notification_type", "service_notifications", ["notification_type"])
op.create_index("ix_service_notifications_recipient_user_id", "service_notifications", ["recipient_user_id"])
op.create_index("ix_service_notifications_service_center_id", "service_notifications", ["service_center_id"])
op.create_index("ix_service_notifications_status", "service_notifications", ["status"])
def downgrade() -> None:
op.drop_index("ix_service_notifications_status", table_name="service_notifications")
op.drop_index("ix_service_notifications_service_center_id", table_name="service_notifications")
op.drop_index("ix_service_notifications_recipient_user_id", table_name="service_notifications")
op.drop_index("ix_service_notifications_notification_type", table_name="service_notifications")
op.drop_index("ix_service_notifications_created_at", table_name="service_notifications")
op.drop_index("ix_service_notifications_appointment_id", table_name="service_notifications")
op.drop_table("service_notifications")
op.drop_index("ix_service_appointments_vehicle_id", table_name="service_appointments")
op.drop_index("ix_service_appointments_status", table_name="service_appointments")
op.drop_index("ix_service_appointments_source_recommendation_id", table_name="service_appointments")
op.drop_index("ix_service_appointments_service_type", table_name="service_appointments")
op.drop_index("ix_service_appointments_service_center_id", table_name="service_appointments")
op.drop_index("ix_service_appointments_requested_start_at", table_name="service_appointments")
op.drop_index("ix_service_appointments_requested_end_at", table_name="service_appointments")
op.drop_index("ix_service_appointments_proposed_start_at", table_name="service_appointments")
op.drop_index("ix_service_appointments_owner_id", table_name="service_appointments")
op.drop_index("ix_service_appointments_linked_work_order_id", table_name="service_appointments")
op.drop_index("ix_service_appointments_created_by", table_name="service_appointments")
op.drop_index("ix_service_appointments_created_at", table_name="service_appointments")
op.drop_index("ix_service_appointments_confirmed_start_at", table_name="service_appointments")
op.drop_table("service_appointments")
op.drop_index("ix_maintenance_recommendations_vehicle_id", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_status", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_source_service_center_id", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_source_appointment_id", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_source", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_recommendation_type", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_priority", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_due_odometer_km", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_due_date", table_name="maintenance_recommendations")
op.drop_index("ix_maintenance_recommendations_created_at", table_name="maintenance_recommendations")
op.drop_table("maintenance_recommendations")
op.drop_index("ix_service_center_holidays_service_center_id", table_name="service_center_holidays")
op.drop_index("ix_service_center_holidays_holiday_date", table_name="service_center_holidays")
op.drop_table("service_center_holidays")
op.drop_index("ix_service_center_booking_settings_service_center_id", table_name="service_center_booking_settings")
op.drop_index("ix_service_center_booking_settings_accepts_online_booking", table_name="service_center_booking_settings")
op.drop_table("service_center_booking_settings")