Regenerate history completion table if needed.

If the HistoryCompletion table is removed, regenerate it from the
History table. This allows users to manually edit History, then remove
HistoryCompletion to prompt regeneration.

See #2903.
This commit is contained in:
Ryan Roden-Corrent 2017-08-17 07:57:14 -04:00
parent c607537319
commit 8c6133e29d
2 changed files with 27 additions and 0 deletions

View File

@ -48,6 +48,8 @@ class WebHistory(sql.SqlTable):
super().__init__("History", ['url', 'title', 'atime', 'redirect'],
parent=parent)
self.completion = CompletionHistory(parent=self)
if len(self.completion) == 0:
self._rebuild_completion()
self.create_index('HistoryIndex', 'url')
self.create_index('HistoryAtimeIndex', 'atime')
self._contains_query = self.contains_query('url')
@ -71,6 +73,17 @@ class WebHistory(sql.SqlTable):
def __contains__(self, url):
return self._contains_query.run(val=url).value()
def _rebuild_completion(self):
data = {'url': [], 'title': [], 'last_atime': []}
# select the latest entry for each url
q = sql.Query('SELECT url, title, max(atime) AS atime FROM History '
'WHERE NOT redirect GROUP BY url')
for entry in q.run():
data['url'].append(self._format_completion_url(QUrl(entry.url)))
data['title'].append(entry.title)
data['last_atime'].append(entry.atime)
self.completion.insert_batch(data)
def get_recent(self):
"""Get the most recent history entries."""
return self.select(sort_by='atime', sort_order='desc', limit=100)

View File

@ -353,3 +353,17 @@ def test_debug_dump_history_nonexistent(hist, tmpdir):
histfile = tmpdir / 'nonexistent' / 'history'
with pytest.raises(cmdexc.CommandError):
hist.debug_dump_history(str(histfile))
def test_rebuild_completion(hist):
hist.add_url(QUrl('example.com/1'), redirect=False, atime=1)
hist.add_url(QUrl('example.com/1'), redirect=False, atime=2)
hist.add_url(QUrl('example.com/2%203'), redirect=False, atime=3)
hist.add_url(QUrl('example.com/3'), redirect=True, atime=4)
hist.completion.delete_all()
hist2 = history.WebHistory()
assert list(hist2.completion) == [
('example.com/1', '', 2),
('example.com/2 3', '', 3),
]