Merge pull request #24318 from blaggacao/refactor/explicit-current-db-name

This commit is contained in:
Raffael Meyer 2024-01-14 18:21:47 +01:00 committed by GitHub
commit 431b2eb89c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 8 deletions

View file

@ -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."""

View file

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

View file

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

View file

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