Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OperationalError from SQlite that indicates a permissions problem. #2508

Merged
merged 12 commits into from Apr 19, 2017
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 @@ -1247,3 +1248,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 @@ -84,6 +84,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