schema_patch.py 5.99 KB
from __future__ import annotations

from sqlalchemy import text


def ensure_runtime_schema(engine) -> None:
    if not str(engine.url).startswith("sqlite"):
        return

    with engine.begin() as conn:
        table_names = {
            row[0]
            for row in conn.execute(text("SELECT name FROM sqlite_master WHERE type='table'"))
        }

        if "wecom_customer_position_groups" not in table_names:
            conn.execute(
                text(
                    """
                    CREATE TABLE wecom_customer_position_groups (
                        id INTEGER PRIMARY KEY,
                        customer_contact_id VARCHAR(191) NOT NULL,
                        customer_name VARCHAR(255),
                        bot_code VARCHAR(191) NOT NULL,
                        bot_id VARCHAR(128) NOT NULL,
                        qrcode_id INTEGER NOT NULL,
                        qrcode_name VARCHAR(255),
                        channel_code VARCHAR(100) NOT NULL,
                        position_id INTEGER NOT NULL,
                        position_code VARCHAR(100) NOT NULL,
                        position_name VARCHAR(255) NOT NULL,
                        group_contact_id VARCHAR(191),
                        group_name VARCHAR(255),
                        group_status VARCHAR(32) NOT NULL DEFAULT 'pending',
                        create_task_id VARCHAR(191),
                        create_status VARCHAR(32) NOT NULL DEFAULT 'pending',
                        member_snapshot JSON,
                        dispatch_enabled BOOLEAN NOT NULL DEFAULT 1,
                        last_message_sync_at DATETIME,
                        is_deleted BOOLEAN NOT NULL DEFAULT 0,
                        created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
                        updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
                    )
                    """
                )
            )
            conn.execute(text("CREATE UNIQUE INDEX uk_wecom_customer_position_group ON wecom_customer_position_groups(customer_contact_id, position_id)"))
            conn.execute(text("CREATE INDEX idx_wcpg_bot_group ON wecom_customer_position_groups(bot_code, group_contact_id)"))

        if "wecom_job_configs" not in table_names:
            conn.execute(
                text(
                    """
                    CREATE TABLE wecom_job_configs (
                        id INTEGER PRIMARY KEY,
                        job_key VARCHAR(64) NOT NULL UNIQUE,
                        enabled BOOLEAN NOT NULL DEFAULT 1,
                        last_run_at DATETIME,
                        last_status VARCHAR(32),
                        last_error TEXT,
                        created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
                        updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
                    )
                    """
                )
            )

        if "official_tabs" in table_names:
            official_tab_columns = {
                row[1]
                for row in conn.execute(text("PRAGMA table_info(official_tabs)"))
            }

            if "main_title" not in official_tab_columns:
                conn.execute(
                    text("ALTER TABLE official_tabs ADD COLUMN main_title VARCHAR(200)")
                )

            if "sub_title" not in official_tab_columns:
                conn.execute(
                    text("ALTER TABLE official_tabs ADD COLUMN sub_title VARCHAR(500)")
                )

        if "ai_agents" in table_names:
            ai_agent_columns = {
                row[1]
                for row in conn.execute(text("PRAGMA table_info(ai_agents)"))
            }

            if "managed_user_id" not in ai_agent_columns:
                conn.execute(
                    text("ALTER TABLE ai_agents ADD COLUMN managed_user_id INTEGER")
                )
                conn.execute(
                    text(
                        "CREATE INDEX IF NOT EXISTS ix_ai_agents_managed_user_id "
                        "ON ai_agents(managed_user_id)"
                    )
                )

            if "site_display_status" not in ai_agent_columns:
                conn.execute(
                    text(
                        "ALTER TABLE ai_agents ADD COLUMN site_display_status "
                        "VARCHAR(20) NOT NULL DEFAULT 'enable'"
                    )
                )
                conn.execute(
                    text(
                        "CREATE INDEX IF NOT EXISTS ix_ai_agents_site_display_status "
                        "ON ai_agents(site_display_status)"
                    )
                )

            if "summary" not in ai_agent_columns:
                conn.execute(
                    text("ALTER TABLE ai_agents ADD COLUMN summary VARCHAR(500)")
                )

        if "official_site_agent_display" in table_names:
            display_columns = {
                row[1]
                for row in conn.execute(text("PRAGMA table_info(official_site_agent_display)"))
            }

            if "media_url" not in display_columns:
                conn.execute(
                    text("ALTER TABLE official_site_agent_display ADD COLUMN media_url VARCHAR(500)")
                )

            if "media_type" not in display_columns:
                conn.execute(
                    text("ALTER TABLE official_site_agent_display ADD COLUMN media_type VARCHAR(10)")
                )

            if "icon_url" not in display_columns:
                conn.execute(
                    text("ALTER TABLE official_site_agent_display ADD COLUMN icon_url VARCHAR(500)")
                )

            if "speech_title" not in display_columns:
                conn.execute(
                    text("ALTER TABLE official_site_agent_display ADD COLUMN speech_title VARCHAR(100)")
                )

            if "speech_content" not in display_columns:
                conn.execute(
                    text("ALTER TABLE official_site_agent_display ADD COLUMN speech_content VARCHAR(200)")
                )