From c238367b64da5af4fb194b4d7644b88443cbfbb8 Mon Sep 17 00:00:00 2001 From: Michael Shavit Date: Sun, 22 Dec 2019 12:58:03 -0500 Subject: [PATCH] Reload database on every call to v1/library/sync. This fixes an issue where side-loaded books appear in the sync response with no download urls. --- cps/db.py | 5 +++++ cps/kobo.py | 4 ++++ cps/web.py | 4 +--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cps/db.py b/cps/db.py index d4b4a7ed..49c44701 100755 --- a/cps/db.py +++ b/cps/db.py @@ -429,3 +429,8 @@ def dispose(): if name.startswith("custom_column_") or name.startswith("books_custom_column_"): if table is not None: Base.metadata.remove(table) + +def reconnect_db(config): + session.close() + engine.dispose() + setup_db(config) \ No newline at end of file diff --git a/cps/kobo.py b/cps/kobo.py index d11ab8e5..840e9e51 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -227,6 +227,10 @@ def HandleSyncRequest(): new_books_last_created = sync_token.books_last_created entitlements = [] + # We reload the book database so that the user get's a fresh view of the library + # in case of external changes (e.g: adding a book through Calibre). + db.reconnect_db(config) + # sqlite gives unexpected results when performing the last_modified comparison without the datetime cast. # It looks like it's treating the db.Books.last_modified field as a string and may fail # the comparison because of the +00:00 suffix. diff --git a/cps/web.py b/cps/web.py index 019887c7..ed01991e 100644 --- a/cps/web.py +++ b/cps/web.py @@ -784,9 +784,7 @@ def get_tasks_status(): @app.route("/reconnect") def reconnect(): - db.session.close() - db.engine.dispose() - db.setup_db(config) + db.reconnect_db(config) return json.dumps({}) @web.route("/search", methods=["GET"])