migrate_link_to_text.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """
  2. 数据库迁移:将 spider_result.link 从 VARCHAR(500) 改为 TEXT
  3. 解决百度搜索返回的长追踪链接(约 2200 字符)超出长度限制导致 500 错误
  4. """
  5. import os
  6. from dotenv import load_dotenv
  7. load_dotenv(os.path.join(os.path.dirname(__file__), '.env'))
  8. from app import create_app, db
  9. def migrate():
  10. app = create_app()
  11. with app.app_context():
  12. conn = db.engine.connect()
  13. # 检查当前 link 列类型
  14. result = conn.execute(
  15. db.text(
  16. "SELECT data_type, character_maximum_length "
  17. "FROM information_schema.columns "
  18. "WHERE table_name='spider_result' AND column_name='link'"
  19. )
  20. )
  21. row = result.fetchone()
  22. if not row:
  23. print("spider_result.link column not found, skipping")
  24. elif row[0] == 'text':
  25. print("spider_result.link is already TEXT type")
  26. else:
  27. print(f"Current type: {row[0]}({row[1]})")
  28. conn.execute(db.text("ALTER TABLE spider_result ALTER COLUMN link TYPE TEXT"))
  29. print("Migrated spider_result.link to TEXT")
  30. conn.commit()
  31. print("Migration complete.")
  32. if __name__ == "__main__":
  33. migrate()