Files
yunke/yunke-server/scripts/fix_media_paths.py
T

42 lines
1.2 KiB
Python
Raw Normal View History

"""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())