fix: Set default port attribute for Database classes

db.default_port wil be available as a class attribute to hold defaults
for DB types.

Usage: frappe.conf.db_port or frappe.db.default_port
Why: I couldn't run the mariadb command because the defaults aren't set
for my system. server is remote / containerized. Setting port in
equivalent mysql command fixes this.
This commit is contained in:
Gavin D'souza 2022-07-22 13:16:26 +05:30
parent d285ce910f
commit bcfa8c276e
5 changed files with 19 additions and 20 deletions

View file

@ -523,22 +523,24 @@ def postgres(context):
def _mariadb():
from frappe.database.mariadb.database import MariaDBDatabase
mysql = find_executable("mysql")
os.execv(
command = [
mysql,
[
mysql,
"-u",
frappe.conf.db_name,
"-p" + frappe.conf.db_password,
frappe.conf.db_name,
"-h",
frappe.conf.db_host or "localhost",
"--pager=less -SFX",
"--safe-updates",
"-A",
],
)
"--port",
frappe.conf.db_port or MariaDBDatabase.default_port,
"-u",
frappe.conf.db_name,
f"-p{frappe.conf.db_password}",
frappe.conf.db_name,
"-h",
frappe.conf.db_host or "localhost",
"--pager=less -SFX",
"--safe-updates",
"-A",
]
os.execv(mysql, command)
def _psql():

View file

@ -143,6 +143,7 @@ class MariaDBDatabase(MariaDBConnectionUtil, MariaDBExceptionUtil, Database):
FIELD_TYPE.DATETIME: get_datetime,
UnicodeWithAttrs: escape_string,
}
default_port = "3306"
def setup_type_map(self):
self.db_type = "mariadb"

View file

@ -108,6 +108,7 @@ class PostgresDatabase(PostgresExceptionUtil, Database):
# to the next non-cached value hence not using cache in postgres.
# ref: https://stackoverflow.com/questions/21356375/postgres-9-0-4-sequence-skipping-numbers
SEQUENCE_CACHE = 0
default_port = "5432"
def setup_type_map(self):
self.db_type = "postgres"

View file

@ -97,7 +97,6 @@ class TestDB(unittest.TestCase):
)
def test_get_value_limits(self):
# check both dict and list style filters
filters = [{"enabled": 1}, [["enabled", "=", 1]]]
for filter in filters:

View file

@ -73,11 +73,7 @@ class BackupGenerator:
if not self.db_type:
self.db_type = "mariadb"
if not self.db_port:
if self.db_type == "mariadb":
self.db_port = 3306
if self.db_type == "postgres":
self.db_port = 5432
self.db_port = self.db_port or frappe.db.default_port
site = frappe.local.site or frappe.generate_hash(length=8)
self.site_slug = site.replace(".", "_")