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