"""Migrate data from JSON files to PostgreSQL.""" import asyncio import json from pathlib import Path from app.db.postgres import PostgresDB DB_DIR = Path(__file__).resolve().parent.parent.parent / "data" USERS_FILE = DB_DIR / "users.json" BOOKINGS_FILE = DB_DIR / "bookings.json" async def migrate(): print("Starting migration from JSON to PostgreSQL...") await PostgresDB.init_pool() # Migrate users if USERS_FILE.exists(): with open(USERS_FILE) as f: users = json.load(f) for uid, data in users.items(): name = data.get("name", "") phone = data.get("phone", "") await PostgresDB.set_user(int(uid), name, phone) print(f"Migrated {len(users)} users") # Migrate bookings if BOOKINGS_FILE.exists(): with open(BOOKINGS_FILE) as f: bookings = json.load(f) for b in bookings: # Re-create via raw INSERT to preserve IDs pool = await PostgresDB.get_pool() async with pool.acquire() as conn: await conn.execute(""" INSERT INTO bookings (id, user_id, user_name, service, master, date, time, status) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT (id) DO NOTHING """, b.get("id"), b.get("user_id"), b.get("user_name", ""), b.get("service"), b.get("master"), b.get("date"), b.get("time"), b.get("status", "active")) print(f"Migrated {len(bookings)} bookings") print("Migration complete!") await PostgresDB.close() if __name__ == "__main__": asyncio.run(migrate())