From 6c59888d777ee0098fe79aafc3978a785eae9ecb Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Fri, 14 Jun 2024 11:26:54 +0800 Subject: [PATCH] perf: Refactor OperateLogStore separator logic for database compatibility --- apps/audits/backends/db.py | 19 +++++++++++++++++-- apps/jumpserver/settings/base.py | 5 +++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/apps/audits/backends/db.py b/apps/audits/backends/db.py index e8fe43c22..9e3143c9e 100644 --- a/apps/audits/backends/db.py +++ b/apps/audits/backends/db.py @@ -1,4 +1,5 @@ # ~*~ coding: utf-8 ~*~ +from django.conf import settings from django.utils.translation import gettext_lazy as _ from audits.models import OperateLog @@ -6,8 +7,22 @@ from perms.const import ActionChoices class OperateLogStore(object): - # 用不可见字符分割前后数据,节省存储-> diff: {'key': 'before\0after'} - SEP = '\0' + + @staticmethod + def get_separator(): + """ + 用不可见字符分割前后数据 + 根据数据库引擎类型返回适当的分隔符。 + PostgreSQL 数据库使用 Unicode 单元分隔符 '\u001f', + 其他数据库使用空字符 '\0' 作为分隔符。 + """ + db_engine = settings.DB_ENGINE + if db_engine == 'postgresql': + return '\u001f' + else: + return '\0' + + SEP = get_separator() def __init__(self, config): self.model = OperateLog diff --git a/apps/jumpserver/settings/base.py b/apps/jumpserver/settings/base.py index 64ae85121..77348674f 100644 --- a/apps/jumpserver/settings/base.py +++ b/apps/jumpserver/settings/base.py @@ -243,9 +243,10 @@ MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage' # https://docs.djangoproject.com/en/1.10/ref/settings/#databases DB_OPTIONS = {} +DB_ENGINE = CONFIG.DB_ENGINE.lower() DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.{}'.format(CONFIG.DB_ENGINE.lower()), + 'ENGINE': 'django.db.backends.{}'.format(DB_ENGINE), 'NAME': CONFIG.DB_NAME, 'HOST': CONFIG.DB_HOST, 'PORT': CONFIG.DB_PORT, @@ -257,7 +258,7 @@ DATABASES = { } DB_USE_SSL = CONFIG.DB_USE_SSL -if CONFIG.DB_ENGINE.lower() == 'mysql': +if DB_ENGINE == 'mysql': DB_OPTIONS['init_command'] = "SET sql_mode='STRICT_TRANS_TABLES'" if DB_USE_SSL: DB_CA_PATH = exist_or_default(os.path.join(CERTS_DIR, 'db_ca.pem'), None)