77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
"""One-time migration: copy data/economy.json → PocketBase.
|
|
|
|
Run this ONCE after setting up PocketBase and before starting the bot with
|
|
the new PocketBase backend.
|
|
|
|
Usage (run from project root):
|
|
python scripts/migrate_to_pb.py
|
|
|
|
Requirements:
|
|
- PocketBase must be running and reachable at PB_URL
|
|
- PB_ADMIN_EMAIL and PB_ADMIN_PASSWORD must be set in .env
|
|
- The 'economy_users' collection must already exist in PocketBase
|
|
(see README or PocketBase admin UI to create it)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
# Ensure the project root is on sys.path so pb_client can be imported
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
load_dotenv()
|
|
|
|
import pb_client # noqa: E402 (needs dotenv loaded first)
|
|
|
|
DATA_FILE = Path("data") / "economy.json"
|
|
|
|
|
|
async def main() -> None:
|
|
if not DATA_FILE.exists():
|
|
print(f"[ERROR] {DATA_FILE} not found - nothing to migrate.")
|
|
sys.exit(1)
|
|
|
|
raw: dict[str, dict] = json.loads(DATA_FILE.read_text(encoding="utf-8"))
|
|
total = len(raw)
|
|
print(f"Found {total} user(s) in {DATA_FILE}")
|
|
|
|
created = skipped = errors = 0
|
|
|
|
for uid, user in raw.items():
|
|
try:
|
|
record = dict(user)
|
|
record["user_id"] = uid
|
|
record.setdefault("balance", 0)
|
|
record.setdefault("exp", 0)
|
|
record.setdefault("items", [])
|
|
record.setdefault("item_uses", {})
|
|
record.setdefault("reminders", ["daily", "work", "beg", "crime", "rob"])
|
|
record.setdefault("eco_banned", False)
|
|
record.setdefault("daily_streak", 0)
|
|
|
|
existing = await pb_client.get_record(uid)
|
|
if existing:
|
|
await pb_client.update_record(existing["id"], record)
|
|
print(f" [UPDATE] {uid}")
|
|
skipped += 1 # reuse skipped counter as "updated"
|
|
else:
|
|
await pb_client.create_record(record)
|
|
print(f" [CREATE] {uid}")
|
|
created += 1
|
|
except Exception as exc:
|
|
print(f" [ERROR] {uid}: {exc}")
|
|
errors += 1
|
|
|
|
print(f"\nDone. Created: {created} Skipped: {skipped} Errors: {errors}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|