migrate_plugin_cols.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import asyncio
  2. from sqlalchemy import text
  3. from app.core.database import engine
  4. async def migrate():
  5. async with engine.begin() as conn:
  6. # Check existing columns
  7. res = await conn.execute(text("""
  8. SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
  9. WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'plugins';
  10. """))
  11. cols = [r[0] for r in res.fetchall()]
  12. print("Existing columns in plugins:", cols)
  13. if "class_name" not in cols:
  14. print("Adding class_name column...")
  15. await conn.execute(text("ALTER TABLE plugins ADD COLUMN class_name VARCHAR(128) NOT NULL DEFAULT '' COMMENT '插件入口类名' AFTER filename;"))
  16. print("Added class_name.")
  17. else:
  18. print("class_name column already exists.")
  19. if "protocol" not in cols:
  20. print("Adding protocol column...")
  21. await conn.execute(text("ALTER TABLE plugins ADD COLUMN protocol VARCHAR(64) NOT NULL DEFAULT 'IPlugin' COMMENT '插件协议标识' AFTER class_name;"))
  22. print("Added protocol.")
  23. else:
  24. print("protocol column already exists.")
  25. print("Migration finished successfully.")
  26. if __name__ == "__main__":
  27. asyncio.run(migrate())