|
|
|
@ -1,4 +1,5 @@ |
|
|
|
|
import os |
|
|
|
|
import subprocess |
|
|
|
|
import pymysql |
|
|
|
|
from update.utils.git_util import GitUtil |
|
|
|
|
from dingxin_toolbox_drf.settings import BASE_DIR |
|
|
|
@ -20,7 +21,9 @@ class DbCompare: |
|
|
|
|
# 生成对比文件 |
|
|
|
|
def gen_diff_file(self): |
|
|
|
|
if os.path.exists(self.diff_sql_path): |
|
|
|
|
print('remove diff_sql_path') |
|
|
|
|
os.remove(self.diff_sql_path) |
|
|
|
|
print(os.path.exists(self.diff_sql_path)) |
|
|
|
|
|
|
|
|
|
# serv_host = '127.0.0.1' |
|
|
|
|
# serv_user = 'dingxin' |
|
|
|
@ -41,15 +44,18 @@ class DbCompare: |
|
|
|
|
cmd = f'mysqldiff {server_1} {server_2} --changes-for=server2 --difftype=sql --force --quiet --skip-table-options {db_name}:cine > {self.diff_sql_path}' |
|
|
|
|
print(cmd) |
|
|
|
|
|
|
|
|
|
# result = subprocess.run(cmd) |
|
|
|
|
result = os.system(cmd) |
|
|
|
|
print(result) |
|
|
|
|
print('gen_diff_file', result) |
|
|
|
|
return self.diff_sql_path |
|
|
|
|
|
|
|
|
|
# 针对降级的情况,需要屏蔽掉语句中的drop语句,此处可能有错误的风险,待验证 |
|
|
|
|
def remove_drop(self): |
|
|
|
|
# 如果清理后的文件存在则删除 |
|
|
|
|
if os.path.exists(self.clear_diff_sql_path): |
|
|
|
|
print('remove clear_diff_sql_path') |
|
|
|
|
os.remove(self.clear_diff_sql_path) |
|
|
|
|
print(os.path.exists(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 = [] |
|
|
|
@ -66,6 +72,12 @@ class DbCompare: |
|
|
|
|
pass |
|
|
|
|
elif line.strip().startswith('[PASS]'): |
|
|
|
|
pass |
|
|
|
|
elif line.strip().startswith('+'): |
|
|
|
|
pass |
|
|
|
|
elif line.strip().startswith('-'): |
|
|
|
|
pass |
|
|
|
|
elif line.strip().startswith('@'): |
|
|
|
|
pass |
|
|
|
|
elif line.strip().startswith('ALTER DEFINER'): |
|
|
|
|
temp.append(line) |
|
|
|
|
alter_definer = 1 |
|
|
|
@ -123,6 +135,6 @@ class DbCompare: |
|
|
|
|
diff_cmd = f'mysql -h{self.target_server} -P{self.target_port} -u{self.target_user} -p{self.target_pwd} < {self.clear_diff_sql_path}' |
|
|
|
|
create_cmd = f'mysql -h{self.target_server} -P{self.target_port} -u{self.target_user} -p{self.target_pwd} < {self.create_sql_path}' |
|
|
|
|
diff_result = os.system(diff_cmd) |
|
|
|
|
print(diff_result) |
|
|
|
|
print('diff_result', diff_result) |
|
|
|
|
create_result = os.system(create_cmd) |
|
|
|
|
print(create_result) |
|
|
|
|
print('create_result', create_result) |
|
|
|
|