import asyncio from sqlalchemy import text from app.core.database import engine async def migrate(): async with engine.begin() as conn: # Check existing columns res = await conn.execute(text(""" SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'plugins'; """)) cols = [r[0] for r in res.fetchall()] print("Existing columns in plugins:", cols) if "class_name" not in cols: print("Adding class_name column...") await conn.execute(text("ALTER TABLE plugins ADD COLUMN class_name VARCHAR(128) NOT NULL DEFAULT '' COMMENT '插件入口类名' AFTER filename;")) print("Added class_name.") else: print("class_name column already exists.") if "protocol" not in cols: print("Adding protocol column...") await conn.execute(text("ALTER TABLE plugins ADD COLUMN protocol VARCHAR(64) NOT NULL DEFAULT 'IPlugin' COMMENT '插件协议标识' AFTER class_name;")) print("Added protocol.") else: print("protocol column already exists.") print("Migration finished successfully.") if __name__ == "__main__": asyncio.run(migrate())