From 176da67d82b3425c66996bba36ef1c1bdea34b06 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 21 Jul 2013 12:40:53 +0530 Subject: [PATCH] Database restore now works with the new API --- src/calibre/db/__init__.py | 5 ++--- src/calibre/db/backend.py | 4 ++++ src/calibre/db/cache.py | 21 +++++++++++++++++++++ src/calibre/db/legacy.py | 2 +- src/calibre/gui2/dialogs/restore_library.py | 8 ++++++-- src/calibre/library/cli.py | 5 ++++- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/calibre/db/__init__.py b/src/calibre/db/__init__.py index 1fe7da1e04..c1b48cad0c 100644 --- a/src/calibre/db/__init__.py +++ b/src/calibre/db/__init__.py @@ -132,9 +132,8 @@ def get_db_loader(): ''' Various things that require other things before they can be migrated: - 1. Port library/restore.py - 2. Check that content server reloading on metadata,db change, metadata + 1. Check that content server reloading on metadata,db change, metadata backup, refresh gui on calibredb add and moving libraries all work (check them on windows as well for file locking issues) - 3. Check for mem leaks when switching libraries + 2. Check for mem leaks when switching libraries ''' diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index f203f5ed0c..9eb49ccd4c 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -1525,6 +1525,10 @@ class DB(object): except: pass + def restore_book(self, book_id, path, formats): + self.conn.execute('UPDATE books SET path=? WHERE id=?', (path.replace(os.sep, '/'), book_id)) + vals = [(book_id, fmt, size, name) for fmt, size, name in formats] + self.conn.executemany('INSERT INTO data (book,format,uncompressed_size,name) VALUES (?,?,?,?)', vals) # }}} diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 37d0428046..16e6d59adb 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -423,6 +423,12 @@ class Cache(object): rmap = {icu_lower(v) if isinstance(v, unicode) else v:k for k, v in self.fields[field].table.id_map.iteritems()} return rmap.get(icu_lower(item_name) if isinstance(item_name, unicode) else item_name, None) + @read_api + def get_item_ids(self, field, item_names): + ' Return the item id for item_name (case-insensitive) ' + rmap = {icu_lower(v) if isinstance(v, unicode) else v:k for k, v in self.fields[field].table.id_map.iteritems()} + return {name:rmap.get(icu_lower(name) if isinstance(name, unicode) else name, None) for name in item_names} + @read_api def author_data(self, author_ids=None): ''' @@ -1568,6 +1574,21 @@ class Cache(object): def dump_and_restore(self, callback=None, sql=None): return self.backend.dump_and_restore(callback=callback, sql=sql) + @write_api + def close(self): + self.backend.close() + + @write_api + def restore_book(self, book_id, mi, last_modified, path, formats): + ''' Restore the book entry in the database for a book that already exists on the filesystem ''' + cover = mi.cover + mi.cover = None + self._create_book_entry(mi, add_duplicates=True, + force_id=book_id, apply_import_tags=False, preserve_uuid=True) + self._update_last_modified((book_id,), last_modified) + if cover and os.path.exists(cover): + self._set_field('cover', {book_id:1}) + self.backend.restore_book(book_id, path, formats) # }}} class SortKey(object): # {{{ diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index 5d41986fce..94c35429b2 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -78,7 +78,7 @@ class LibraryDatabase(object): set_saved_searches(self, 'saved_searches') def close(self): - self.backend.close() + self.new_api.close() def break_cycles(self): delattr(self.backend, 'field_metadata') diff --git a/src/calibre/gui2/dialogs/restore_library.py b/src/calibre/gui2/dialogs/restore_library.py index a460460120..58731fa2d0 100644 --- a/src/calibre/gui2/dialogs/restore_library.py +++ b/src/calibre/gui2/dialogs/restore_library.py @@ -8,11 +8,11 @@ __docformat__ = 'restructuredtext en' from PyQt4.Qt import (QDialog, QLabel, QVBoxLayout, QDialogButtonBox, QProgressBar, QSize, QTimer, pyqtSignal, Qt) -from calibre.library.restore import Restore from calibre.gui2 import (error_dialog, question_dialog, warning_dialog, info_dialog) from calibre import force_unicode from calibre.constants import filesystem_encoding +from calibre.utils.config_base import tweaks class DBRestore(QDialog): @@ -42,13 +42,16 @@ class DBRestore(QDialog): self.library_path = library_path self.update_signal.connect(self.do_update, type=Qt.QueuedConnection) + if tweaks.get('use_new_db', False): + from calibre.db.restore import Restore + else: + from calibre.library.restore import Restore self.restorer = Restore(library_path, self) self.restorer.daemon = True # Give the metadata backup thread time to stop QTimer.singleShot(2000, self.start) - def start(self): self.restorer.start() QTimer.singleShot(10, self.update) @@ -133,3 +136,4 @@ def repair_library_at(library_path, parent=None): return True + diff --git a/src/calibre/library/cli.py b/src/calibre/library/cli.py index 2c6e7cd777..5a888f672e 100644 --- a/src/calibre/library/cli.py +++ b/src/calibre/library/cli.py @@ -1214,7 +1214,6 @@ what is found in the OPF files. return parser def command_restore_database(args, dbpath): - from calibre.library.restore import Restore parser = restore_database_option_parser() opts, args = parser.parse_args(args) if len(args) != 0: @@ -1242,6 +1241,10 @@ def command_restore_database(args, dbpath): self.total = float(step) else: prints(msg, '...', '%d%%'%int(100*(step/self.total))) + if tweaks.get('use_new_db', False): + from calibre.db.restore import Restore + else: + from calibre.library.restore import Restore r = Restore(dbpath, progress_callback=Progress()) r.start() r.join()