-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_components_inventory.py
More file actions
36 lines (26 loc) · 1.1 KB
/
migrate_components_inventory.py
File metadata and controls
36 lines (26 loc) · 1.1 KB
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
import asyncio
import json
from utils.database.components_inventory_db import ComponentsInventoryDB
async def migrate_components_inventory_from_json_file(file_path: str):
db = ComponentsInventoryDB()
await db.connect()
try:
# Load data
with open(file_path, "r", encoding="utf-8") as f:
data = json.load(f)
components = data.get("components", [])
if not components:
print("No components found in the data.")
return
print(f"Found {len(components)} components. Inserting into database...")
for idx, component in enumerate(components, start=1):
component["id"] = idx
component["name"] = component["part_number"]
component_id = await db.add_component(component)
print(f"[{idx}] Inserted component ID: {component_id} - Name: {component['name']}")
except Exception as e:
print(f"Error during migration: {e}")
finally:
await db.close()
if __name__ == "__main__":
asyncio.run(migrate_components_inventory_from_json_file("data/components_inventory.json"))