This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
BWTAqua/modules/migrator.py

27 lines
792 B
Python

from os import rename
from pathlib import Path
from typing import Mapping
from libbot.sync import json_read
from modules.database import cursor
def migrate_database() -> None:
"""Apply migrations from old JSON database to SQLite"""
if not Path("data/database.json").exists():
return
db_old: Mapping[str, Mapping[str, str]] = json_read(Path("data/database.json"))
for user, keys in db_old.items():
user_locale = None if "locale" not in keys else keys["locale"]
user_card = None if "card" not in keys else keys["card"]
cursor.execute(
"INSERT INTO users VALUES (?, ?, ?)", (int(user), user_card, user_locale)
)
cursor.connection.commit()
rename(Path("data/database.json"), Path("data/database.migrated.json"))