Repository: qutebrowser/qutebrowser

Issue:
Crash with web-history-max-items and no items in the history
When running `qutebrowser --debug --temp-basedir --backend webengine -s completion web-history-max-items 1000` and immediately pressing `o`, this happens:

```
12:21:36 DEBUG    sql        sql:__init__:80 Preparing SQL query: "SELECT min(last_atime) FROM (SELECT last_atime FROM CompletionHistory ORDER BY last_atime DESC LIMIT :limit)"
12:21:36 DEBUG    sql        sql:run:99 Running SQL query: "SELECT min(last_atime) FROM (SELECT last_atime FROM CompletionHistory ORDER BY last_atime DESC LIMIT :limit)"
12:21:36 DEBUG    sql        sql:run:102 query bindings: {':limit': 1000}
12:21:36 DEBUG    sql        sql:__init__:80 Preparing SQL query: "SELECT url, title, strftime('%Y-%m-%d', last_atime, 'unixepoch', 'localtime') FROM CompletionHistory WHERE (url LIKE :pat escape '\' or title LIKE :pat escape '\') AND last_atime >=  ORDER BY last_atime DESC"
12:21:36 DEBUG    completion debug:__exit__:264 Starting url completion took 0.003652 seconds.
12:21:36 ERROR    misc       crashsignal:exception_hook:205 Uncaught exception
Traceback (most recent call last):
  File "/home/florian/proj/qutebrowser/git/qutebrowser/completion/completer.py", line 236, in _update_completion
    model = func(*args)
  File "/home/florian/proj/qutebrowser/git/qutebrowser/completion/models/urlmodel.py", line 70, in url
    hist_cat = histcategory.HistoryCategory(delete_func=_delete_history)
  File "/home/florian/proj/qutebrowser/git/qutebrowser/completion/models/histcategory.py", line 54, in __init__
    ]), forward_only=False)
  File "/home/florian/proj/qutebrowser/git/qutebrowser/misc/sql.py", line 83, in __init__
    querystr, self.lastError().text()))
qutebrowser.misc.sql.SqlException: Failed to prepare query "SELECT url, title, strftime('%Y-%m-%d', last_atime, 'unixepoch', 'localtime') FROM CompletionHistory WHERE (url LIKE :pat escape '\' or title LIKE :pat escape '\') AND last_atime >=  ORDER BY last_atime DESC": "near "ORDER": syntax error Unable to execute statement"
```

cc @rcorre 


Discussion:
While we're at it, we should probably add an `assert min_atime` in `_atime_expr` to make sure we have a value for `min_atime`.

Resolving patch:
diff --git a/qutebrowser/completion/models/histcategory.py b/qutebrowser/completion/models/histcategory.py
--- a/qutebrowser/completion/models/histcategory.py
+++ b/qutebrowser/completion/models/histcategory.py
@@ -72,6 +72,10 @@ def _atime_expr(self):
             'ORDER BY last_atime DESC LIMIT :limit)',
         ])).run(limit=max_items).value()
 
+        if not min_atime:
+            # if there are no history items, min_atime may be '' (issue #2849)
+            return ''
+
         return "AND last_atime >= {}".format(min_atime)
 
     def set_pattern(self, pattern):


Test patch:
diff --git a/tests/unit/completion/test_histcategory.py b/tests/unit/completion/test_histcategory.py
--- a/tests/unit/completion/test_histcategory.py
+++ b/tests/unit/completion/test_histcategory.py
@@ -126,7 +126,8 @@ def test_set_pattern(pattern, before, after, model_validator, hist):
     ], [
         ('b', 'b', '2017-06-16'),
         ('c', 'c', '2017-05-16'),
-    ])
+    ]),
+    (1, [], []),  # issue 2849 (crash with empty history)
 ])
 def test_sorting(max_items, before, after, model_validator, hist, config_stub):
     """Validate the filtering and sorting results of set_pattern."""