From d35b47c9d8c139986834f2c6603c8d81fd7e00de Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Mon, 21 Aug 2017 08:38:14 -0400 Subject: [PATCH] Regenerate history completion on version change. Incrementing _USER_VERSION in the source will cause the HistoryCompletion table to regenerate when users update. This is currently necessary to support some recent formatting fixes, but could be incremented again in the future for other changes. --- qutebrowser/browser/history.py | 8 ++++++++ tests/unit/browser/test_history.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/qutebrowser/browser/history.py b/qutebrowser/browser/history.py index 0b3cf108e..9948992ea 100644 --- a/qutebrowser/browser/history.py +++ b/qutebrowser/browser/history.py @@ -30,6 +30,10 @@ from qutebrowser.utils import (utils, objreg, log, usertypes, message, from qutebrowser.misc import objects, sql +# increment to indicate that HistoryCompletion must be regenerated +_USER_VERSION = 1 + + class CompletionHistory(sql.SqlTable): """History which only has the newest entry for each URL.""" @@ -48,7 +52,10 @@ class WebHistory(sql.SqlTable): super().__init__("History", ['url', 'title', 'atime', 'redirect'], parent=parent) self.completion = CompletionHistory(parent=self) + if sql.Query('pragma user_version').run().value() < _USER_VERSION: + self.completion.delete_all() if not self.completion: + # either the table is out-of-date or the user wiped it manually self._rebuild_completion() self.create_index('HistoryIndex', 'url') self.create_index('HistoryAtimeIndex', 'atime') @@ -83,6 +90,7 @@ class WebHistory(sql.SqlTable): data['title'].append(entry.title) data['last_atime'].append(entry.atime) self.completion.insert_batch(data) + sql.Query('pragma user_version = {}'.format(_USER_VERSION)).run() def get_recent(self): """Get the most recent history entries.""" diff --git a/tests/unit/browser/test_history.py b/tests/unit/browser/test_history.py index 6fa160d31..b49a52a2a 100644 --- a/tests/unit/browser/test_history.py +++ b/tests/unit/browser/test_history.py @@ -377,3 +377,20 @@ def test_no_rebuild_completion(hist): hist2 = history.WebHistory() assert list(hist2.completion) == [('example.com/1', '', 1)] + + +def test_user_version(hist, monkeypatch): + """Ensure that completion is regenerated if user_version is incremented.""" + hist.add_url(QUrl('example.com/1'), redirect=False, atime=1) + hist.add_url(QUrl('example.com/2'), redirect=False, atime=2) + hist.completion.delete('url', 'example.com/2') + + hist2 = history.WebHistory() + assert list(hist2.completion) == [('example.com/1', '', 1)] + + monkeypatch.setattr(history, '_USER_VERSION', history._USER_VERSION + 1) + hist3 = history.WebHistory() + assert list(hist3.completion) == [ + ('example.com/1', '', 1), + ('example.com/2', '', 2), + ]