Skip to content

Commit

Permalink
Fix update_search() not working if search database is missing. #362
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Nov 20, 2024
1 parent 0ca7a23 commit c08dbd7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Version 3.16

Unreleased

* Fix :meth:`~Reader.enable_search` / :meth:`~Reader.update_search`
not working when the search database is missing but change tracking is enabled
(e.g. when restoring the main database from backup).
(:issue:`362`)


Version 3.15
------------
Expand Down
16 changes: 9 additions & 7 deletions src/reader/_storage/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,18 @@ def strip_html(text: SQLiteType) -> SQLiteType:

@wrap_exceptions()
def enable(self) -> None:
self.storage.changes.enable()
try:
with ddl_transaction(self.get_db()) as db:
self._enable(db, self.schema)
except sqlite3.OperationalError as e:
if "table entries_search already exists" in str(e).lower():
return
else: # pragma: no cover
if "table entries_search already exists" not in str(e): # pragma: no cover
raise
else:
# if search was not already enabled, make sure changes.enable()
# is not a no-op and we backfill all the resources
# (changes can be enabled and search not when restoring from backup)
self.storage.changes.disable()
self.storage.changes.enable()

@classmethod
def _enable(cls, db: sqlite3.Connection, schema: str = 'main') -> None:
Expand Down Expand Up @@ -217,10 +220,9 @@ def _is_enabled(cls, db: sqlite3.Connection) -> bool:
try:
cls._enabled_check(db)
except sqlite3.OperationalError as e:
if "no such table: entries_search" in str(e):
return False
else: # pragma: no cover
if "no such table: entries_search" not in str(e): # pragma: no cover
raise
return False
else:
return True

Expand Down
29 changes: 29 additions & 0 deletions tests/test_reader_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,35 @@ def done(changes):
assert set(changes) == done_changes


@pytest.mark.parametrize('enable_before_update', [False, True])
def test_search_database_is_missing(db_path, make_reader, enable_before_update):
"""update_search() should work after search database goes missing
(can happen when restoring from backup).
https://github.com/lemon24/reader/issues/362
"""
reader = make_reader(db_path)
reader._parser = parser = Parser()

reader.add_feed(parser.feed(1))
parser.entry(1, '1', title='one')

reader.update_feeds()
reader.update_search()

assert [e.resource_id for e in reader.search_entries('one')] == [('1', '1')]

reader.close()
os.remove(db_path + '.search')

if enable_before_update:
reader.enable_search()
reader.update_search()

assert [e.resource_id for e in reader.search_entries('one')] == [('1', '1')]


def test_update_actually_deletes_from_database(reader):
# TODO: this is implementation-dependent, move to test_search.py?
reader._parser = parser = Parser()
Expand Down

0 comments on commit c08dbd7

Please sign in to comment.