Skip to content

Commit

Permalink
Merge 05a47bd into b145c4e
Browse files Browse the repository at this point in the history
  • Loading branch information
agateau committed Dec 1, 2015
2 parents b145c4e + 05a47bd commit ff93328
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
38 changes: 21 additions & 17 deletions yokadi/core/db.py
Expand Up @@ -27,7 +27,6 @@
sys.exit(1)

from yokadi.core.yokadiexception import YokadiException
from yokadi.ycli import tui # TODO: try to remove dependancy on tui
from yokadi.core import utils

# Yokadi database version needed for this code
Expand All @@ -39,6 +38,15 @@
# Task frequency
FREQUENCY = {0: "Yearly", 1: "Monthly", 2: "Weekly", 3: "Daily"}


class DbUserError(Exception):
"""
This exception is for errors which are not caused by a failure in our code
and which must be fixed by the user.
"""
pass


Base = declarative_base()


Expand Down Expand Up @@ -231,16 +239,14 @@ def __init__(self, dbFileName, createIfNeeded=True, memoryDatabase=False, update
self.session = Session()

if not os.path.exists(dbFileName) or memoryDatabase:
if createIfNeeded:
print("Creating %s" % dbFileName)
self.createTables()
# Set database version according to current yokadi release
if not updateMode: # Update script add it from dump
self.session.add(Config(name=DB_VERSION_KEY, value=str(DB_VERSION), system=True, desc="Database schema release number"))
self.session.commit()
else:
print("Database file (%s) does not exist or is not readable. Exiting" % dbFileName)
sys.exit(1)
if not createIfNeeded:
raise DbUserError("Database file (%s) does not exist or is not readable." % dbFileName)
print("Creating %s" % dbFileName)
self.createTables()
# Set database version according to current yokadi release
if not updateMode: # Update script add it from dump
self.session.add(Config(name=DB_VERSION_KEY, value=str(DB_VERSION), system=True, desc="Database schema release number"))
self.session.commit()

if not updateMode:
self.checkVersion()
Expand Down Expand Up @@ -271,12 +277,10 @@ def checkVersion(self):
version = self.getVersion()
if version != DB_VERSION:
sharePath = os.path.abspath(utils.shareDirPath())
tui.error("Your database version is %d but Yokadi wants version %d." \
% (version, DB_VERSION))
print("Please, run the %s/update/update.py script to migrate your database prior to running Yokadi" % \
sharePath)
print("See %s/doc/update.md for details" % sharePath)
sys.exit(1)
msg = "Your database version is %d but Yokadi wants version %d.\n" % (version, DB_VERSION)
msg += "Please run the %s/update/update.py script to migrate your database prior to running Yokadi.\n" % sharePath
msg += "See %s/doc/update.md for details." % sharePath
raise DbUserError(msg)


def setDefaultConfig():
Expand Down
6 changes: 5 additions & 1 deletion yokadi/ycli/main.py
Expand Up @@ -223,7 +223,11 @@ def main():
args.filename = os.path.normcase(os.path.expanduser("~/.yokadi.db"))
print("Using default database (%s)" % args.filename)

db.connectDatabase(args.filename)
try:
db.connectDatabase(args.filename)
except db.DbUserError as exc:
print(exc)
sys.exit(1)

if args.createOnly:
return
Expand Down

0 comments on commit ff93328

Please sign in to comment.