更新updatesql的逻辑
This commit is contained in:
parent
b0cb2bcc3a
commit
a664a3584f
|
|
@ -2,25 +2,41 @@ import shutil
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from Common import *
|
from Common import *
|
||||||
from loguru import logger
|
|
||||||
|
|
||||||
class UpdateSql():
|
class UpdateSql():
|
||||||
def __init__(self, install_path):
|
def __init__(self, install_path):
|
||||||
self.service_name = 'ComacMariaDB'
|
|
||||||
self.root_path = install_path
|
self.root_path = install_path
|
||||||
self.mariadb_data_path = os.path.join(install_path, 'mariadb', 'data')
|
self.mariadb_data_path = os.path.join(install_path, 'mariadb', 'data')
|
||||||
self.mariadb_port = mariadb_port
|
self.init_sql = get_resource_path(os.path.join('datas', 'init.sql'))
|
||||||
self.passowrd = mariadb_passowrd
|
self.__set_replace()
|
||||||
self.init_schema = mariadb_init_schema
|
|
||||||
self.init_sql = os.path.join(install_path, 'mariadb', 'data', 'init.sql')
|
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)
|
||||||
|
|
||||||
def init_db(self):
|
def init_db(self):
|
||||||
new_path = get_resource_path(os.path.join('datas', 'init.sql'))
|
shutil.copy(str(self.init_sql), self.mariadb_data_path)
|
||||||
shutil.copy(str(new_path), 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}'
|
||||||
command2 = fr'{self.root_path}\mariadb\bin\mysql --no-defaults -u root -p{self.passowrd} -P {self.mariadb_port} {self.init_schema} < {self.init_sql}'
|
|
||||||
with os.popen(command2) as stream:
|
with os.popen(command2) as stream:
|
||||||
res2 = stream.read()
|
res2 = stream.read()
|
||||||
logger.info(res2)
|
logger.info(res2)
|
||||||
os.remove(self.init_sql)
|
os.remove(self.init_sql)
|
||||||
|
os.remove(self.mariadb_data_path)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue