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.
This commit is contained in:
Ryan Roden-Corrent 2017-08-21 08:38:14 -04:00
parent 5f45b9b40e
commit d35b47c9d8
2 changed files with 25 additions and 0 deletions

View File

@ -30,6 +30,10 @@ from qutebrowser.utils import (utils, objreg, log, usertypes, message,
from qutebrowser.misc import objects, sql from qutebrowser.misc import objects, sql
# increment to indicate that HistoryCompletion must be regenerated
_USER_VERSION = 1
class CompletionHistory(sql.SqlTable): class CompletionHistory(sql.SqlTable):
"""History which only has the newest entry for each URL.""" """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'], super().__init__("History", ['url', 'title', 'atime', 'redirect'],
parent=parent) parent=parent)
self.completion = CompletionHistory(parent=self) self.completion = CompletionHistory(parent=self)
if sql.Query('pragma user_version').run().value() < _USER_VERSION:
self.completion.delete_all()
if not self.completion: if not self.completion:
# either the table is out-of-date or the user wiped it manually
self._rebuild_completion() self._rebuild_completion()
self.create_index('HistoryIndex', 'url') self.create_index('HistoryIndex', 'url')
self.create_index('HistoryAtimeIndex', 'atime') self.create_index('HistoryAtimeIndex', 'atime')
@ -83,6 +90,7 @@ class WebHistory(sql.SqlTable):
data['title'].append(entry.title) data['title'].append(entry.title)
data['last_atime'].append(entry.atime) data['last_atime'].append(entry.atime)
self.completion.insert_batch(data) self.completion.insert_batch(data)
sql.Query('pragma user_version = {}'.format(_USER_VERSION)).run()
def get_recent(self): def get_recent(self):
"""Get the most recent history entries.""" """Get the most recent history entries."""

View File

@ -377,3 +377,20 @@ def test_no_rebuild_completion(hist):
hist2 = history.WebHistory() hist2 = history.WebHistory()
assert list(hist2.completion) == [('example.com/1', '', 1)] 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),
]