作者 lyh

gx批量操作关键词

@@ -239,25 +239,16 @@ class CopyProject extends Command @@ -239,25 +239,16 @@ class CopyProject extends Command
239 foreach ($tables as $table) { 239 foreach ($tables as $table) {
240 // 1. 删除目标数据库中的表 240 // 1. 删除目标数据库中的表
241 DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}"); 241 DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");
242 - // 2. 重新创建表  
243 - $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}"); 242 + // 2. 复制建表 SQL
  243 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");
244 DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']); 244 DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
245 // 3. 跳过指定的表 245 // 3. 跳过指定的表
246 if (in_array($table, ['gl_customer_visit', 'gl_customer_visit_item', 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form'])) { 246 if (in_array($table, ['gl_customer_visit', 'gl_customer_visit_item', 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form'])) {
247 continue; 247 continue;
248 } 248 }
249 -// try {  
250 - // 4. 重新插入数据  
251 - DB::connection('custom_mysql')->table($table)->insertUsing(  
252 - [], // 插入所有列  
253 - function ($query) use ($table, $project_id) {  
254 - $name = 'gl_data_' . $project_id . '.' . $table;  
255 - $query->select('*')->from("{$name}");  
256 - }  
257 - );  
258 -// }catch (\Exception $e){  
259 -// continue;  
260 -// } 249 + // 4. 原生 SQL 插入数据(完全复制)
  250 + $insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";
  251 + DB::connection('custom_mysql')->statement($insert_sql);
261 // 5. 更新 project_id(如果存在) 252 // 5. 更新 project_id(如果存在)
262 if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) { 253 if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
263 DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]); 254 DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);