async def get_cards_list() -> list[dict[str, Any]]: logger.info("Getting all cards") stmt = sa.select([models.Cards, models.Notes, models.Materials.c.title]) \ .join(models.Notes, models.Cards.c.note_id == models.Notes.c.note_id)\ .join(models.Materials, models.Notes.c.material_id == models.Materials.c.material_id) async with database.session() as ses: return [{ "card": { "card_id": row.card_id, "question": row.question, "answer": row.answer, "added_at": row.added_at }, "note": { "note_id": row.note_id, "material_title": row.title, "content": row.content, "page": row.page, "chapter": row.chapter } } async for row in await ses.stream(stmt)]
async def get_materials() -> list[RowMapping]: logger.info("Getting all materials") stmt = sa.select(models.Materials) async with database.session() as ses: return (await ses.execute(stmt)).all()
async def notes_with_cards() -> list[UUID]: logger.info("Getting notes with a card") stmt = sa.select(models.Notes.c.note_id)\ .join(models.Cards, models.Cards.c.note_id == models.Notes.c.note_id) async with database.session() as ses: return (await ses.execute(stmt)).all()
async def backup() -> None: logger.info("Backuping started") start_time = time.perf_counter() db_snapshot = await _get_db_snapshot() dump_file = _dump_snapshot(db_snapshot) _send_dump(dump_file) _remove_file(dump_file) logger.info("Backuping completed, %ss", round(time.perf_counter() - start_time, 2))
async def get_title(*, material_id: UUID) -> str: logger.info("Getting title for material_id=%s", material_id) if material := await get_material(material_id=material_id): return material.title
async def restore() -> None: logger.info("Restoring started") start_time = time.perf_counter() if not (dump_file_id := _get_last_dump()): raise ValueError("Dump not found")
logger.debug("Data into %s inserted", table.name) async def restore() -> None: logger.info("Restoring started") start_time = time.perf_counter() if not (dump_file_id := _get_last_dump()): raise ValueError("Dump not found") dump_file = _download_file(dump_file_id) await _recreate_db() await _restore_db(dump_file) _remove_file(dump_file) logger.info("Restoring completed, %ss", round(time.perf_counter() - start_time, 2)) async def main() -> None: parser = argparse.ArgumentParser(description="Backup/restore the database") parser.add_argument('--backup', help="Create and send a backup to the Google Drive", action="store_true", dest="backup") parser.add_argument( '--restore', help= "Downloand the last backup from the Google Drive and restore the datbase", action="store_true", dest="restore") args = parser.parse_args()