CopyOldProject.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @remark :
* @name :CopyOldProject.php
* @author :lyh
* @method :post
* @time :2025/2/18 14:10
*/
namespace App\Console\Commands\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
class CopyOldProject extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'copy_project_s {old_project_id} {project_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'copy--复制项目';
public function handle()
{
$old_project_id = $this->argument('old_project_id');
$this->output('CopyProjectJob start, old_project_id: ' . $old_project_id);
$project_id = $this->argument('project_id');
$this->output('CopyProjectJob start, project_id: ' . $project_id);
$this->copyMysql($old_project_id,$project_id);
return true;
}
//复制数据库
public function copyMysql($project_id,$new_project_id){
//切换数据库配置
$project = ProjectServer::useProject($new_project_id);
//创建数据库
ProjectServer::createDatabase($project);
//创建表
$this->initTable($project_id,$new_project_id);
}
/**
* @remark :创建数据库
* @name :initTable
* @author :lyh
* @method :post
* @time :2023/12/11 10:09
*/
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) {
try {
// 1. 检查源表是否存在(防止 gl_data_{$project_id} 下缺表)
$exists = Schema::connection('custom_tmp_mysql_copy')->hasTable($table);
if (!$exists) {
@file_put_contents(
storage_path('logs/copy_mysql_error.log'),
"源库中不存在表:{$table}" . PHP_EOL,
FILE_APPEND
);
continue;
}
// 2. 删除目标数据库中的表
$result = DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS `{$table}`");
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "删除旧表:{$table} => {$result}" . PHP_EOL, FILE_APPEND);
// 3. 复制建表 SQL
$sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE `{$table}`");
$createSql = get_object_vars($sql[0])['Create Table'];
$result1 = DB::connection('custom_mysql')->statement($createSql);
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "创建表:{$table} => {$result1}" . PHP_EOL, FILE_APPEND);
// 4. 跳过指定表
if (in_array($table, [
'gl_customer_visit', 'gl_customer_visit_item',
'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form',
'gl_ai_blog', 'gl_ai_blog_author', 'gl_ai_blog_list', 'gl_ai_blog_log'
])) {
continue;
}
// 5. 插入数据前,再次确认源表存在(双保险)
if (!Schema::connection('custom_tmp_mysql_copy')->hasTable($table)) {
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据前发现表不存在:{$table}" . PHP_EOL, FILE_APPEND);
continue;
}
// 6. 原生 SQL 插入数据(完全复制)
$insert_sql = "INSERT INTO `{$table}` SELECT * FROM `gl_data_{$project_id}`.`{$table}`";
try {
$result2 = DB::connection('custom_mysql')->statement($insert_sql);
@file_put_contents(storage_path('logs/copy_mysql_error.log'), "插入数据成功:{$table} => {$result2}" . PHP_EOL, FILE_APPEND);
} catch (\Exception $e) {
@file_put_contents(storage_path('logs/copy_mysql_error.log'),
"插入数据失败:{$table} => " . $e->getMessage() . PHP_EOL,
FILE_APPEND
);
continue; // 跳过这个表,不中断整个流程
}
// 7. 更新 project_id(如果存在)
if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
}
} catch (\Exception $e) {
@file_put_contents(
storage_path('logs/copy_mysql_error.log'),
"处理表 {$table} 出错:" . $e->getMessage() . PHP_EOL,
FILE_APPEND
);
continue;
}
}
return true;
}
/**
* @param $message
* @return bool
*/
public function output($message)
{
$date = date('Y-m-d H:i:s');
$output = $date . ', ' . $message . PHP_EOL;
echo $output;
Log::info($output);
return true;
}
}