| import os |
| import shutil |
| from pathlib import Path |
| import json |
|
|
| SOURCE_ROOT = Path("source") |
|
|
| def shard_numeric(id_val): |
| return f"{int(id_val) // 1000:03d}" |
|
|
| def shard_string(slug): |
| return slug[0].lower() if slug else "_" |
|
|
| CONFIG = { |
| "flights": shard_numeric, |
| "products": shard_numeric, |
| "reviews": shard_numeric, |
| "motors": shard_numeric, |
| "designs": shard_numeric, |
| "clubs": shard_numeric, |
| "parts": shard_string, |
| "glossary": shard_string, |
| "plans": shard_string, |
| } |
|
|
| def migrate(): |
| for entity, shard_fn in CONFIG.items(): |
| detail_dir = SOURCE_ROOT / entity / "detail" |
| if not detail_dir.exists(): |
| continue |
| |
| print(f"Migrating {entity}...") |
| |
| |
| files = [f for f in detail_dir.iterdir() if f.is_file() and f.suffix == ".json"] |
| |
| for f in files: |
| |
| name = f.stem |
| try: |
| |
| |
| shard = shard_fn(name) |
| shard_path = detail_dir / shard |
| shard_path.mkdir(parents=True, exist_ok=True) |
| |
| dest = shard_path / f.name |
| shutil.move(str(f), str(dest)) |
| except Exception as e: |
| print(f"Error moving {f}: {e}") |
|
|
| if __name__ == "__main__": |
| migrate() |
|
|