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.
This commit is contained in:
Ryan Roden-Corrent 2017-08-22 07:19:04 -04:00
parent d35b47c9d8
commit b89caf0458
2 changed files with 14 additions and 8 deletions

View File

@ -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):

View File

@ -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),
]