refactor(init): 移除 _init_v2_tables 启动期建表/加列,schema 变更统一走部署期 create_tables.py

This commit is contained in:
ymq 2026-08-18 16:02:19 +08:00
parent 5d40cdcd16
commit 31ba001482

View File

@ -787,75 +787,8 @@ def load_pipeline_service():
add_startup(_failed_poller)
# v2 DDL auto-init: create tables and columns at startup
async def _init_v2_tables(app):
from sqlor.dbpools import DBPools as _DBP2
from appPublic.log import debug as _debug
try:
_db = _DBP2()
async with _db.sqlorContext("pipeline") as sor:
await sor.sqlExe(
"CREATE TABLE IF NOT EXISTS pipeline_user_memory ("
"id varchar(32) NOT NULL, memory_key varchar(64) NOT NULL,"
"content text NOT NULL, category varchar(32) NOT NULL DEFAULT 'memory',"
"priority int NOT NULL DEFAULT 0, access_count int NOT NULL DEFAULT 0,"
"created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,"
"updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,"
"PRIMARY KEY (id), UNIQUE KEY uk_memory (memory_key, category),"
"KEY idx_category_priority (category, priority)"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", {})
_debug("pipeline_user_memory table ready")
# 应用部署主机逻辑账号表
await sor.sqlExe(
"CREATE TABLE IF NOT EXISTS sd_deploy_accounts ("
"id varchar(32) NOT NULL, user_id varchar(32) NOT NULL,"
"account_name varchar(64) NOT NULL, deploy_dir varchar(500) NOT NULL,"
"status varchar(20) NOT NULL DEFAULT 'active',"
"sandbox_config text,"
"created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,"
"updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,"
"PRIMARY KEY (id), UNIQUE KEY uk_account_name (account_name),"
"UNIQUE KEY uk_user_id (user_id), KEY idx_status (status)"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", {})
_debug("sd_deploy_accounts table ready")
# 工作环境表(沙箱本地/远程切换)
await sor.sqlExe(
"CREATE TABLE IF NOT EXISTS sd_work_envs ("
"id varchar(32) NOT NULL, owner_type varchar(10) NOT NULL,"
"owner_id varchar(32) NOT NULL, mode varchar(10) NOT NULL DEFAULT 'local',"
"remote_host varchar(200), remote_port int DEFAULT 22,"
"remote_user varchar(100), remote_key_path varchar(500), remote_dir varchar(500),"
"created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,"
"updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,"
"PRIMARY KEY (id), UNIQUE KEY uk_owner (owner_type, owner_id)"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", {})
_debug("sd_work_envs table ready")
# agent_config columns
try:
await sor.sqlExe("ALTER TABLE pipelines ADD COLUMN agent_config text", {})
_debug("pipelines.agent_config column added")
except Exception:
pass
try:
await sor.sqlExe("ALTER TABLE sd_org_settings ADD COLUMN agent_config text", {})
_debug("sd_org_settings.agent_config column added")
except Exception:
pass
# failed 任务重试计数 + 失败原因
try:
await sor.sqlExe("ALTER TABLE pipeline_tasks ADD COLUMN retry_count int NOT NULL DEFAULT 0", {})
_debug("pipeline_tasks.retry_count column added")
except Exception:
pass
try:
await sor.sqlExe("ALTER TABLE pipeline_tasks ADD COLUMN last_error text", {})
_debug("pipeline_tasks.last_error column added")
except Exception:
pass
except Exception as e:
_debug(f"v2 DDL init: {e}")
add_startup(_init_v2_tables)
# 铁律:运行期不做任何 schema 变更。v2 引擎的建表/加列已全部迁到
# 部署期 scripts/create_tables.pybuild.sh 会调用),此处不再启动时建表。
debug(f"[{MODULE_NAME}] v{MODULE_VERSION} loaded — pipeline engine with role-agent + question loop")
return True