diff --git a/frappe/database/database.py b/frappe/database/database.py index 5b99f75938..a7490675ba 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -73,7 +73,7 @@ class Database: self.host = host or frappe.conf.db_host self.port = port or frappe.conf.db_port self.user = user or frappe.conf.db_name - self.db_name = frappe.conf.db_name + self.cur_db_name = frappe.conf.db_name self._conn = None if ac_name: @@ -103,7 +103,6 @@ class Database: def connect(self): """Connects to a database as set in `site_config.json`.""" - self.cur_db_name = self.user self._conn = self.get_connection() self._cursor = self._conn.cursor() @@ -121,6 +120,7 @@ class Database: def use(self, db_name): """`USE` db_name.""" self._conn.select_db(db_name) + self.cur_db_name = db_name def get_connection(self): """Return a Database connection object that conforms with https://peps.python.org/pep-0249/#connection-objects.""" diff --git a/frappe/database/mariadb/database.py b/frappe/database/mariadb/database.py index 00cbd1c332..33eddeaf42 100644 --- a/frappe/database/mariadb/database.py +++ b/frappe/database/mariadb/database.py @@ -124,7 +124,7 @@ class MariaDBConnectionUtil: } if self.user not in (frappe.flags.root_login, "root"): - conn_settings["database"] = self.user + conn_settings["database"] = self.cur_db_name if self.port: conn_settings["port"] = int(self.port) @@ -198,7 +198,7 @@ class MariaDBDatabase(MariaDBConnectionUtil, MariaDBExceptionUtil, Database): SUM(`data_length` + `index_length`) / 1024 / 1024 AS `database_size` FROM information_schema.tables WHERE `table_schema` = %s GROUP BY `table_schema` """, - self.db_name, + self.cur_db_name, as_dict=True, ) diff --git a/frappe/database/postgres/database.py b/frappe/database/postgres/database.py index 48dd55381a..2ed595d5e7 100644 --- a/frappe/database/postgres/database.py +++ b/frappe/database/postgres/database.py @@ -162,10 +162,11 @@ class PostgresDatabase(PostgresExceptionUtil, Database): def get_connection(self): conn_settings = { "user": self.user, - "dbname": self.user, "host": self.host, "password": self.password, } + if self.user not in (frappe.flags.root_login, "root"): + conn_settings["dbname"] = self.cur_db_name if self.port: conn_settings["port"] = self.port @@ -199,7 +200,7 @@ class PostgresDatabase(PostgresExceptionUtil, Database): def get_database_size(self): """Return database size in MB""" db_size = self.sql( - "SELECT (pg_database_size(%s) / 1024 / 1024) as database_size", self.db_name, as_dict=True + "SELECT (pg_database_size(%s) / 1024 / 1024) as database_size", self.cur_db_name, as_dict=True ) return db_size[0].get("database_size") @@ -219,7 +220,7 @@ class PostgresDatabase(PostgresExceptionUtil, Database): where table_catalog='{}' and table_type = 'BASE TABLE' and table_schema='{}'""".format( - frappe.conf.db_name, frappe.conf.get("db_schema", "public") + self.cur_db_name, frappe.conf.get("db_schema", "public") ) ) ] diff --git a/frappe/patches/v15_0/remove_implicit_primary_key.py b/frappe/patches/v15_0/remove_implicit_primary_key.py index 388edca8cd..50becd0bdb 100644 --- a/frappe/patches/v15_0/remove_implicit_primary_key.py +++ b/frappe/patches/v15_0/remove_implicit_primary_key.py @@ -44,7 +44,7 @@ def _is_implicit_int_pk(doctype: str) -> bool: values = () if frappe.db.db_type == "mariadb": query += " and table_schema = %s" - values = (frappe.db.db_name,) + values = (frappe.db.cur_db_name,) col_type = frappe.db.sql(query, values) return bool(col_type and col_type[0][0] == "bigint")