作者 lyh

gx

@@ -575,13 +575,13 @@ class ProjectLogic extends BaseLogic @@ -575,13 +575,13 @@ class ProjectLogic extends BaseLogic
575 } 575 }
576 576
577 //复制数据库 577 //复制数据库
578 - public function copyMysql($new_project_id){ 578 + public function copyMysql($project_id,$new_project_id){
579 //切换数据库配置 579 //切换数据库配置
580 ProjectServer::useProject($new_project_id); 580 ProjectServer::useProject($new_project_id);
581 //创建数据库 581 //创建数据库
582 ProjectServer::createDatabase($new_project_id); 582 ProjectServer::createDatabase($new_project_id);
583 //创建表 583 //创建表
584 - $this->initTable(); 584 + $this->initTable($project_id,$new_project_id);
585 //初始数据 585 //初始数据
586 ProjectServer::saveInitParam($new_project_id); 586 ProjectServer::saveInitParam($new_project_id);
587 } 587 }
@@ -595,22 +595,21 @@ class ProjectLogic extends BaseLogic @@ -595,22 +595,21 @@ class ProjectLogic extends BaseLogic
595 */ 595 */
596 public function initTable($project_id,$news_project_id) 596 public function initTable($project_id,$news_project_id)
597 { 597 {
598 - config(['database.connections.custom_tmp_mysql.database' => 'gl_data_'.$project_id]);  
599 - $database_name = DB::connection('custom_tmp_mysql')->getDatabaseName();  
600 - $table = Schema::connection('custom_tmp_mysql')->getAllTables(); 598 + config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]);
  599 + $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
  600 + $table = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
601 $table = array_column($table, 'Tables_in_' . $database_name); 601 $table = array_column($table, 'Tables_in_' . $database_name);
602 foreach ($table as $v) { 602 foreach ($table as $v) {
603 $has_table = Schema::connection('custom_mysql')->hasTable($v); 603 $has_table = Schema::connection('custom_mysql')->hasTable($v);
604 if ($has_table) { 604 if ($has_table) {
605 continue; 605 continue;
606 } 606 }
607 - $sql = DB::connection('custom_tmp_mysql')->select("SHOW CREATE TABLE {$v}"); 607 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$v}");
608 DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']); 608 DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
609 Schema::table($v, function ($v) use ($news_project_id) { 609 Schema::table($v, function ($v) use ($news_project_id) {
610 $v->update(['project_id' => $news_project_id]); 610 $v->update(['project_id' => $news_project_id]);
611 }); 611 });
612 } 612 }
613 - config(['database.connections.custom_tmp_mysql.database' => 'gl_data_tmp']);  
614 return true; 613 return true;
615 } 614 }
616 615
@@ -83,6 +83,26 @@ return [ @@ -83,6 +83,26 @@ return [
83 ]) : [], 83 ]) : [],
84 ], 84 ],
85 85
  86 + 'custom_tmp_mysql_copy' => [
  87 + 'driver' => 'mysql',
  88 + 'url' => env('DATABASE_URL'),
  89 + 'host' => env('DB_HOST', '127.0.0.1'),
  90 + 'port' => env('DB_PORT', '3306'),
  91 + 'database' => env('DB_DATABASE_TMP', ''),
  92 + 'username' => env('DB_USERNAME', 'forge'),
  93 + 'password' => env('DB_PASSWORD', ''),
  94 + 'unix_socket' => env('DB_SOCKET', ''),
  95 + 'charset' => 'utf8mb4',
  96 + 'collation' => 'utf8mb4_unicode_ci',
  97 + 'prefix' => '',
  98 + 'prefix_indexes' => true,
  99 + 'strict' => true,
  100 + 'engine' => null,
  101 + 'options' => extension_loaded('pdo_mysql') ? array_filter([
  102 + PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
  103 + ]) : [],
  104 + ],
  105 +
86 'custom_mysql' => [ 106 'custom_mysql' => [
87 'driver' => 'mysql', 107 'driver' => 'mysql',
88 'url' => '', // DB_DATABASE_URL 108 'url' => '', // DB_DATABASE_URL