From 21e2b139556238d0ad21cb151016c1429ed34f51 Mon Sep 17 00:00:00 2001 From: Revant Nandgaonkar Date: Fri, 10 Jul 2020 19:21:59 +0530 Subject: [PATCH] fix: mysql db commands with subprocess --- build/common/commands/new.py | 6 +++++- build/common/commands/restore_backup.py | 12 ++++++------ build/common/commands/utils.py | 1 + 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/build/common/commands/new.py b/build/common/commands/new.py index f6cc00a3..3438fa89 100644 --- a/build/common/commands/new.py +++ b/build/common/commands/new.py @@ -97,6 +97,10 @@ def main(): mysql_command = ["mysql", f"-h{db_host}", f"-u{mariadb_root_username}", f"-p{mariadb_root_password}", "-e"] + # Drop User if exists + command = mysql_command + [f"DROP USER IF EXISTS '{db_name}'@'%'; FLUSH PRIVILEGES;"] + run_command(command) + # update User's host to '%' required to connect from any container command = mysql_command + [f"UPDATE mysql.user SET Host = '%' where User = '{db_name}'; FLUSH PRIVILEGES;"] run_command(command) @@ -106,7 +110,7 @@ def main(): run_command(command) # Grant permission to database - command = mysql_command + [f"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;"] + command = mysql_command + [f"GRANT ALL PRIVILEGES ON `{db_name}`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;"] run_command(command) if frappe.redis_server: diff --git a/build/common/commands/restore_backup.py b/build/common/commands/restore_backup.py index 2ae6ca78..9fa9ef55 100644 --- a/build/common/commands/restore_backup.py +++ b/build/common/commands/restore_backup.py @@ -222,7 +222,7 @@ def restore_mariadb(config, site_config, database_file): db_root_user = os.environ.get("DB_ROOT_USER", 'root') db_host = site_config.get('db_host', config.get('db_host')) - db_port = site_config.get('db_port', config.get('db_port')) + db_port = site_config.get('db_port', config.get('db_port', 3306)) db_name = site_config.get('db_name') db_password = site_config.get('db_password') @@ -230,23 +230,23 @@ def restore_mariadb(config, site_config, database_file): mysql_command = ["mysql", f"-u{db_root_user}", f"-h{db_host}", f"-p{db_root_password}", f"-P{db_port}"] # drop db if exists for clean restore - drop_database = mysql_command + ["-e", f"DROP DATABASE IF EXISTS \`{db_name}\`;"] + drop_database = mysql_command + ["-e", f"DROP DATABASE IF EXISTS `{db_name}`;"] run_command(drop_database) # create db - create_database = mysql_command + ["-e", f"CREATE DATABASE IF NOT EXISTS \`{db_name}\`;"] + create_database = mysql_command + ["-e", f"CREATE DATABASE IF NOT EXISTS `{db_name}`;"] run_command(create_database) # create user - create_user = mysql_command + ["-e", f"CREATE USER IF NOT EXISTS \'{db_name}\'@\'%\' IDENTIFIED BY \'{db_password}\'; FLUSH PRIVILEGES;"] + create_user = mysql_command + ["-e", f"CREATE USER IF NOT EXISTS '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;"] run_command(create_user) # grant db privileges to user - grant_privileges = mysql_command + ["-e", f"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;"] + grant_privileges = mysql_command + ["-e", f"GRANT ALL PRIVILEGES ON `{db_name}`.* TO '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;"] run_command(grant_privileges) print('Restoring MariaDB') - with open(database_file.replace(".gz", ""), "r") as db_file: + with open(database_file.replace('.gz', ''), 'r') as db_file: run_command(mysql_command + [f"{db_name}", "<"], stdin=db_file) diff --git a/build/common/commands/utils.py b/build/common/commands/utils.py index 8ebffb63..c808132e 100644 --- a/build/common/commands/utils.py +++ b/build/common/commands/utils.py @@ -4,6 +4,7 @@ import subprocess def run_command(command, stdout=None, stdin=None, stderr=None): stdout = stdout or subprocess.PIPE stderr = stderr or subprocess.PIPE + stdin = stdin or subprocess.PIPE process = subprocess.Popen(command, stdout=stdout, stdin=stdin, stderr=stderr) out, error = process.communicate() if process.returncode: