a
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
#!/usr/bin/env python3
|
||||
"""迁移 home_modules 表中的图片路径"""
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
from sqlalchemy import create_engine, text
|
||||
from app.config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
engine = create_engine(settings.DATABASE_URL.replace('+aiosqlite', ''), echo=False)
|
||||
|
||||
|
||||
def update_banner_paths():
|
||||
"""更新 banner 模块中的图片路径"""
|
||||
with engine.connect() as conn:
|
||||
result = conn.execute(text('SELECT id, code, content FROM home_modules WHERE code = "banner"'))
|
||||
row = result.fetchone()
|
||||
|
||||
if not row:
|
||||
print("未找到 banner 模块")
|
||||
return
|
||||
|
||||
module_id = row[0]
|
||||
code = row[1]
|
||||
content = json.loads(row[2]) if row[2] else {}
|
||||
|
||||
slides = content.get('slides', [])
|
||||
updated = False
|
||||
|
||||
for slide in slides:
|
||||
old_image = slide.get('image', '')
|
||||
if old_image.startswith('/uploads/'):
|
||||
# 转换路径
|
||||
new_image = old_image.replace('/uploads/', '/storage/admin/images/')
|
||||
slide['image'] = new_image
|
||||
print(f" 更新: {old_image} -> {new_image}")
|
||||
updated = True
|
||||
|
||||
if updated:
|
||||
new_content = json.dumps(content, ensure_ascii=False)
|
||||
conn.execute(
|
||||
text('UPDATE home_modules SET content = :content WHERE id = :id'),
|
||||
{'content': new_content, 'id': module_id}
|
||||
)
|
||||
conn.commit()
|
||||
print(f"\n✅ banner 模块已更新")
|
||||
else:
|
||||
print("✅ banner 模块无需更新")
|
||||
|
||||
|
||||
def update_features_paths():
|
||||
"""更新 features 模块中的图片路径"""
|
||||
with engine.connect() as conn:
|
||||
result = conn.execute(text('SELECT id, code, content FROM home_modules WHERE code = "features"'))
|
||||
row = result.fetchone()
|
||||
|
||||
if not row:
|
||||
print("未找到 features 模块")
|
||||
return
|
||||
|
||||
module_id = row[0]
|
||||
content = json.loads(row[2]) if row[2] else {}
|
||||
|
||||
items = content.get('items', [])
|
||||
updated = False
|
||||
|
||||
for item in items:
|
||||
# 更新 icon 字段
|
||||
old_icon = item.get('icon', '')
|
||||
if old_icon.startswith('/uploads/') or old_icon.startswith('/images/'):
|
||||
if old_icon.startswith('/uploads/'):
|
||||
new_icon = old_icon.replace('/uploads/', '/storage/admin/images/')
|
||||
else:
|
||||
new_icon = old_icon.replace('/images/', '/storage/admin/images/')
|
||||
item['icon'] = new_icon
|
||||
print(f" 更新 icon: {old_icon} -> {new_icon}")
|
||||
updated = True
|
||||
|
||||
# 更新 media_url 字段
|
||||
old_media = item.get('media_url', '')
|
||||
if old_media.startswith('/uploads/') or old_media.startswith('/images/'):
|
||||
if old_media.startswith('/uploads/'):
|
||||
new_media = old_media.replace('/uploads/', '/storage/admin/images/')
|
||||
else:
|
||||
new_media = old_media.replace('/images/', '/storage/admin/images/')
|
||||
item['media_url'] = new_media
|
||||
print(f" 更新 media_url: {old_media} -> {new_media}")
|
||||
updated = True
|
||||
|
||||
if updated:
|
||||
new_content = json.dumps(content, ensure_ascii=False)
|
||||
conn.execute(
|
||||
text('UPDATE home_modules SET content = :content WHERE id = :id'),
|
||||
{'content': new_content, 'id': module_id}
|
||||
)
|
||||
conn.commit()
|
||||
print(f"\n✅ features 模块已更新")
|
||||
else:
|
||||
print("✅ features 模块无需更新")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("=" * 60)
|
||||
print("迁移 home_modules 图片路径")
|
||||
print("=" * 60)
|
||||
|
||||
update_banner_paths()
|
||||
update_features_paths()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("迁移完成")
|
||||
print("=" * 60)
|
||||
Reference in New Issue
Block a user