MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / sync_db_table

Function sync_db_table

script/init_db.py:122–295  ·  view source on GitHub ↗
(table, dst_db_name, src_db_obj)

Source from the content-addressed store, hash-verified

120
121# 同步单个表
122def sync_db_table(table, dst_db_name, src_db_obj):
123 try:
124
125 if src_db_obj == None:
126 src_field = get_table_byjson(table, dst_db_name)
127 else:
128 src_field = get_table_info(src_db_obj, table)
129
130 if not src_field:
131 return False
132
133 # 检查表存在
134 db_file = '{}/{}'.format(dst_dir, dst_db_name)
135 dst_obj = get_db_obj(db_file)
136 if not check_exists_table(dst_obj, table):
137 print_x('{}数据库 {} 表不存在,准备创建数据库'.format(dst_db_name, table))
138 res = dst_obj.execute(src_field['sql'])
139 try:
140 # 数据库文件损坏
141 if str(res).find('file is not a database') != -1:
142 print_x('数据库文件损坏,尝试重新创建!')
143
144 if os.path.exists(db_file):
145 print_x('备份已损坏的数据库文件:{}'.format(db_file))
146 os.rename(db_file, '{}.{}.bak'.format(db_file, time.strftime('%Y%m%d%H%M%S')))
147
148 return sync_db_all(table,dst_db_name,src_db_obj)
149 except:pass
150
151 if str(res).find("error") != -1:
152 print_x("创建表 {} 错误:{}".format(table, res))
153 return False
154
155 # 修复字段
156 dst_field = get_table_info(dst_obj, table)
157 if not dst_field:
158 return False
159
160 is_alert = False
161 for field in src_field['field']:
162 if not field in dst_field['field']:
163 continue
164
165 dst_fs = dst_field['field'][field]
166 src_fs = src_field['field'][field]
167
168 d4 = dst_fs[4]
169 s4 = src_fs[4]
170
171 try:
172 d4 = int(d4)
173 except:pass
174
175 try:
176 s4 = int(s4)
177 except:pass
178
179 if d4 == '0': d4 = 0

Callers 1

sync_db_allFunction · 0.85

Calls 15

get_table_byjsonFunction · 0.85
get_table_infoFunction · 0.85
get_db_objFunction · 0.85
check_exists_tableFunction · 0.85
sync_db_allFunction · 0.85
timeMethod · 0.80
executemanyMethod · 0.80
print_xFunction · 0.70
setFunction · 0.50
formatMethod · 0.45
executeMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected