完成数据库对比文件生成的脚本

main
RogerWork 10 months ago
parent 32339fe0e4
commit db9471e2e0
  1. 75
      update/utils/db_compare.py
  2. 27
      update/utils/git_util.py
  3. 16
      update/views.py

@ -1,5 +1,6 @@
import os import os
import pymysql import pymysql
from update.utils.git_util import GitUtil
from dingxin_toolbox_drf.settings import BASE_DIR from dingxin_toolbox_drf.settings import BASE_DIR
from update.utils.git_util import GitUtil from update.utils.git_util import GitUtil
@ -11,12 +12,16 @@ class DbCompare:
self.target_pwd = 'cine123456' self.target_pwd = 'cine123456'
self.target_port = '3306' self.target_port = '3306'
self.target_release = short_release self.target_release = short_release
self.cine_sql_path = os.path.join(BASE_DIR, 'dx', 'sql', self.target_release + '.sql') self.cine_sql_path = os.path.join(BASE_DIR, 'dx', 'sql', GitUtil(short_release).get_db_name() + '.sql')
self.diff_sql_path = os.path.join(BASE_DIR, 'dx', 'temp', 'diff_' + self.target_server + '.sql') self.diff_sql_path = os.path.join(BASE_DIR, 'dx', 'temp', self.target_server + '_diff.sql')
self.create_sql_path = os.path.join(BASE_DIR, 'dx', 'temp', 'create_' + self.target_server + '.sql') self.clear_diff_sql_path = os.path.join(BASE_DIR, 'dx', 'temp', self.target_server + '_diff_clear.sql')
self.create_sql_path = os.path.join(BASE_DIR, 'dx', 'temp', self.target_server + '_create.sql')
# 生成对比文件 # 生成对比文件
def gen_diff_file(self): def gen_diff_file(self):
if os.path.exists(self.diff_sql_path):
os.remove(self.diff_sql_path)
# serv_host = '127.0.0.1' # serv_host = '127.0.0.1'
# serv_user = 'dingxin' # serv_user = 'dingxin'
# serv_pwd = 'cine123456' # serv_pwd = 'cine123456'
@ -40,45 +45,63 @@ class DbCompare:
print(result) print(result)
return self.diff_sql_path return self.diff_sql_path
# 生成差异sql # 针对降级的情况,需要屏蔽掉语句中的drop语句,此处可能有错误的风险,待验证
# def gen_diff_sql(self): def remove_drop(self):
# diff_sql_list = [] # 如果清理后的文件存在则删除
# diff_file = open(self.diff_sql_path, 'w', encoding='utf-8') if os.path.exists(self.clear_diff_sql_path):
# with open(self.diff_sql_tmp_path, 'r', encoding='utf-8') as tmp_f: os.remove(self.clear_diff_sql_path)
# for line in tmp_f.readlines(): # 打开生成的差异sql,如果遇到ALTER语句则进入alter模式,将ALTER下面的语句写入temp中,如果是DROP就跳过,当遇到换行时就结束alter模式,把temp写入主列表
# if line.startswith('#'): with open(self.diff_sql_path, 'r', encoding='utf-8') as f:
# continue sql_list = []
# if line == '': temp = []
# continue alter = 0
# diff_file.write(line) for line in f.readlines():
# tmp_f.close() if line.strip().startswith('ALTER TABLE'):
# diff_file.close() temp.append(line)
# with open(self.diff_sql_path, 'r', encoding='utf-8') as diff_f: alter = 1
# content = diff_f.read() elif line.strip().startswith('DROP INDEX'):
# temp = content.split(';') pass
# for sql in temp: elif line.strip().startswith('DROP COLUMN'):
# diff_sql_list.append(sql + ';') pass
# diff_f.close() elif line.strip() == '':
# print(diff_sql_list) if len(temp) > 1:
# return diff_sql_list sql_list.extend(temp)
sql_list.extend('\n')
temp = []
alter = 0
else:
if alter == 1:
temp.append(line)
else:
sql_list.append(line)
f.close()
# 将清洗过的sql列表写入文件
with open(self.clear_diff_sql_path, 'w', encoding='utf-8') as f:
f.writelines(sql_list)
f.close()
# 生成新表sql # 生成新表sql
def gen_create_file(self): def gen_create_file(self):
# 如果有已生成的文件则删除
if os.path.exists(self.create_sql_path):
os.remove(self.create_sql_path)
# 在生成的差异文件中查找原始版本没有,目标版本中存在的表明
create_sql_list = [] create_sql_list = []
with open(self.diff_sql_path, 'r', encoding='utf-8') as f: with open(self.diff_sql_path, 'r', encoding='utf-8') as f:
for line in f.readlines(): for line in f.readlines():
if line.startswith('# TABLE'): if line.startswith('# TABLE'):
create_sql_list.append(line.split(':')[1].strip()) create_sql_list.append(line.split(':')[1].strip())
print(create_sql_list)
f.close() f.close()
# 通过上步中找到的表名,在目标版本的cine.sql中查找创建表的sql语句,并组成sql语句列表
create_table_sql_list = ['USE `cine`;\n\n'] create_table_sql_list = ['USE `cine`;\n\n']
with open(self.cine_sql_path, 'r', encoding='utf-8') as f: with open(self.cine_sql_path, 'r', encoding='utf-8') as f:
content = f.read() content = f.read()
for sql in content.split(';\n'): for sql in content.split(';\n'):
for table in create_sql_list: for table in create_sql_list:
if sql.find(f'CREATE TABLE `{table}`'): if f'CREATE TABLE `{table}`' in sql:
create_table_sql_list.append(sql + ';\n\n') create_table_sql_list.append(sql + ';\n\n')
f.close() f.close()
# 将上步中的sql语句列表些入create.sql文件中
with open(self.create_sql_path, 'w', encoding='utf-8') as f: with open(self.create_sql_path, 'w', encoding='utf-8') as f:
f.writelines(create_table_sql_list) f.writelines(create_table_sql_list)
f.close() f.close()

@ -22,6 +22,7 @@ class GitUtil:
self.short_release = short_release self.short_release = short_release
self.local_code_path = os.path.join(settings.BASE_DIR, r'dx\code') self.local_code_path = os.path.join(settings.BASE_DIR, r'dx\code')
self.cine_sql_path = os.path.join(settings.BASE_DIR, r'dx\sql') self.cine_sql_path = os.path.join(settings.BASE_DIR, r'dx\sql')
self.db_name = self.get_db_name(self.short_release)
self.db_config = { self.db_config = {
'host': '172.16.3.112', 'host': '172.16.3.112',
'user': 'test', 'user': 'test',
@ -70,9 +71,8 @@ class GitUtil:
# 复制文cine.sql到本地路径 # 复制文cine.sql到本地路径
def copy_cine_sql(self): def copy_cine_sql(self):
self.checkout_release() self.checkout_release()
short_ver = 'cine_' + self.short_release
org_path = os.path.join(self.local_code_path, 'install', 'cine.sql') org_path = os.path.join(self.local_code_path, 'install', 'cine.sql')
target_path = os.path.join(self.cine_sql_path, f'{self.short_release}.sql') target_path = os.path.join(self.cine_sql_path, f'{self.db_name}.sql')
# if os.path.exists(target_path): # if os.path.exists(target_path):
# os.remove(target_path) # os.remove(target_path)
with open(target_path, 'w', encoding='utf-8') as target_file: with open(target_path, 'w', encoding='utf-8') as target_file:
@ -82,12 +82,12 @@ class GitUtil:
for line in lines: for line in lines:
if line.startswith('CREATE DATABASE'): if line.startswith('CREATE DATABASE'):
print('CREATE DATABASE') print('CREATE DATABASE')
target_lines.append(f'DROP DATABASE IF EXISTS `{short_ver}`;\r\n') target_lines.append(f'DROP DATABASE IF EXISTS `{self.db_name}`;\r\n')
target_lines.append( target_lines.append(
f'CREATE DATABASE /*!32312 IF NOT EXISTS*/`{short_ver}` /*!40100 DEFAULT CHARACTER SET utf8 */;\r\n') f'CREATE DATABASE /*!32312 IF NOT EXISTS*/`{self.db_name}` /*!40100 DEFAULT CHARACTER SET utf8 */;\r\n')
elif line.startswith('USE `cine`'): elif line.startswith('USE `cine`'):
print('USE `cine`') print('USE `cine`')
target_lines.append(f'USE `{short_ver}`;\r\n') target_lines.append(f'USE `{self.db_name}`;\r\n')
else: else:
target_lines.append(line) target_lines.append(line)
f.close() f.close()
@ -96,7 +96,7 @@ class GitUtil:
# 检查cine.sql是否是最新的 # 检查cine.sql是否是最新的
def check_cine_sql(self): def check_cine_sql(self):
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.short_release}.sql')): if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.db_name}.sql')):
saved_md5 = self.get_md5(sql_path) saved_md5 = self.get_md5(sql_path)
new_md5 = self.get_md5(os.path.join(self.local_code_path, 'install', 'cine.sql')) new_md5 = self.get_md5(os.path.join(self.local_code_path, 'install', 'cine.sql'))
if saved_md5 == new_md5: if saved_md5 == new_md5:
@ -111,7 +111,7 @@ class GitUtil:
return False return False
def write_sql(self): def write_sql(self):
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.short_release}.sql')): if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.db_name}.sql')):
execute_sql_list = [] execute_sql_list = []
with open(sql_path, 'r', encoding='utf-8') as sql_file: with open(sql_path, 'r', encoding='utf-8') as sql_file:
content = sql_file.read() content = sql_file.read()
@ -135,15 +135,22 @@ class GitUtil:
# print(result, ext_sql) # print(result, ext_sql)
def write_cine_sql_by_mysql(self): def write_cine_sql_by_mysql(self):
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.short_release}.sql')): if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.db_name}.sql')):
cmd = f'mysql -h172.16.3.112 -P3306 -utest -pcine123456 < {sql_path}' cmd = f'mysql -h172.16.3.112 -P3306 -utest -pcine123456 < {sql_path}'
r = os.system(cmd) r = os.system(cmd)
print(type(r)) print(type(r))
print(r) print(r)
# 获取数据库名称 # 获取数据库名称
def get_db_name(self): def get_db_name(self, _release=''):
return 'cine_' + self.short_release if _release == '':
release = self.short_release
else:
release = _release
print(release)
rel = Release.objects.filter(short_release=release).first()
print(rel)
return 'cine_' + str(rel.ver_id)
# 写入cine.sql # 写入cine.sql
def handle_create_cine(self): def handle_create_cine(self):

@ -85,16 +85,14 @@ def write_git_version_to_db(request):
def write_cine_sql(request): def write_cine_sql(request):
git_util = GitUtil('2.0.33.0338_Release') git_util = GitUtil('2.0.33.0333_Release')
git_util.handle_create_cine() git_util.handle_create_cine()
return JsonResponse({'result': 'success'}) return JsonResponse({'result': 'success'})
def compare_db_structor(request): def compare_db_structor(request):
git_util = GitUtil('') db_compare = DbCompare('172.16.3.205', '2.0.33.0338_Release')
# # git_util.checkout_release('origin/2.0.33.0338_Release') db_compare.gen_diff_file()
# git_util.check_cine_sql('origin/2.0.33.0338_Release') db_compare.remove_drop()
# git_util.copy_cine_sql('origin/2.0.33.0338_Release') db_compare.gen_create_file()
# return JsonResponse({'result': 'success'})
# db_compare = DbCompare('172.16.3.111', 'origin/2.0.33.0338_Release')
# path = db_compare.gen_diff_file()
# print(path)

Loading…
Cancel
Save