Previous attempts to run this would lead to stalls...long long stalls. And nothing blocking the query, it would stay in a "Waiting for table metadata lock" state.
85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
import frappe
|
|
|
|
|
|
class DbManager:
|
|
def __init__(self, db):
|
|
"""
|
|
Pass root_conn here for access to all databases.
|
|
"""
|
|
if db:
|
|
self.db = db
|
|
|
|
def get_current_host(self):
|
|
return self.db.sql("select user()")[0][0].split("@")[1]
|
|
|
|
def create_user(self, user, password, host=None):
|
|
host = host or self.get_current_host()
|
|
password_predicate = f" IDENTIFIED BY '{password}'" if password else ""
|
|
self.db.sql(f"CREATE USER '{user}'@'{host}'{password_predicate}")
|
|
|
|
def delete_user(self, target, host=None):
|
|
host = host or self.get_current_host()
|
|
self.db.sql(f"DROP USER IF EXISTS '{target}'@'{host}'")
|
|
|
|
def create_database(self, target):
|
|
if target in self.get_database_list():
|
|
self.drop_database(target)
|
|
self.db.sql(f"CREATE DATABASE `{target}`")
|
|
|
|
def drop_database(self, target):
|
|
self.db.sql_ddl(f"DROP DATABASE IF EXISTS `{target}`")
|
|
|
|
def grant_all_privileges(self, target, user, host=None):
|
|
host = host or self.get_current_host()
|
|
permissions = (
|
|
(
|
|
"SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, "
|
|
"CREATE TEMPORARY TABLES, CREATE VIEW, EVENT, TRIGGER, SHOW VIEW, "
|
|
"CREATE ROUTINE, ALTER ROUTINE, EXECUTE, LOCK TABLES"
|
|
)
|
|
if frappe.conf.rds_db
|
|
else "ALL PRIVILEGES"
|
|
)
|
|
self.db.sql(f"GRANT {permissions} ON `{target}`.* TO '{user}'@'{host}'")
|
|
|
|
def flush_privileges(self):
|
|
self.db.sql("FLUSH PRIVILEGES")
|
|
|
|
def get_database_list(self):
|
|
return self.db.sql("SHOW DATABASES", pluck=True)
|
|
|
|
@staticmethod
|
|
def restore_database(target, source, user, password):
|
|
import os
|
|
from distutils.spawn import find_executable
|
|
|
|
from frappe.utils import make_esc
|
|
|
|
esc = make_esc("$ ")
|
|
pv = find_executable("pv")
|
|
|
|
if pv:
|
|
pipe = "{pv} {source} |".format(pv=pv, source=source)
|
|
source = ""
|
|
else:
|
|
pipe = ""
|
|
source = "< {source}".format(source=source)
|
|
|
|
if pipe:
|
|
print("Restoring Database file...")
|
|
|
|
command = (
|
|
"{pipe} mysql -u {user} -p{password} -h{host} "
|
|
+ ("-P{port}" if frappe.db.port else "")
|
|
+ " {target} {source}"
|
|
)
|
|
command = command.format(
|
|
pipe=pipe,
|
|
user=esc(user),
|
|
password=esc(password),
|
|
host=esc(frappe.db.host),
|
|
target=esc(target),
|
|
source=source,
|
|
port=frappe.db.port,
|
|
)
|
|
os.system(command)
|