You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.7 KiB
96 lines
3.7 KiB
import os |
|
import pymysql |
|
from dingxin_toolbox_drf.settings import BASE_DIR |
|
from update.utils.git_util import GitUtil |
|
|
|
|
|
class DbCompare: |
|
def __init__(self, target_ip, short_release): |
|
self.target_server = target_ip |
|
self.target_user = 'test' |
|
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') |
|
|
|
# 生成对比文件 |
|
def gen_diff_file(self): |
|
# serv_host = '127.0.0.1' |
|
# serv_user = 'dingxin' |
|
# serv_pwd = 'cine123456' |
|
# serv_port = '3306' |
|
|
|
serv_host = '172.16.3.112' |
|
serv_user = 'test' |
|
serv_pwd = 'cine123456' |
|
serv_port = '3306' |
|
|
|
server_1 = f'--server1={serv_user}:{serv_pwd}@{serv_host}:{serv_port}' |
|
server_2 = f'--server2={self.target_user}:{self.target_pwd}@{self.target_server}:{self.target_port}' |
|
|
|
git_util = GitUtil(self.target_release) |
|
db_name = git_util.get_db_name() |
|
|
|
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 = os.system(cmd) |
|
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 |
|
|
|
# 生成新表sql |
|
def gen_create_file(self): |
|
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() |
|
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}`'): |
|
create_table_sql_list.append(sql + ';\n\n') |
|
f.close() |
|
with open(self.create_sql_path, 'w', encoding='utf-8') as f: |
|
f.writelines(create_table_sql_list) |
|
f.close() |
|
return self.create_sql_path |
|
|
|
# 执行差异 |
|
def exec_diff_sql(self): |
|
self.gen_diff_file() |
|
self.gen_create_file() |
|
diff_cmd = f'mysql -h{self.target_server} -P{self.target_port} -u{self.target_user} -p{self.target_pwd} < {self.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) |
|
create_result = os.system(create_cmd) |
|
print(create_result)
|
|
|