Files
yunke/yunke-server/scripts/fix_media_paths.py
T
浅念 31d22ddd45 fix(media): 修复 Windows 反斜杠路径致 Linux 容器 404(49536 行 local_path 全量改为 /data/media 前缀)
SQLite 迁移时保留了 Windows 路径(media\xx\),Linux 后端 Path.resolve()
失败 → 404。一次性修复全部 media 行 + API 恢复 200。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-24 15:16:18 +08:00

42 lines
1.2 KiB
Python

"""Fix media table local_path from Windows backslash to Linux forward-slash."""
import asyncio
from sqlalchemy import text
async def main():
from app.db.base import async_engine, async_session_maker
# 先看当前状态
async with async_engine.connect() as conn:
sample = (await conn.execute(
text("SELECT local_path FROM media LIMIT 3")
)).fetchall()
for r in sample:
print(f"before: {r[0][:50]}")
# 批量修复: media\xx\hash.ext → /data/media/xx/hash.ext
async with async_session_maker() as session:
result = await session.execute(text(
"UPDATE media SET local_path = "
"CONCAT('/data/media/', "
" SUBSTRING_INDEX(REPLACE(local_path, CHAR(92), '/'), '/', -2)) "
"WHERE local_path LIKE 'media%'"
))
print(f"updated: {result.rowcount} rows")
await session.commit()
# 验证
async with async_engine.connect() as conn:
sample = (await conn.execute(
text("SELECT local_path FROM media LIMIT 3")
)).fetchall()
for r in sample:
print(f"after: {r[0][:50]}")
await async_engine.dispose()
if __name__ == "__main__":
asyncio.run(main())