作者 lyh

gx

@@ -53,7 +53,6 @@ class InitProject extends Command @@ -53,7 +53,6 @@ class InitProject extends Command
53 echo 'start:' . $item['id'] . PHP_EOL; 53 echo 'start:' . $item['id'] . PHP_EOL;
54 try { 54 try {
55 $project = Project::find($item['data']['project_id']); 55 $project = Project::find($item['data']['project_id']);
56 -  
57 $project_logic = new ProjectLogic(); 56 $project_logic = new ProjectLogic();
58 //初始化数据库 57 //初始化数据库
59 if(!empty($project['mysql_id'])){ 58 if(!empty($project['mysql_id'])){
@@ -34,6 +34,7 @@ use Hashids\Hashids; @@ -34,6 +34,7 @@ use Hashids\Hashids;
34 use App\Models\User\User as UserModel; 34 use App\Models\User\User as UserModel;
35 use Illuminate\Support\Facades\DB; 35 use Illuminate\Support\Facades\DB;
36 use Illuminate\Support\Facades\Log; 36 use Illuminate\Support\Facades\Log;
  37 +use Illuminate\Support\Facades\Schema;
37 38
38 /** 39 /**
39 * Class ProjectLogic 40 * Class ProjectLogic
@@ -528,6 +529,7 @@ class ProjectLogic extends BaseLogic @@ -528,6 +529,7 @@ class ProjectLogic extends BaseLogic
528 $data = $this->model::where('id', $this->param['project_id'])->first(); 529 $data = $this->model::where('id', $this->param['project_id'])->first();
529 $data = $data->getAttributes(); 530 $data = $data->getAttributes();
530 $data['type'] = 0; 531 $data['type'] = 0;
  532 + $data['title'] = $data['title'].'-copy';
531 unset($data['id']); 533 unset($data['id']);
532 $project_id = $this->model->insertGetId($data); 534 $project_id = $this->model->insertGetId($data);
533 //复制部署表 535 //复制部署表
@@ -568,6 +570,48 @@ class ProjectLogic extends BaseLogic @@ -568,6 +570,48 @@ class ProjectLogic extends BaseLogic
568 unset($settingData['id']); 570 unset($settingData['id']);
569 $settingData['project_id'] = $project_id; 571 $settingData['project_id'] = $project_id;
570 $settingTemplateModel->insert($settingData); 572 $settingTemplateModel->insert($settingData);
  573 + $this->copyMysql($this->param['project_id'],$project_id);
571 return $this->success($data); 574 return $this->success($data);
572 } 575 }
  576 +
  577 + //复制数据库
  578 + public function copyMysql($new_project_id){
  579 + //切换数据库配置
  580 + ProjectServer::useProject($new_project_id);
  581 + //创建数据库
  582 + ProjectServer::createDatabase($new_project_id);
  583 + //创建表
  584 + $this->initTable();
  585 + //初始数据
  586 + ProjectServer::saveInitParam($new_project_id);
  587 + }
  588 +
  589 +
  590 + /**
  591 + * @param $project
  592 + * @return bool
  593 + * @author zbj
  594 + * @date 2023/4/23
  595 + */
  596 + public function initTable($project_id,$news_project_id)
  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();
  601 + $table = array_column($table, 'Tables_in_' . $database_name);
  602 + foreach ($table as $v) {
  603 + $has_table = Schema::connection('custom_mysql')->hasTable($v);
  604 + if ($has_table) {
  605 + continue;
  606 + }
  607 + $sql = DB::connection('custom_tmp_mysql')->select("SHOW CREATE TABLE {$v}");
  608 + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
  609 + Schema::table($v, function ($v) use ($news_project_id) {
  610 + $v->update(['project_id' => $news_project_id]);
  611 + });
  612 + }
  613 + config(['database.connections.custom_tmp_mysql.database' => 'gl_data_tmp']);
  614 + return true;
  615 + }
  616 +
573 } 617 }