From 0981d8d0dec058e78e4421d71a0f5930d223c7de Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 6 Feb 2021 19:01:48 +1000 Subject: [PATCH] fix backup not being taken before full download --- qt/aqt/main.py | 13 ++++++++++++- qt/aqt/sync.py | 12 +++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 50bb61144..539b13e8e 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -557,6 +557,13 @@ class AnkiQt(QMainWindow): if not corrupt and not self.restoringBackup: self.backup() + def _close_for_full_download(self) -> None: + "Backup and prepare collection to be overwritten." + self.col.close(downgrade=False) + self.backup() + self.col.reopen(after_full_sync=False) + self.col.close_for_full_sync() + # Backup and auto-optimize ########################################################################## @@ -577,6 +584,9 @@ class AnkiQt(QMainWindow): z.close() def backup(self) -> None: + "Read data into memory, and complete backup on a background thread." + assert not self.col or not self.col.db + nbacks = self.pm.profile["numBackups"] if not nbacks or devMode: return @@ -607,7 +617,8 @@ class AnkiQt(QMainWindow): fname = backups.pop(0) path = os.path.join(dir, fname) os.unlink(path) - gui_hooks.backup_did_complete() + + self.taskman.run_on_main(gui_hooks.backup_did_complete) def maybeOptimize(self) -> None: # have two weeks passed? diff --git a/qt/aqt/sync.py b/qt/aqt/sync.py index 7c8e2f283..8b19d54f0 100644 --- a/qt/aqt/sync.py +++ b/qt/aqt/sync.py @@ -172,16 +172,18 @@ def on_full_sync_timer(mw: aqt.main.AnkiQt) -> None: def full_download(mw: aqt.main.AnkiQt, on_done: Callable[[], None]) -> None: - mw.col.close_for_full_sync() - - def on_timer(): + def on_timer() -> None: on_full_sync_timer(mw) timer = QTimer(mw) qconnect(timer.timeout, on_timer) timer.start(150) - def on_future_done(fut): + def download() -> None: + mw._close_for_full_download() + mw.col.backend.full_download(mw.pm.sync_auth()) + + def on_future_done(fut) -> None: timer.stop() mw.col.reopen(after_full_sync=True) mw.reset() @@ -193,7 +195,7 @@ def full_download(mw: aqt.main.AnkiQt, on_done: Callable[[], None]) -> None: return on_done() mw.taskman.with_progress( - lambda: mw.col.backend.full_download(mw.pm.sync_auth()), + download, on_future_done, label=tr(TR.SYNC_DOWNLOADING_FROM_ANKIWEB), )