Skip to content

Commit

Permalink
Merge pull request #2508 from Mary011196/master
Browse files Browse the repository at this point in the history
OperationalError from SQlite that indicates a permissions problem.
  • Loading branch information
sampsyo committed Apr 19, 2017
2 parents 512031a + 7eaaa99 commit 21c59bf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
23 changes: 21 additions & 2 deletions beets/dbcore/db.py
Expand Up @@ -33,6 +33,15 @@
import six


class DBAccessError(Exception):
"""The SQLite database became inaccessible.
This can happen when trying to read or write the database when, for
example, the database file is deleted or otherwise disappears. There
is probably no way to recover from this error.
"""


class FormattedMapping(collections.Mapping):
"""A `dict`-like formatted view of a model.
Expand Down Expand Up @@ -680,8 +689,18 @@ def mutate(self, statement, subvals=()):
"""Execute an SQL statement with substitution values and return
the row ID of the last affected row.
"""
cursor = self.db._connection().execute(statement, subvals)
return cursor.lastrowid
try:
cursor = self.db._connection().execute(statement, subvals)
return cursor.lastrowid
except sqlite3.OperationalError as e:
# In two specific cases, SQLite reports an error while accessing
# the underlying database file. We surface these exceptions as
# DBAccessError so the application can abort.
if e.args[0] in ("attempt to write a readonly database",
"unable to open database file"):
raise DBAccessError(e.args[0])
else:
raise

def script(self, statements):
"""Execute a string containing multiple SQL statements."""
Expand Down
8 changes: 8 additions & 0 deletions beets/ui/__init__.py
Expand Up @@ -41,6 +41,7 @@
from beets.util import confit, as_string
from beets.autotag import mb
from beets.dbcore import query as db_query
from beets.dbcore import db
import six

# On Windows platforms, use colorama to support "ANSI" terminal colors.
Expand Down Expand Up @@ -1253,3 +1254,10 @@ def main(args=None):
except KeyboardInterrupt:
# Silently ignore ^C except in verbose mode.
log.debug(u'{}', traceback.format_exc())
except db.DBAccessError as exc:
log.error(
u'database access error: {0}\n'
u'the library file might have a permissions problem',
exc
)
sys.exit(1)
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -88,6 +88,8 @@ Fixes:
AAC codec instead of faac. Thanks to :user:`jansol`. :bug:`2484`
* Fix import of multidisc releases with subdirectories, which previously
made each disc be imported separately in different releases. :bug:`2493`
* When the SQLite database stops being accessible, we now print a friendly
error message. Thanks to :user:`Mary011196`. :bug:`1676` :bug:`2508`


1.4.3 (January 9, 2017)
Expand Down

0 comments on commit 21c59bf

Please sign in to comment.