Files
eve_structure/webapp/data/migrate.py
Oliver Walter 36f7f6d278 migrate to table based tinydb
NOTE to migrate database install tinydb & run migrate.py

- added new functions to filter data with db queries
- in the storage module
2025-08-27 20:39:04 +02:00

59 lines
1.5 KiB
Python

import os, json
from tinydb import TinyDB
from tinydb.storages import Storage
# Paths
OLD_FILE = "orders.json"
ARCHIVE_FILE = "orders_old.json"
NEW_FILE = "orders_new.json" # final TinyDB JSON file
TABLE_NAME = "orders"
class PrettyJSONStorage(Storage):
"""Custom TinyDB storage that pretty-prints JSON."""
def __init__(self, path):
self.path = path
def read(self):
try:
with open(self.path, "r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
return None
def write(self, data):
with open(self.path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def migrate():
if not os.path.isfile(OLD_FILE):
print("No old JSON file found. Nothing to migrate.")
return
# Load old orders
with open(OLD_FILE, "r", encoding="utf-8") as f:
orders = json.load(f) or []
if not orders:
print("No orders found in the old JSON.")
return
print(orders)
# Open TinyDB and insert into table
db = TinyDB(NEW_FILE, storage=PrettyJSONStorage)
orders_table = db.table(TABLE_NAME)
for order in orders:
orders_table.insert(order)
# Rename old file to archive
os.rename(OLD_FILE, ARCHIVE_FILE)
os.rename(NEW_FILE, OLD_FILE)
print(f"Migrated {len(orders)} orders into table '{TABLE_NAME}'.")
print(f"Old JSON file renamed to '{ARCHIVE_FILE}'.")
print(f"New TinyDB JSON file is '{OLD_FILE}'.")
if __name__ == "__main__":
migrate()