作者 刘锟

Merge remote-tracking branch 'origin/master' into akun

@@ -50,7 +50,6 @@ class CopyProject extends Command @@ -50,7 +50,6 @@ class CopyProject extends Command
50 continue; 50 continue;
51 } 51 }
52 foreach ($list as $item){ 52 foreach ($list as $item){
53 - DB::beginTransaction();  
54 try { 53 try {
55 $old_project_id = $item['data']['project_id']; 54 $old_project_id = $item['data']['project_id'];
56 $this->output('CopyProjectJob start, project_id: ' . $old_project_id); 55 $this->output('CopyProjectJob start, project_id: ' . $old_project_id);
@@ -70,9 +69,7 @@ class CopyProject extends Command @@ -70,9 +69,7 @@ class CopyProject extends Command
70 $this->output('CopyProjectJob end, old project_id: ' . $old_project_id . ', new project_id: ' . $project_id); 69 $this->output('CopyProjectJob end, old project_id: ' . $old_project_id . ', new project_id: ' . $project_id);
71 $item->status = NoticeLog::STATUS_SUCCESS; 70 $item->status = NoticeLog::STATUS_SUCCESS;
72 $item->save(); 71 $item->save();
73 - DB::commit();  
74 }catch (\Exception $e){ 72 }catch (\Exception $e){
75 - DB::rollBack();  
76 echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL . date('Y-m-d H:i:s'); 73 echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL . date('Y-m-d H:i:s');
77 errorLog('项目初始化失败', $item, $e); 74 errorLog('项目初始化失败', $item, $e);
78 $item->status = NoticeLog::STATUS_FAIL; 75 $item->status = NoticeLog::STATUS_FAIL;
@@ -232,33 +229,27 @@ class CopyProject extends Command @@ -232,33 +229,27 @@ class CopyProject extends Command
232 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); 229 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
233 $tables = array_column($tables, 'Tables_in_' . $database_name); 230 $tables = array_column($tables, 'Tables_in_' . $database_name);
234 foreach ($tables as $table) { 231 foreach ($tables as $table) {
235 - // 目标数据库是否存在该表  
236 - $has_table = Schema::connection('custom_mysql')->hasTable($table);  
237 - if ($has_table) {  
238 - // 1. 删除目标数据库中的表  
239 - DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");  
240 - } 232 + // 1. 删除目标数据库中的表
  233 + DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");
241 // 2. 重新创建表 234 // 2. 重新创建表
242 $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}"); 235 $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
243 DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']); 236 DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
244 // 3. 跳过指定的表 237 // 3. 跳过指定的表
245 - if (in_array($table, [  
246 - 'gl_customer_visit',  
247 - 'gl_customer_visit_item',  
248 - 'gl_inquiry_other',  
249 - 'gl_inquiry_form_data',  
250 - 'gl_inquiry_form'  
251 - ])) { 238 + if (in_array($table, ['gl_customer_visit', 'gl_customer_visit_item', 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form'])) {
  239 + continue;
  240 + }
  241 + try {
  242 + // 4. 重新插入数据
  243 + DB::connection('custom_mysql')->table($table)->insertUsing(
  244 + [], // 插入所有列
  245 + function ($query) use ($table, $project_id) {
  246 + $name = 'gl_data_' . $project_id . '.' . $table;
  247 + $query->select('*')->from("{$name}");
  248 + }
  249 + );
  250 + }catch (\Exception $e){
252 continue; 251 continue;
253 } 252 }
254 - // 4. 重新插入数据  
255 - DB::connection('custom_mysql')->table($table)->insertUsing(  
256 - [], // 插入所有列  
257 - function ($query) use ($table, $project_id) {  
258 - $name = 'gl_data_' . $project_id . '.' . $table;  
259 - $query->select('*')->from("{$name}");  
260 - }  
261 - );  
262 // 5. 更新 project_id(如果存在) 253 // 5. 更新 project_id(如果存在)
263 if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) { 254 if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
264 DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]); 255 DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);