正在显示
1 个修改的文件
包含
11 行增加
和
1 行删除
| @@ -350,17 +350,27 @@ class UpdateRoute extends Command | @@ -350,17 +350,27 @@ class UpdateRoute extends Command | ||
| 350 | { | 350 | { |
| 351 | $sourceTable = 'gl_route_map'; | 351 | $sourceTable = 'gl_route_map'; |
| 352 | $destinationTable = 'gl_route_map_copy'; | 352 | $destinationTable = 'gl_route_map_copy'; |
| 353 | + | ||
| 353 | if (!Schema::connection('custom_mysql')->hasTable($sourceTable)) { | 354 | if (!Schema::connection('custom_mysql')->hasTable($sourceTable)) { |
| 354 | $this->error("Source table {$sourceTable} does not exist."); | 355 | $this->error("Source table {$sourceTable} does not exist."); |
| 355 | return false; | 356 | return false; |
| 356 | } | 357 | } |
| 358 | + | ||
| 357 | if (Schema::connection('custom_mysql')->hasTable($destinationTable)) { | 359 | if (Schema::connection('custom_mysql')->hasTable($destinationTable)) { |
| 358 | Schema::connection('custom_mysql')->dropIfExists($destinationTable); | 360 | Schema::connection('custom_mysql')->dropIfExists($destinationTable); |
| 359 | } | 361 | } |
| 362 | + | ||
| 360 | $columns = DB::connection('custom_mysql')->select(DB::raw("SHOW COLUMNS FROM {$sourceTable}")); | 363 | $columns = DB::connection('custom_mysql')->select(DB::raw("SHOW COLUMNS FROM {$sourceTable}")); |
| 361 | $columnsDefinition = collect($columns)->map(function($column) { | 364 | $columnsDefinition = collect($columns)->map(function($column) { |
| 362 | - return $column->Field . ' ' . $column->Type . ($column->Null === 'NO' ? ' NOT NULL' : '') . ($column->Default !== null ? " DEFAULT '" . $column->Default . "'" : '') . ($column->Extra ? ' ' . $column->Extra : ''); | 365 | + $columnDefinition = $column->Field . ' ' . $column->Type . |
| 366 | + ($column->Null === 'NO' ? ' NOT NULL' : '') . | ||
| 367 | + ($column->Default !== null ? " DEFAULT '" . $column->Default . "'" : '') . | ||
| 368 | + ($column->Extra ? ' ' . $column->Extra : ''); | ||
| 369 | + return $columnDefinition; | ||
| 363 | })->implode(', '); | 370 | })->implode(', '); |
| 371 | + // Add PRIMARY KEY to the id column | ||
| 372 | + $primaryKey = collect($columns)->firstWhere('Field', 'id') ? 'PRIMARY KEY (id)' : ''; | ||
| 373 | + $columnsDefinition = $columnsDefinition . ($primaryKey ? ', ' . $primaryKey : ''); | ||
| 364 | DB::connection('custom_mysql')->statement("CREATE TABLE {$destinationTable} ({$columnsDefinition})"); | 374 | DB::connection('custom_mysql')->statement("CREATE TABLE {$destinationTable} ({$columnsDefinition})"); |
| 365 | DB::connection('custom_mysql')->statement("INSERT INTO {$destinationTable} SELECT * FROM {$sourceTable}"); | 375 | DB::connection('custom_mysql')->statement("INSERT INTO {$destinationTable} SELECT * FROM {$sourceTable}"); |
| 366 | $this->info("Table {$sourceTable} has been copied to {$destinationTable} successfully."); | 376 | $this->info("Table {$sourceTable} has been copied to {$destinationTable} successfully."); |
-
请 注册 或 登录 后发表评论