From be7be766beabc5d901972b96ca6483fc2a31a691 Mon Sep 17 00:00:00 2001 From: tundebabzy Date: Mon, 8 May 2017 11:36:33 +0100 Subject: [PATCH] Issue 3171 (#3178) * adds new exception to be raised when any improper database configuration is detected * changes behavior of `check_if_ready_for_barracuda` to raise ImproperDBConfigurationError instead of sys.exit` * refactors `drop_site` to use a new exactly identical `_drop_site` function. The reason for this is because the original `drop_site` function is decorated and cannot be undecorated without nasty hacks. Breaking the function this way allows me to make use of the `drop-site` logic easily. * catches the ImproperDBConfigurationError raised from `check_if_ready_for_barracuda` function to drop all the artifacts of the failed new `Site` --- frappe/commands/site.py | 8 ++++++++ frappe/exceptions.py | 16 +++++++++++++++- frappe/installer.py | 5 +++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/frappe/commands/site.py b/frappe/commands/site.py index c3bebbdeaa..be5cd72493 100755 --- a/frappe/commands/site.py +++ b/frappe/commands/site.py @@ -67,6 +67,9 @@ def _new_site(db_name, site, mariadb_root_username=None, mariadb_root_password=N scheduler_status = "disabled" if frappe.utils.scheduler.is_scheduler_disabled() else "enabled" print("*** Scheduler is", scheduler_status, "***") + except frappe.exceptions.ImproperDBConfigurationError: + _drop_site(site, mariadb_root_username, mariadb_root_password, force=True) + finally: if installing and os.path.exists(installing): os.remove(installing) @@ -326,6 +329,10 @@ def uninstall(context, app, dry_run=False, yes=False): @click.option('--archived-sites-path') @click.option('--force', help='Force drop-site even if an error is encountered', is_flag=True, default=False) def drop_site(site, root_login='root', root_password=None, archived_sites_path=None, force=False): + _drop_site(site, root_login, root_password, archived_sites_path, force) + + +def _drop_site(site, root_login='root', root_password=None, archived_sites_path=None, force=False): "Remove site from database and filesystem" from frappe.installer import get_root_connection from frappe.model.db_schema import DbManager @@ -364,6 +371,7 @@ def drop_site(site, root_login='root', root_password=None, archived_sites_path=N move(archived_sites_path, site) + def move(dest_dir, site): import os if not os.path.isdir(dest_dir): diff --git a/frappe/exceptions.py b/frappe/exceptions.py index 1ffbf96391..1a5f6d688e 100644 --- a/frappe/exceptions.py +++ b/frappe/exceptions.py @@ -6,7 +6,8 @@ from __future__ import unicode_literals # BEWARE don't put anything in this file except exceptions from werkzeug.exceptions import NotFound -from MySQLdb import ProgrammingError as SQLError +from MySQLdb import ProgrammingError as SQLError, Error + class ValidationError(Exception): http_status_code = 417 @@ -41,6 +42,19 @@ class Redirect(Exception): class CSRFTokenError(Exception): http_status_code = 400 + +class ImproperDBConfigurationError(Error): + """ + Used when frappe detects that database or tables are not properly + configured + """ + def __init__(self, reason, msg=None): + if not msg: + msg = "MariaDb is not properly configured" + super(ImproperDBConfigurationError, self).__init__(msg) + self.reason = reason + + class DuplicateEntryError(NameError):pass class DataError(ValidationError): pass class UnknownDomainError(Exception): pass diff --git a/frappe/installer.py b/frappe/installer.py index db82c3eb17..f4a4b1a711 100755 --- a/frappe/installer.py +++ b/frappe/installer.py @@ -364,8 +364,9 @@ def check_if_ready_for_barracuda(): "").format(x=site, sep2="\n"*2, sep="\n") print_db_config(msg, expected_config_for_barracuda) - sys.exit(1) - # raise Exception, "MariaDB needs to be configured!" + raise frappe.exceptions.ImproperDBConfigurationError( + reason="MariaDB default file format is not Barracuda" + ) def print_db_config(explanation, config_text):