2025-06-25 09:03:35 +08:00
|
|
|
|
import shutil
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
from Common import *
|
2025-06-25 11:55:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-25 09:03:35 +08:00
|
|
|
|
class UpdateSql():
|
|
|
|
|
|
def __init__(self, install_path):
|
|
|
|
|
|
self.root_path = install_path
|
|
|
|
|
|
self.mariadb_data_path = os.path.join(install_path, 'mariadb', 'data')
|
2025-06-25 11:55:53 +08:00
|
|
|
|
self.init_sql = get_resource_path(os.path.join('datas', 'init.sql'))
|
|
|
|
|
|
self.__set_replace()
|
|
|
|
|
|
|
|
|
|
|
|
def __set_replace(self):
|
|
|
|
|
|
with open(self.init_sql, 'r', encoding='utf-8') as file:
|
|
|
|
|
|
lines = file.readlines()
|
|
|
|
|
|
modified = False
|
|
|
|
|
|
new_lines = []
|
|
|
|
|
|
for line in lines:
|
|
|
|
|
|
if 'CREATE TABLE' in line and 'IF NOT EXISTS' not in line:
|
|
|
|
|
|
new_line = line.replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS ')
|
|
|
|
|
|
new_lines.append(new_line)
|
|
|
|
|
|
modified = True
|
|
|
|
|
|
else:
|
|
|
|
|
|
new_lines.append(line)
|
|
|
|
|
|
|
|
|
|
|
|
# 如果有修改,则写回文件
|
|
|
|
|
|
if modified:
|
|
|
|
|
|
with open(self.init_sql, 'w', encoding='utf-8') as file:
|
|
|
|
|
|
file.writelines(new_lines)
|
2025-06-25 09:03:35 +08:00
|
|
|
|
|
|
|
|
|
|
def init_db(self):
|
2025-06-25 11:55:53 +08:00
|
|
|
|
shutil.copy(str(self.init_sql), self.mariadb_data_path)
|
|
|
|
|
|
command2 = fr'{self.root_path}\mariadb\bin\mysql --no-defaults -u root -p{mariadb_passowrd} -P {mariadb_port} {mariadb_init_schema} < {self.mariadb_data_path}'
|
2025-06-25 09:03:35 +08:00
|
|
|
|
with os.popen(command2) as stream:
|
|
|
|
|
|
res2 = stream.read()
|
|
|
|
|
|
logger.info(res2)
|
|
|
|
|
|
os.remove(self.init_sql)
|
2025-06-25 11:55:53 +08:00
|
|
|
|
os.remove(self.mariadb_data_path)
|
2025-06-25 09:03:35 +08:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
current_dir = os.getcwd()
|
|
|
|
|
|
logger.info("开始更新")
|
|
|
|
|
|
UpdateSql(current_dir).init_db()
|
|
|
|
|
|
logger.info("完成更新,3秒钟后退出")
|
|
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|