作者 lyh

gx

@@ -605,22 +605,23 @@ class ProjectLogic extends BaseLogic @@ -605,22 +605,23 @@ class ProjectLogic extends BaseLogic
605 { 605 {
606 config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]); 606 config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]);
607 $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName(); 607 $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
608 - $table = Schema::connection('custom_tmp_mysql_copy')->getAllTables();  
609 - $table = array_column($table, 'Tables_in_' . $database_name);  
610 - foreach ($table as $v) {  
611 - $has_table = Schema::connection('custom_mysql')->hasTable($v);  
612 - if ($has_table) {  
613 - continue; 608 + $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
  609 + $tables = array_column($tables, 'Tables_in_' . $database_name);
  610 + foreach ($tables as $table) {
  611 + $has_table = Schema::connection('custom_mysql')->hasTable($table);
  612 + if (!$has_table) {
  613 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
  614 + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
614 } 615 }
615 - $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$v}");  
616 - DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);  
617 - }  
618 - foreach ($table as $v1){  
619 - // 执行同步数据的操作  
620 - DB::connection('custom_mysql')->insert("INSERT INTO {$v1} SELECT * FROM custom_tmp_mysql_copy.{$v1}");  
621 -  
622 - if (Schema::connection('custom_mysql')->hasColumn($v1, 'project_id')) {  
623 - DB::connection('custom_mysql')->table($v1)->update(['project_id' => $news_project_id]); 616 + DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据
  617 + DB::connection('custom_mysql')->table($table)->insertUsing(
  618 + [], // 列名数组,留空表示插入所有列
  619 + function ($query) use ($table) {
  620 + $query->select('*')->from("custom_tmp_mysql_copy.{$table}");
  621 + }
  622 + );
  623 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  624 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
624 } 625 }
625 } 626 }
626 return true; 627 return true;