2
0
mirror of https://github.com/frappe/frappe_docker.git synced 2025-01-11 09:35:12 +00:00

fix: mysql db commands with subprocess

This commit is contained in:
Revant Nandgaonkar 2020-07-10 19:21:59 +05:30
parent 4598cb24f4
commit 21e2b13955
3 changed files with 12 additions and 7 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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: