Browse Source

feat: 服务启动时校验 migrations 文件是否有冲突(DEBUG_DEV)

pull/10254/head
Bai 2 years ago committed by Jiangjie.Bai
parent
commit
d402780d00
  1. 55
      apps/common/signal_handlers.py

55
apps/common/signal_handlers.py

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
import logging
import os
import re
from collections import defaultdict
@ -12,6 +13,7 @@ from django.dispatch import receiver
from jumpserver.utils import get_current_request
from .local import thread_local
from .signals import django_ready
pattern = re.compile(r'FROM `(\w+)`')
logger = logging.getLogger("jumpserver.common")
@ -123,3 +125,56 @@ if settings.DEBUG_DEV:
request_finished.connect(on_request_finished_logging_db_query)
else:
request_finished.connect(on_request_finished_release_local)
@receiver(django_ready)
def check_migrations_file_prefix_conflict(*args, **kwargs):
if not settings.DEBUG_DEV:
return
from jumpserver.const import BASE_DIR
print('>>> Check migrations file prefix conflict')
# 指定 app 目录
_dir = BASE_DIR
# 获取所有子目录
sub_dirs = next(os.walk(_dir))[1]
# 记录冲突的文件,元素为 (subdir, file1, file2)
conflict_files = []
# 遍历每个子目录
for subdir in sub_dirs:
# 拼接 migrations 目录路径
migrations_dir = os.path.join(_dir, subdir, 'migrations')
# 判断是否存在 migrations 目录
if not os.path.exists(migrations_dir):
continue
# 获取所有文件名
files = os.listdir(migrations_dir)
# 遍历每个文件名
prefix_file_map = dict()
for file in files:
file = str(file)
# 判断是否为 Python 文件
if not file.endswith('.py'):
continue
if 'squashed' in file:
continue
# file 为文件名
file_prefix = file.split('_')[0]
if file_prefix in prefix_file_map.keys():
conflict_files.append((subdir, file, prefix_file_map.get(file_prefix)))
else:
prefix_file_map[file_prefix] = file
print('='*80)
print(f'Conflict count:({len(conflict_files)})')
for conflict_file in conflict_files:
msg_dir = '{:<15}'.format(conflict_file[0])
msg_split = '=> '
msg_left = msg_dir
msg_right1 = msg_split + '{:<80}'.format(conflict_file[1])
msg_right2 = ' ' * len(msg_left) + msg_split + conflict_file[2]
print(f'{msg_left}{msg_right1}\n{msg_right2}\n')
print('='*80)

Loading…
Cancel
Save