| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- """
- 数据库迁移:将 spider_result.link 从 VARCHAR(500) 改为 TEXT
- 解决百度搜索返回的长追踪链接(约 2200 字符)超出长度限制导致 500 错误
- """
- import os
- from dotenv import load_dotenv
- load_dotenv(os.path.join(os.path.dirname(__file__), '.env'))
- from app import create_app, db
- def migrate():
- app = create_app()
- with app.app_context():
- conn = db.engine.connect()
- # 检查当前 link 列类型
- result = conn.execute(
- db.text(
- "SELECT data_type, character_maximum_length "
- "FROM information_schema.columns "
- "WHERE table_name='spider_result' AND column_name='link'"
- )
- )
- row = result.fetchone()
- if not row:
- print("spider_result.link column not found, skipping")
- elif row[0] == 'text':
- print("spider_result.link is already TEXT type")
- else:
- print(f"Current type: {row[0]}({row[1]})")
- conn.execute(db.text("ALTER TABLE spider_result ALTER COLUMN link TYPE TEXT"))
- print("Migrated spider_result.link to TEXT")
- conn.commit()
- print("Migration complete.")
- if __name__ == "__main__":
- migrate()
|