作者 刘锟

Merge remote-tracking branch 'origin/master' into akun

... ... @@ -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'])){
... ...
... ... @@ -5,6 +5,8 @@ namespace App\Http\Logic\Aside\Project;
use App\Models\Com\NoticeLog;
use App\Models\Devops\ServerConfig;
use App\Models\Project\ProjectRenew;
use App\Models\Template\BSettingTemplate;
use App\Models\Template\Setting;
use App\Models\User\ProjectMenu;
use App\Models\User\ProjectRole;
use App\Services\SyncService;
... ... @@ -32,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
... ... @@ -525,17 +528,104 @@ 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);
//复制部署表
$buildModel = new DeployBuild();
$buildData = $buildModel::where('id', $this->param['project_id'])->first();
$buildData = $buildModel::where('project_id', $this->param['project_id'])->first();
if(!empty($buildData)){
$buildData = $buildData->getAttributes();
$buildData['project_id'] = $project_id;
$hashids = new Hashids('test_domain', 5, 'abcdefghjkmnpqrstuvwxyz1234567890');
$code = $hashids->encode($project_id);
$buildData['test_domain'] = 'https://v6-' . $code . '.globalso.site/';
unset($buildData['id']);
$buildModel->insert($buildData);
}
//复制优化表
$optimizeModel = new DeployOptimize();
$optimizeData = $optimizeModel::where('project_id', $this->param['project_id'])->first();
if(!empty($optimizeData)){
$optimizeData = $optimizeData->getAttributes();
unset($optimizeData['id'],$optimizeData['domain']);
$optimizeData['project_id'] = $project_id;
$optimizeModel->insert($optimizeData);
}
//复制付费表
$paymentModel = new Payment();
$paymentData = $paymentModel::where('project_id', $this->param['project_id'])->first();
if(!empty($paymentData)){
$paymentData = $paymentData->getAttributes();
unset($paymentData['id']);
$paymentData['project_id'] = $project_id;
$paymentModel->insert($paymentData);
}
//复制用户
$userModel = new UserModel();
$userData = $userModel::where('project_id', $this->param['project_id'])->where('role_id',0)->first();
if(!empty($userData)){
$userData = $userData->getAttributes();
unset($userData['id']);
$userData['project_id'] = $project_id;
$userModel->insert($userData);
}
//复制设置的模版
$settingTemplateModel = new Setting();
$settingData = $settingTemplateModel::where('project_id', $this->param['project_id'])->first();
if(!empty($settingData)){
$settingData = $settingData->getAttributes();
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($project_id,$new_project_id){
//切换数据库配置
$project = ProjectServer::useProject($new_project_id);
//创建数据库
ProjectServer::createDatabase($project);
//创建表
$this->initTable($project_id,$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_copy.database' => 'gl_data_'.$project_id]);
$database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
$tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
$tables = array_column($tables, 'Tables_in_' . $database_name);
foreach ($tables as $table) {
$has_table = Schema::connection('custom_mysql')->hasTable($table);
if (!$has_table) {
$sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
}
DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据
DB::connection('custom_mysql')->table($table)->insertUsing(
[], // 列名数组,留空表示插入所有列
function ($query) use ($table,$project_id) {
$name = 'gl_data_'.$project_id.'.'.$table;
$query->select('*')->from("{$name}");
}
);
if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
}
}
return true;
}
}
... ...
... ... @@ -39,7 +39,6 @@ class BlogLogic extends BaseLogic
$this->editNewsRoute($this->param['id'],$this->param['url']);
$this->edit($this->param,['id'=>$this->param['id']]);
}else{
$this->param['url'] = $this->param['url'].'-'.RouteMap::SOURCE_BLOG;
$id = $this->model->addReturnId($this->param);
}
$route = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $id, $this->user['project_id']);
... ...
... ... @@ -68,7 +68,6 @@ class NewsLogic extends BaseLogic
$id = $this->param['id'];
$this->edit($this->param, ['id' => $this->param['id']]);
} else {
$this->param['url'] = $this->param['url'].'-'.RouteMap::SOURCE_NEWS;
$id = $this->model->addReturnId($this->param);
}
//更新路由
... ...
... ... @@ -60,7 +60,6 @@ class KeywordLogic extends BaseLogic
$this->param['project_id'] = $this->user['project_id'];
$this->param['created_at'] = date('Y-m-d H:i:s');
$this->param['updated_at'] = $this->param['created_at'];
$this->param['route'] = $this->param['route'].'-'.RouteMap::SOURCE_PRODUCT;
$id = $this->model->insertGetId($this->param);
//路由映射
$route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
... ...
... ... @@ -83,6 +83,26 @@ return [
]) : [],
],
'custom_tmp_mysql_copy' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_TMP', ''),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'custom_mysql' => [
'driver' => 'mysql',
'url' => '', // DB_DATABASE_URL
... ...