正在显示
1 个修改的文件
包含
24 行增加
和
10 行删除
| @@ -60,29 +60,43 @@ class CopyOldProject extends Command | @@ -60,29 +60,43 @@ class CopyOldProject extends Command | ||
| 60 | * @method :post | 60 | * @method :post |
| 61 | * @time :2023/12/11 10:09 | 61 | * @time :2023/12/11 10:09 |
| 62 | */ | 62 | */ |
| 63 | - public function initTable($project_id,$news_project_id) | 63 | + public function initTable($project_id, $news_project_id) |
| 64 | { | 64 | { |
| 65 | - config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]); | 65 | + // 设置源数据库 |
| 66 | + config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_' . $project_id]); | ||
| 66 | $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName(); | 67 | $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName(); |
| 68 | + // 获取源数据库的所有表 | ||
| 67 | $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); | 69 | $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables(); |
| 68 | $tables = array_column($tables, 'Tables_in_' . $database_name); | 70 | $tables = array_column($tables, 'Tables_in_' . $database_name); |
| 69 | foreach ($tables as $table) { | 71 | foreach ($tables as $table) { |
| 72 | + // 目标数据库是否存在该表 | ||
| 70 | $has_table = Schema::connection('custom_mysql')->hasTable($table); | 73 | $has_table = Schema::connection('custom_mysql')->hasTable($table); |
| 71 | - if (!$has_table) { | ||
| 72 | - $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}"); | ||
| 73 | - DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']); | 74 | + if ($has_table) { |
| 75 | + // 1. 删除目标数据库中的表 | ||
| 76 | + DB::connection('custom_mysql')->statement("DROP TABLE IF EXISTS {$table}"); | ||
| 74 | } | 77 | } |
| 75 | - if($table == 'gl_customer_visit' || $table == 'gl_customer_visit_item' || $table == 'gl_inquiry_other' || $table == 'gl_inquiry_form_data' || $table == 'gl_inquiry_form'){ | 78 | + // 2. 重新创建表 |
| 79 | + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}"); | ||
| 80 | + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']); | ||
| 81 | + // 3. 跳过指定的表 | ||
| 82 | + if (in_array($table, [ | ||
| 83 | + 'gl_customer_visit', | ||
| 84 | + 'gl_customer_visit_item', | ||
| 85 | + 'gl_inquiry_other', | ||
| 86 | + 'gl_inquiry_form_data', | ||
| 87 | + 'gl_inquiry_form' | ||
| 88 | + ])) { | ||
| 76 | continue; | 89 | continue; |
| 77 | } | 90 | } |
| 78 | - DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据 | 91 | + // 4. 重新插入数据 |
| 79 | DB::connection('custom_mysql')->table($table)->insertUsing( | 92 | DB::connection('custom_mysql')->table($table)->insertUsing( |
| 80 | - [], // 列名数组,留空表示插入所有列 | ||
| 81 | - function ($query) use ($table,$project_id) { | ||
| 82 | - $name = 'gl_data_'.$project_id.'.'.$table; | 93 | + [], // 插入所有列 |
| 94 | + function ($query) use ($table, $project_id) { | ||
| 95 | + $name = 'gl_data_' . $project_id . '.' . $table; | ||
| 83 | $query->select('*')->from("{$name}"); | 96 | $query->select('*')->from("{$name}"); |
| 84 | } | 97 | } |
| 85 | ); | 98 | ); |
| 99 | + // 5. 更新 project_id(如果存在) | ||
| 86 | if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) { | 100 | if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) { |
| 87 | DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]); | 101 | DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]); |
| 88 | } | 102 | } |
-
请 注册 或 登录 后发表评论