Бот для капчи в чатах
fix db: миграция старых таблиц chat_settings/user_violations при обновлении
1 files modified
17 ■■■■■ changed files
main.py 17 ●●●●● patch | view | raw | blame | history
main.py
@@ -117,11 +117,28 @@
        await conn.close()
async def tbl_exists(db: aiosqlite.Connection, name: str) -> bool:
    async with db.execute(
        "SELECT 1 FROM sqlite_master WHERE type='table' AND name=?", (name,)
    ) as cur:
        return await cur.fetchone() is not None
async def migrate_db(db: aiosqlite.Connection) -> None:
    # Обратная совместимость: таблицы переименованы при рефакторинге.
    # Старые имена переименовываются в новые, данные при этом сохраняются.
    if await tbl_exists(db, "chat_settings") and not await tbl_exists(db, "chat_cfg"):
        await db.execute("ALTER TABLE chat_settings RENAME TO chat_cfg")
    if await tbl_exists(db, "user_violations") and not await tbl_exists(db, "usr_fails"):
        await db.execute("ALTER TABLE user_violations RENAME TO usr_fails")
async def init_db() -> None:
    # Создание каталога обязательно: без него sqlite не откроет файл БД.
    os.makedirs(DB_DIR, exist_ok=True)
    async with aiosqlite.connect(DB_PATH) as db:
        await db.execute("PRAGMA journal_mode=WAL")
        await migrate_db(db)
        await db.execute(SQL_CREATE_CFG)
        await db.execute(SQL_CREATE_FAILS)
        await db.commit()