perf: Refactor OperateLogStore separator logic for database compatibility

pull/13424/head^2
feng 2024-06-14 11:26:54 +08:00 committed by Bryan
parent 1c1d839b82
commit 6c59888d77
2 changed files with 20 additions and 4 deletions

View File

@ -1,4 +1,5 @@
# ~*~ coding: utf-8 ~*~ # ~*~ coding: utf-8 ~*~
from django.conf import settings
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from audits.models import OperateLog from audits.models import OperateLog
@ -6,8 +7,22 @@ from perms.const import ActionChoices
class OperateLogStore(object): 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): def __init__(self, config):
self.model = OperateLog self.model = OperateLog

View File

@ -243,9 +243,10 @@ MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage'
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases # https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DB_OPTIONS = {} DB_OPTIONS = {}
DB_ENGINE = CONFIG.DB_ENGINE.lower()
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.{}'.format(CONFIG.DB_ENGINE.lower()), 'ENGINE': 'django.db.backends.{}'.format(DB_ENGINE),
'NAME': CONFIG.DB_NAME, 'NAME': CONFIG.DB_NAME,
'HOST': CONFIG.DB_HOST, 'HOST': CONFIG.DB_HOST,
'PORT': CONFIG.DB_PORT, 'PORT': CONFIG.DB_PORT,
@ -257,7 +258,7 @@ DATABASES = {
} }
DB_USE_SSL = CONFIG.DB_USE_SSL 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'" DB_OPTIONS['init_command'] = "SET sql_mode='STRICT_TRANS_TABLES'"
if DB_USE_SSL: if DB_USE_SSL:
DB_CA_PATH = exist_or_default(os.path.join(CERTS_DIR, 'db_ca.pem'), None) DB_CA_PATH = exist_or_default(os.path.join(CERTS_DIR, 'db_ca.pem'), None)