diff --git a/yunke-server/scripts/fix_media_paths.py b/yunke-server/scripts/fix_media_paths.py new file mode 100644 index 0000000..245575e --- /dev/null +++ b/yunke-server/scripts/fix_media_paths.py @@ -0,0 +1,41 @@ +"""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())