[GH-632] raising exception in upgrade.py on fail

Handling it in install.py to prevent config from being overwritten when upgrade fails
This commit is contained in:
Micah Jerome Ellison 2019-08-24 15:01:23 -07:00
parent 12c474ecd8
commit b13a411e6f
2 changed files with 16 additions and 2 deletions

View file

@ -14,6 +14,7 @@ from .Journal import PlainJournal
from .EncryptedJournal import EncryptedJournal
import yaml
import logging
import sys
DEFAULT_CONFIG_NAME = 'jrnl.yaml'
DEFAULT_JOURNAL_NAME = 'journal.txt'
@ -85,8 +86,15 @@ def load_or_install_jrnl():
if os.path.exists(config_path):
log.debug('Reading configuration from file %s', config_path)
config = util.load_config(config_path)
upgrade.upgrade_jrnl_if_necessary(config_path)
try:
upgrade.upgrade_jrnl_if_necessary(config_path)
except upgrade.UpgradeValidationException:
util.prompt("Aborting upgrade. Exiting.")
sys.exit(1)
upgrade_config(config)
return config
else:
log.debug('Configuration file not found, installing jrnl...')

View file

@ -96,12 +96,14 @@ older versions of jrnl anymore.
# loop through lists to validate
failed_journals = [j for j in all_journals if not j.validate_parsing()]
if len(failed_journals) > 0:
util.prompt("\nThe following journal{} failed to upgrade:\n{}".format(
's' if len(failed_journals) > 1 else '', "\n".join(j.name for j in failed_journals))
)
util.prompt("Aborting upgrade.")
raise UpgradeValidationException
return
# write all journals - or - don't
@ -112,3 +114,7 @@ older versions of jrnl anymore.
backup(config_path)
util.prompt("\nWe're all done here and you can start enjoying jrnl 2.".format(config_path))
class UpgradeValidationException(Exception):
"""Raised when the contents of an upgraded journal do not match the old journal"""
pass