fix(drop-site): set db root credentials in frappe.flags like how new-site is done
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
This commit is contained in:
parent
f876aa385c
commit
265c0b0e01
5 changed files with 51 additions and 46 deletions
|
|
@ -1061,9 +1061,11 @@ def _drop_site(
|
|||
sys.exit(1)
|
||||
|
||||
click.secho("Dropping site database and user", fg="green")
|
||||
drop_user_and_database(
|
||||
frappe.conf.db_name, frappe.conf.db_user, db_root_username, db_root_password
|
||||
)
|
||||
|
||||
frappe.flags.root_login = db_root_username
|
||||
frappe.flags.root_password = db_root_password
|
||||
|
||||
drop_user_and_database(frappe.conf.db_name, frappe.conf.db_user)
|
||||
|
||||
archived_sites_path = archived_sites_path or os.path.join(
|
||||
frappe.utils.get_bench_path(), "archived", "sites"
|
||||
|
|
@ -1341,7 +1343,6 @@ def build_search_index(context):
|
|||
@click.option("--no-backup", is_flag=True, default=False, help="Do not backup the table")
|
||||
@pass_context
|
||||
def clear_log_table(context, doctype, days, no_backup):
|
||||
|
||||
"""If any logtype table grows too large then clearing it with DELETE query
|
||||
is not feasible in reasonable time. This command copies recent data to new
|
||||
table and replaces current table with new smaller table.
|
||||
|
|
|
|||
|
|
@ -36,21 +36,17 @@ def bootstrap_database(db_name, verbose=None, source_sql=None):
|
|||
return frappe.database.mariadb.setup_db.bootstrap_database(db_name, verbose, source_sql)
|
||||
|
||||
|
||||
def drop_user_and_database(db_name, db_user, root_login=None, root_password=None):
|
||||
def drop_user_and_database(db_name, db_user):
|
||||
import frappe
|
||||
|
||||
if frappe.conf.db_type == "postgres":
|
||||
import frappe.database.postgres.setup_db
|
||||
|
||||
return frappe.database.postgres.setup_db.drop_user_and_database(
|
||||
db_name, db_user, root_login, root_password
|
||||
)
|
||||
return frappe.database.postgres.setup_db.drop_user_and_database(db_name, db_user)
|
||||
else:
|
||||
import frappe.database.mariadb.setup_db
|
||||
|
||||
return frappe.database.mariadb.setup_db.drop_user_and_database(
|
||||
db_name, db_user, root_login, root_password
|
||||
)
|
||||
return frappe.database.mariadb.setup_db.drop_user_and_database(db_name, db_user)
|
||||
|
||||
|
||||
def get_db(host=None, user=None, password=None, port=None):
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ def setup_database(force, verbose, no_mariadb_socket=False):
|
|||
|
||||
db_user = frappe.conf.db_user
|
||||
db_name = frappe.local.conf.db_name
|
||||
root_conn = get_root_connection(frappe.flags.root_login, frappe.flags.root_password)
|
||||
root_conn = get_root_connection()
|
||||
dbman = DbManager(root_conn)
|
||||
dbman_kwargs = {}
|
||||
if no_mariadb_socket:
|
||||
|
|
@ -62,8 +62,11 @@ def setup_database(force, verbose, no_mariadb_socket=False):
|
|||
root_conn.close()
|
||||
|
||||
|
||||
def drop_user_and_database(db_name, db_user, root_login, root_password):
|
||||
frappe.local.db = get_root_connection(root_login, root_password)
|
||||
def drop_user_and_database(
|
||||
db_name,
|
||||
db_user,
|
||||
):
|
||||
frappe.local.db = get_root_connection()
|
||||
dbman = DbManager(frappe.local.db)
|
||||
dbman.drop_database(db_name)
|
||||
dbman.delete_user(db_user, host="%")
|
||||
|
|
@ -109,7 +112,6 @@ def import_db_from_sql(source_sql=None, verbose=False):
|
|||
|
||||
|
||||
def check_database_settings():
|
||||
|
||||
check_compatible_versions()
|
||||
|
||||
# Check each expected value vs. actuals:
|
||||
|
|
@ -158,24 +160,24 @@ def check_compatible_versions():
|
|||
)
|
||||
|
||||
|
||||
def get_root_connection(root_login, root_password):
|
||||
import getpass
|
||||
|
||||
def get_root_connection():
|
||||
if not frappe.local.flags.root_connection:
|
||||
if not root_login:
|
||||
root_login = "root"
|
||||
if not frappe.flags.root_login:
|
||||
frappe.flags.root_login = "root"
|
||||
|
||||
if not root_password:
|
||||
root_password = frappe.conf.get("root_password") or None
|
||||
if not frappe.flags.root_password:
|
||||
frappe.flags.root_password = frappe.conf.get("root_password") or None
|
||||
|
||||
if not root_password:
|
||||
root_password = getpass.getpass("MySQL root password: ")
|
||||
if not frappe.flags.root_password:
|
||||
import getpass
|
||||
|
||||
frappe.flags.root_password = getpass.getpass("MySQL root password: ")
|
||||
|
||||
frappe.local.flags.root_connection = frappe.database.get_db(
|
||||
host=frappe.conf.db_host,
|
||||
port=frappe.conf.db_port,
|
||||
user=root_login,
|
||||
password=root_password,
|
||||
user=frappe.flags.root_login,
|
||||
password=frappe.flags.root_password,
|
||||
)
|
||||
|
||||
return frappe.local.flags.root_connection
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from frappe.utils import cint
|
|||
|
||||
|
||||
def setup_database():
|
||||
root_conn = get_root_connection(frappe.flags.root_login, frappe.flags.root_password)
|
||||
root_conn = get_root_connection()
|
||||
root_conn.commit()
|
||||
root_conn.sql("end")
|
||||
root_conn.sql(f"DROP DATABASE IF EXISTS `{frappe.conf.db_name}`")
|
||||
|
|
@ -61,36 +61,34 @@ def import_db_from_sql(source_sql=None, verbose=False):
|
|||
print("Imported from database %s" % source_sql)
|
||||
|
||||
|
||||
def get_root_connection(root_login=None, root_password=None):
|
||||
def get_root_connection():
|
||||
if not frappe.local.flags.root_connection:
|
||||
if not root_login:
|
||||
root_login = frappe.conf.get("root_login") or None
|
||||
if not frappe.flags.root_login:
|
||||
frappe.flags.root_login = frappe.conf.get("root_login") or None
|
||||
|
||||
if not root_login:
|
||||
root_login = input("Enter postgres super user: ")
|
||||
if not frappe.flags.root_login:
|
||||
frappe.flags.root_login = input("Enter postgres super user: ")
|
||||
|
||||
if not root_password:
|
||||
root_password = frappe.conf.get("root_password") or None
|
||||
if not frappe.flags.root_password:
|
||||
frappe.flags.root_password = frappe.conf.get("root_password") or None
|
||||
|
||||
if not root_password:
|
||||
if not frappe.flags.root_password:
|
||||
from getpass import getpass
|
||||
|
||||
root_password = getpass("Postgres super user password: ")
|
||||
frappe.flags.root_password = getpass("Postgres super user password: ")
|
||||
|
||||
frappe.local.flags.root_connection = frappe.database.get_db(
|
||||
host=frappe.conf.db_host,
|
||||
port=frappe.conf.db_port,
|
||||
user=root_login,
|
||||
password=root_password,
|
||||
user=frappe.flags.root_login,
|
||||
password=frappe.flags.root_password,
|
||||
)
|
||||
|
||||
return frappe.local.flags.root_connection
|
||||
|
||||
|
||||
def drop_user_and_database(db_name, db_user, root_login, root_password):
|
||||
root_conn = get_root_connection(
|
||||
frappe.flags.root_login or root_login, frappe.flags.root_password or root_password
|
||||
)
|
||||
def drop_user_and_database(db_name, db_user):
|
||||
root_conn = get_root_connection()
|
||||
root_conn.commit()
|
||||
root_conn.sql(
|
||||
"SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = %s",
|
||||
|
|
|
|||
|
|
@ -524,6 +524,7 @@ class TestCommands(BaseTestCommands):
|
|||
"db_type": frappe.conf.db_type,
|
||||
"db_user": user,
|
||||
"db_password": password,
|
||||
"db_root_username": frappe.conf.root_login,
|
||||
}
|
||||
self.execute(
|
||||
"bench new-site {new_site} --force --verbose "
|
||||
|
|
@ -540,7 +541,10 @@ class TestCommands(BaseTestCommands):
|
|||
config = json.loads(self.stdout)
|
||||
self.assertEqual(config[site]["db_user"], user)
|
||||
self.assertEqual(config[site]["db_password"], password)
|
||||
self.execute("bench drop-site {new_site} --force --db-root-password {root_password}", kwargs)
|
||||
self.execute(
|
||||
"bench drop-site {new_site} --force --db-root-username {db_root_username} --db-root-password {root_password}",
|
||||
kwargs,
|
||||
)
|
||||
self.assertEqual(self.returncode, 0)
|
||||
|
||||
def test_existing_db_username(self):
|
||||
|
|
@ -550,12 +554,12 @@ class TestCommands(BaseTestCommands):
|
|||
if frappe.conf.db_type == "mariadb":
|
||||
from frappe.database.mariadb.setup_db import get_root_connection
|
||||
|
||||
root_conn = get_root_connection(frappe.flags.root_login, frappe.flags.root_password)
|
||||
root_conn = get_root_connection()
|
||||
root_conn.sql(f"CREATE USER '{user}'@'localhost'")
|
||||
else:
|
||||
from frappe.database.postgres.setup_db import get_root_connection
|
||||
|
||||
root_conn = get_root_connection(frappe.flags.root_login, frappe.flags.root_password)
|
||||
root_conn = get_root_connection()
|
||||
root_conn.sql(f"CREATE USER {user}")
|
||||
password = frappe.conf.db_password or frappe.generate_hash()
|
||||
kwargs = {
|
||||
|
|
@ -565,6 +569,7 @@ class TestCommands(BaseTestCommands):
|
|||
"db_type": frappe.conf.db_type,
|
||||
"db_user": user,
|
||||
"db_password": password,
|
||||
"db_root_username": frappe.conf.root_login,
|
||||
}
|
||||
self.execute(
|
||||
"bench new-site {new_site} --force --verbose "
|
||||
|
|
@ -581,7 +586,10 @@ class TestCommands(BaseTestCommands):
|
|||
config = json.loads(self.stdout)
|
||||
self.assertEqual(config[site]["db_user"], user)
|
||||
self.assertEqual(config[site]["db_password"], password)
|
||||
self.execute("bench drop-site {new_site} --force --db-root-password {root_password}", kwargs)
|
||||
self.execute(
|
||||
"bench drop-site {new_site} --force --db-root-username {db_root_username} --db-root-password {root_password}",
|
||||
kwargs,
|
||||
)
|
||||
self.assertEqual(self.returncode, 0)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue