作者 lyh

gx复制项目数据库

@@ -45,10 +45,6 @@ class CopyOldProject extends Command @@ -45,10 +45,6 @@ class CopyOldProject extends Command
45 45
46 //复制数据库 46 //复制数据库
47 public function copyMysql($project_id,$new_project_id){ 47 public function copyMysql($project_id,$new_project_id){
48 - //切换数据库配置  
49 - $project = ProjectServer::useProject($new_project_id);  
50 - //创建数据库  
51 - ProjectServer::createDatabase($project);  
52 //创建表 48 //创建表
53 $this->initTable($project_id,$new_project_id); 49 $this->initTable($project_id,$new_project_id);
54 } 50 }
@@ -209,10 +209,6 @@ class CopyProject extends Command @@ -209,10 +209,6 @@ class CopyProject extends Command
209 209
210 //复制数据库 210 //复制数据库
211 public function copyMysql($project_id,$new_project_id){ 211 public function copyMysql($project_id,$new_project_id){
212 - //切换数据库配置  
213 - $project = ProjectServer::useProject($new_project_id);  
214 - //创建数据库  
215 - ProjectServer::createDatabase($project);  
216 //创建表 212 //创建表
217 $this->initTable($project_id,$new_project_id); 213 $this->initTable($project_id,$new_project_id);
218 } 214 }
@@ -224,29 +220,43 @@ class CopyProject extends Command @@ -224,29 +220,43 @@ class CopyProject extends Command
224 * @method :post 220 * @method :post
225 * @time :2023/12/11 10:09 221 * @time :2023/12/11 10:09
226 */ 222 */
227 - public function initTable($project_id,$news_project_id) 223 + public function initTable($project_id, $news_project_id)
228 { 224 {
229 - config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]); 225 + // 设置源数据库
  226 + config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]);
230 $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName(); 227 $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
  228 + // 获取源数据库的所有表
231 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); 229 $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
232 $tables = array_column($tables, 'Tables_in_' . $database_name); 230 $tables = array_column($tables, 'Tables_in_' . $database_name);
233 foreach ($tables as $table) { 231 foreach ($tables as $table) {
  232 + // 目标数据库是否存在该表
234 $has_table = Schema::connection('custom_mysql')->hasTable($table); 233 $has_table = Schema::connection('custom_mysql')->hasTable($table);
235 - if (!$has_table) {  
236 - $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");  
237 - DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']); 234 + if ($has_table) {
  235 + // 1. 删除目标数据库中的表
  236 + DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");
238 } 237 }
239 - if($table == 'gl_customer_visit' || $table == 'gl_customer_visit_item' || $table == 'gl_inquiry_other' || $table == 'gl_inquiry_form_data' || $table == 'gl_inquiry_form'){ 238 + // 2. 重新创建表
  239 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
  240 + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
  241 + // 3. 跳过指定的表
  242 + if (in_array($table, [
  243 + 'gl_customer_visit',
  244 + 'gl_customer_visit_item',
  245 + 'gl_inquiry_other',
  246 + 'gl_inquiry_form_data',
  247 + 'gl_inquiry_form'
  248 + ])) {
240 continue; 249 continue;
241 } 250 }
242 - DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据 251 + // 4. 重新插入数据
243 DB::connection('custom_mysql')->table($table)->insertUsing( 252 DB::connection('custom_mysql')->table($table)->insertUsing(
244 - [], // 列名数组,留空表示插入所有列  
245 - function ($query) use ($table,$project_id) {  
246 - $name = 'gl_data_'.$project_id.'.'.$table; 253 + [], // 插入所有列
  254 + function ($query) use ($table, $project_id) {
  255 + $name = 'gl_data_' . $project_id . '.' . $table;
247 $query->select('*')->from("{$name}"); 256 $query->select('*')->from("{$name}");
248 } 257 }
249 ); 258 );
  259 + // 5. 更新 project_id(如果存在)
250 if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) { 260 if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
251 DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]); 261 DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
252 } 262 }