-- ============================================================
-- 租户库结构升级：主库表（ylrz_saas）
-- 执行一次即可；TenantUpgradeService 通过 MyBatis-Plus 读写
-- ============================================================

CREATE TABLE IF NOT EXISTS tenant_upgrade_task (
    id BIGINT NOT NULL AUTO_INCREMENT,
    task_no VARCHAR(64) NOT NULL,
    scope_type VARCHAR(16) NOT NULL,
    target_versions VARCHAR(512) NOT NULL,
    status VARCHAR(16) NOT NULL,
    total_tenants INT NOT NULL DEFAULT 0,
    success_tenants INT NOT NULL DEFAULT 0,
    failed_tenants INT NOT NULL DEFAULT 0,
    started_at DATETIME NOT NULL,
    finished_at DATETIME DEFAULT NULL,
    trigger_by VARCHAR(64) DEFAULT NULL,
    remark VARCHAR(500) DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uk_task_no (task_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租户库升级任务';

CREATE TABLE IF NOT EXISTS tenant_upgrade_task_detail (
    id BIGINT NOT NULL AUTO_INCREMENT,
    task_no VARCHAR(64) NOT NULL,
    tenant_id BIGINT NOT NULL,
    tenant_code VARCHAR(64) NOT NULL,
    current_version VARCHAR(64) DEFAULT NULL,
    target_version VARCHAR(64) DEFAULT NULL,
    status VARCHAR(16) NOT NULL,
    error_msg TEXT DEFAULT NULL,
    started_at DATETIME NOT NULL,
    finished_at DATETIME DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uk_task_tenant (task_no, tenant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租户库升级任务明细';
