作者 刘锟

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

... ... @@ -50,7 +50,6 @@ class CopyProject extends Command
continue;
}
foreach ($list as $item){
DB::beginTransaction();
try {
$old_project_id = $item['data']['project_id'];
$this->output('CopyProjectJob start, project_id: ' . $old_project_id);
... ... @@ -70,9 +69,7 @@ class CopyProject extends Command
$this->output('CopyProjectJob end, old project_id: ' . $old_project_id . ', new project_id: ' . $project_id);
$item->status = NoticeLog::STATUS_SUCCESS;
$item->save();
DB::commit();
}catch (\Exception $e){
DB::rollBack();
echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL . date('Y-m-d H:i:s');
errorLog('项目初始化失败', $item, $e);
$item->status = NoticeLog::STATUS_FAIL;
... ... @@ -232,33 +229,27 @@ class CopyProject extends Command
$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) {
// 1. 删除目标数据库中的表
DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");
}
// 1. 删除目标数据库中的表
DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}");
// 2. 重新创建表
$sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
// 3. 跳过指定的表
if (in_array($table, [
'gl_customer_visit',
'gl_customer_visit_item',
'gl_inquiry_other',
'gl_inquiry_form_data',
'gl_inquiry_form'
])) {
if (in_array($table, ['gl_customer_visit', 'gl_customer_visit_item', 'gl_inquiry_other', 'gl_inquiry_form_data', 'gl_inquiry_form'])) {
continue;
}
try {
// 4. 重新插入数据
DB::connection('custom_mysql')->table($table)->insertUsing(
[], // 插入所有列
function ($query) use ($table, $project_id) {
$name = 'gl_data_' . $project_id . '.' . $table;
$query->select('*')->from("{$name}");
}
);
}catch (\Exception $e){
continue;
}
// 4. 重新插入数据
DB::connection('custom_mysql')->table($table)->insertUsing(
[], // 插入所有列
function ($query) use ($table, $project_id) {
$name = 'gl_data_' . $project_id . '.' . $table;
$query->select('*')->from("{$name}");
}
);
// 5. 更新 project_id(如果存在)
if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
... ...