from os import rename from pathlib import Path from typing import Mapping from libbot.sync import json_read from modules.database_sqlite 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"))