forked from sass/tipibot
85 lines
2.8 KiB
Python
85 lines
2.8 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 core modules can be imported
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
load_dotenv()
|
|
|
|
from core 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 = updated = errors = 0
|
|
|
|
for uid, user in raw.items():
|
|
try:
|
|
existing = await pb_client.get_record(uid)
|
|
if existing:
|
|
# Merge JSON fields *onto* the existing record so values that have
|
|
# accumulated in PB (items, daily_streak, reminders, etc.) are not
|
|
# clobbered by JSON defaults on a re-run. JSON values take
|
|
# precedence only for keys that are actually present.
|
|
merged: dict = {k: v for k, v in existing.items() if not k.startswith("_") and k != "id"}
|
|
merged.update(user)
|
|
merged["user_id"] = uid
|
|
await pb_client.update_record(existing["id"], merged)
|
|
print(f" [UPDATE] {uid}")
|
|
updated += 1
|
|
else:
|
|
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)
|
|
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} Updated: {updated} Errors: {errors}")
|
|
if errors:
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|