From bd357f41d3c68a24803fa2c435c0bd26a36463fc Mon Sep 17 00:00:00 2001 From: David Arnold Date: Wed, 17 Jan 2024 20:25:27 +0100 Subject: [PATCH 1/3] feat: add defaults to db superuser prompt --- frappe/database/mariadb/setup_db.py | 5 ++++- frappe/database/postgres/setup_db.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/frappe/database/mariadb/setup_db.py b/frappe/database/mariadb/setup_db.py index 035ac18c0c..80a67584c1 100644 --- a/frappe/database/mariadb/setup_db.py +++ b/frappe/database/mariadb/setup_db.py @@ -163,7 +163,10 @@ def check_compatible_versions(): def get_root_connection(): if not frappe.local.flags.root_connection: if not frappe.flags.root_login: - frappe.flags.root_login = "root" + frappe.flags.root_login = frappe.conf.get("root_login") or None + + if not frappe.flags.root_login: + frappe.flags.root_login = input("Enter mysql super user [root]: ") or "root" if not frappe.flags.root_password: frappe.flags.root_password = frappe.conf.get("root_password") or None diff --git a/frappe/database/postgres/setup_db.py b/frappe/database/postgres/setup_db.py index 83ab09c36f..9b64293144 100644 --- a/frappe/database/postgres/setup_db.py +++ b/frappe/database/postgres/setup_db.py @@ -67,7 +67,7 @@ def get_root_connection(): frappe.flags.root_login = frappe.conf.get("root_login") or None if not frappe.flags.root_login: - frappe.flags.root_login = input("Enter postgres super user: ") + frappe.flags.root_login = input("Enter postgres super user [postgres]: ") or "postgres" if not frappe.flags.root_password: frappe.flags.root_password = frappe.conf.get("root_password") or None From 9a1af12a4124b522883323b9629d99122a7860ce Mon Sep 17 00:00:00 2001 From: David Arnold Date: Thu, 18 Jan 2024 10:20:12 +0100 Subject: [PATCH 2/3] chore: more concise code --- frappe/database/mariadb/setup_db.py | 16 +++++++--------- frappe/database/postgres/setup_db.py | 16 +++++++--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/frappe/database/mariadb/setup_db.py b/frappe/database/mariadb/setup_db.py index 80a67584c1..9908b6d5a5 100644 --- a/frappe/database/mariadb/setup_db.py +++ b/frappe/database/mariadb/setup_db.py @@ -162,19 +162,17 @@ def check_compatible_versions(): def get_root_connection(): if not frappe.local.flags.root_connection: - if not frappe.flags.root_login: - frappe.flags.root_login = frappe.conf.get("root_login") or None + from getpass import getpass if not frappe.flags.root_login: - frappe.flags.root_login = input("Enter mysql super user [root]: ") or "root" + frappe.flags.root_login = ( + frappe.conf.get("root_login") or input("Enter mysql super user [root]: ") or "root" + ) if not frappe.flags.root_password: - frappe.flags.root_password = frappe.conf.get("root_password") or None - - if not frappe.flags.root_password: - import getpass - - frappe.flags.root_password = getpass.getpass("MySQL root password: ") + frappe.flags.root_password = frappe.conf.get("root_password") or getpass( + "MySQL root password: " + ) frappe.local.flags.root_connection = frappe.database.get_db( host=frappe.conf.db_host, diff --git a/frappe/database/postgres/setup_db.py b/frappe/database/postgres/setup_db.py index 9b64293144..e02b1c1280 100644 --- a/frappe/database/postgres/setup_db.py +++ b/frappe/database/postgres/setup_db.py @@ -63,19 +63,17 @@ def import_db_from_sql(source_sql=None, verbose=False): def get_root_connection(): if not frappe.local.flags.root_connection: - if not frappe.flags.root_login: - frappe.flags.root_login = frappe.conf.get("root_login") or None + from getpass import getpass if not frappe.flags.root_login: - frappe.flags.root_login = input("Enter postgres super user [postgres]: ") or "postgres" + frappe.flags.root_login = ( + frappe.conf.get("root_login") or input("Enter postgres super user [postgres]: ") or "postgres" + ) if not frappe.flags.root_password: - frappe.flags.root_password = frappe.conf.get("root_password") or None - - if not frappe.flags.root_password: - from getpass import getpass - - frappe.flags.root_password = getpass("Postgres super user password: ") + frappe.flags.root_password = frappe.conf.get("root_password") or getpass( + "Postgres super user password: " + ) frappe.local.flags.root_connection = frappe.database.get_db( host=frappe.conf.db_host, From e5ea6018821c24cc3b627197684eedba5d2d24d4 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Thu, 18 Jan 2024 11:07:15 +0100 Subject: [PATCH 3/3] test: amend to consistently specify the test env's root db user --- frappe/tests/test_commands.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/frappe/tests/test_commands.py b/frappe/tests/test_commands.py index ad42e355c1..7eff954054 100644 --- a/frappe/tests/test_commands.py +++ b/frappe/tests/test_commands.py @@ -461,12 +461,17 @@ class TestCommands(BaseTestCommands): self.execute( f"bench new-site {site} --force --verbose " f"--admin-password {frappe.conf.admin_password} " - f"--mariadb-root-password {frappe.conf.root_password} " + f"--db-root-username {frappe.conf.root_login} " + f"--db-root-password {frappe.conf.root_password} " f"--db-type {frappe.conf.db_type} " ) self.assertEqual(self.returncode, 0) - self.execute(f"bench drop-site {site} --force --root-password {frappe.conf.root_password}") + self.execute( + f"bench drop-site {site} --force " + f"--db-root-username {frappe.conf.root_login} " + f"--db-root-password {frappe.conf.root_password} " + ) self.assertEqual(self.returncode, 0) bench_path = get_bench_path() @@ -486,7 +491,8 @@ class TestCommands(BaseTestCommands): self.execute( f"bench new-site {TEST_SITE} --verbose " f"--admin-password {frappe.conf.admin_password} " - f"--mariadb-root-password {frappe.conf.root_password} " + f"--db-root-username {frappe.conf.root_login} " + f"--db-root-password {frappe.conf.root_password} " f"--db-type {frappe.conf.db_type} " ) @@ -520,16 +526,17 @@ class TestCommands(BaseTestCommands): kwargs = { "new_site": site, "admin_password": frappe.conf.admin_password, - "root_password": frappe.conf.root_password or "", "db_type": frappe.conf.db_type, "db_user": user, "db_password": password, "db_root_username": frappe.conf.root_login, + "db_root_password": frappe.conf.root_password or "", } self.execute( "bench new-site {new_site} --force --verbose " "--admin-password {admin_password} " - "--db-root-password {root_password} " + "--db-root-username {db_root_username} " + "--db-root-password {db_root_password} " "--db-type {db_type} " "--db-user {db_user} " "--db-password {db_password}", @@ -542,7 +549,9 @@ class TestCommands(BaseTestCommands): self.assertEqual(config[site]["db_user"], user) self.assertEqual(config[site]["db_password"], password) self.execute( - "bench drop-site {new_site} --force --db-root-username {db_root_username} --db-root-password {root_password}", + "bench drop-site {new_site} --force " + "--db-root-username {db_root_username} " + "--db-root-password {db_root_password} ", kwargs, ) self.assertEqual(self.returncode, 0) @@ -564,19 +573,20 @@ class TestCommands(BaseTestCommands): kwargs = { "new_site": site, "admin_password": frappe.conf.admin_password, - "root_password": frappe.conf.root_password, "db_type": frappe.conf.db_type, "db_user": user, "db_password": password, "db_root_username": frappe.conf.root_login, + "db_root_password": frappe.conf.root_password, } self.execute( "bench new-site {new_site} --force --verbose " "--admin-password {admin_password} " - "--db-root-password {root_password} " "--db-type {db_type} " "--db-user {db_user} " - "--db-password {db_password}", + "--db-password {db_password} " + "--db-root-username {db_root_username} " + "--db-root-password {db_root_password} ", kwargs, ) self.assertEqual(self.returncode, 0) @@ -586,7 +596,9 @@ class TestCommands(BaseTestCommands): self.assertEqual(config[site]["db_user"], user) self.assertEqual(config[site]["db_password"], password) self.execute( - "bench drop-site {new_site} --force --db-root-username {db_root_username} --db-root-password {root_password}", + "bench drop-site {new_site} --force " + "--db-root-username {db_root_username} " + "--db-root-password {db_root_password} ", kwargs, ) self.assertEqual(self.returncode, 0)