From b89caf04580ffccefcc1f1482f3f9c7f8e7d86f2 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Tue, 22 Aug 2017 07:19:04 -0400 Subject: [PATCH] Use REPLACE when rebuilding completion table. When upgrading from an old table that used different url formatting, two entries might map to the same key, so we'll need to replace the previous entry to avoid a primary key conflict. --- qutebrowser/browser/history.py | 4 ++-- tests/unit/browser/test_history.py | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/qutebrowser/browser/history.py b/qutebrowser/browser/history.py index 9948992ea..2d5c263bc 100644 --- a/qutebrowser/browser/history.py +++ b/qutebrowser/browser/history.py @@ -84,12 +84,12 @@ class WebHistory(sql.SqlTable): 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') + 'WHERE NOT redirect GROUP BY url ORDER BY atime asc') 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) + self.completion.insert_batch(data, replace=True) sql.Query('pragma user_version = {}'.format(_USER_VERSION)).run() def get_recent(self): diff --git a/tests/unit/browser/test_history.py b/tests/unit/browser/test_history.py index b49a52a2a..2462a1141 100644 --- a/tests/unit/browser/test_history.py +++ b/tests/unit/browser/test_history.py @@ -356,16 +356,22 @@ def test_debug_dump_history_nonexistent(hist, tmpdir): 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.insert({'url': 'example.com/1', 'title': 'example1', + 'redirect': False, 'atime': 1}) + hist.insert({'url': 'example.com/1', 'title': 'example1', + 'redirect': False, 'atime': 2}) + hist.insert({'url': 'example.com/2%203', 'title': 'example2', + 'redirect': False, 'atime': 3}) + hist.insert({'url': 'example.com/3', 'title': 'example3', + 'redirect': True, 'atime': 4}) + hist.insert({'url': 'example.com/2 3', 'title': 'example2', + 'redirect': False, 'atime': 5}) hist.completion.delete_all() hist2 = history.WebHistory() assert list(hist2.completion) == [ - ('example.com/1', '', 2), - ('example.com/2 3', '', 3), + ('example.com/1', 'example1', 2), + ('example.com/2 3', 'example2', 5), ]