File size: 1,480 Bytes
25b0ae9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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}...")
        
        # Iterate over files in the detail directory
        files = [f for f in detail_dir.iterdir() if f.is_file() and f.suffix == ".json"]
        
        for f in files:
            # Extract ID/slug from filename
            name = f.stem
            try:
                # Most numeric ones are 000123.json
                # Most string ones are slug.json
                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()