作者 lyh

gx

... ... @@ -53,7 +53,6 @@ class InitProject extends Command
echo 'start:' . $item['id'] . PHP_EOL;
try {
$project = Project::find($item['data']['project_id']);
$project_logic = new ProjectLogic();
//初始化数据库
if(!empty($project['mysql_id'])){
... ...
... ... @@ -34,6 +34,7 @@ use Hashids\Hashids;
use App\Models\User\User as UserModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
/**
* Class ProjectLogic
... ... @@ -528,6 +529,7 @@ class ProjectLogic extends BaseLogic
$data = $this->model::where('id', $this->param['project_id'])->first();
$data = $data->getAttributes();
$data['type'] = 0;
$data['title'] = $data['title'].'-copy';
unset($data['id']);
$project_id = $this->model->insertGetId($data);
//复制部署表
... ... @@ -568,6 +570,48 @@ class ProjectLogic extends BaseLogic
unset($settingData['id']);
$settingData['project_id'] = $project_id;
$settingTemplateModel->insert($settingData);
$this->copyMysql($this->param['project_id'],$project_id);
return $this->success($data);
}
//复制数据库
public function copyMysql($new_project_id){
//切换数据库配置
ProjectServer::useProject($new_project_id);
//创建数据库
ProjectServer::createDatabase($new_project_id);
//创建表
$this->initTable();
//初始数据
ProjectServer::saveInitParam($new_project_id);
}
/**
* @param $project
* @return bool
* @author zbj
* @date 2023/4/23
*/
public function initTable($project_id,$news_project_id)
{
config(['database.connections.custom_tmp_mysql.database' => 'gl_data_'.$project_id]);
$database_name = DB::connection('custom_tmp_mysql')->getDatabaseName();
$table = Schema::connection('custom_tmp_mysql')->getAllTables();
$table = array_column($table, 'Tables_in_' . $database_name);
foreach ($table as $v) {
$has_table = Schema::connection('custom_mysql')->hasTable($v);
if ($has_table) {
continue;
}
$sql = DB::connection('custom_tmp_mysql')->select("SHOW CREATE TABLE {$v}");
DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
Schema::table($v, function ($v) use ($news_project_id) {
$v->update(['project_id' => $news_project_id]);
});
}
config(['database.connections.custom_tmp_mysql.database' => 'gl_data_tmp']);
return true;
}
}
... ...