Prechádzať zdrojové kódy

refactor: 重构 farm_land_upgrade_configs 表,直接使用新字段名,移除数据迁移命令

notfff 8 mesiacov pred
rodič
commit
c12a4a1f4d

+ 0 - 122
app/Module/Farm/Commands/MigrateFieldNamesCommand.php

@@ -1,122 +0,0 @@
-<?php
-
-namespace App\Module\Farm\Commands;
-
-use App\Module\Farm\Models\FarmLandUpgradeConfig;
-use Illuminate\Console\Command;
-use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\Schema;
-
-/**
- * 迁移字段名称命令
- */
-class MigrateFieldNamesCommand extends Command
-{
-    /**
-     * 命令名称
-     *
-     * @var string
-     */
-    protected $signature = 'farm:migrate-field-names {--dry-run : 只显示将要执行的操作,不实际执行}';
-
-    /**
-     * 命令描述
-     *
-     * @var string
-     */
-    protected $description = '迁移字段名称,将 consume_group_id 改为 materials_group_id,将 condition_group_id 改为 conditions_group_id';
-
-    /**
-     * 执行命令
-     *
-     * @return int
-     */
-    public function handle()
-    {
-        $this->info('开始迁移字段名称...');
-
-        // 是否为干运行模式
-        $dryRun = $this->option('dry-run');
-        if ($dryRun) {
-            $this->warn('干运行模式:不会实际执行数据库操作');
-        }
-
-        // 检查新字段是否存在
-        $hasNewFields = Schema::hasColumns('farm_land_upgrade_configs', ['materials_group_id', 'conditions_group_id']);
-        
-        if (!$hasNewFields) {
-            $this->error('新字段不存在,请先执行 SQL 脚本添加新字段');
-            return 1;
-        }
-
-        // 检查旧字段是否存在
-        $hasOldFields = Schema::hasColumns('farm_land_upgrade_configs', ['consume_group_id', 'condition_group_id']);
-        
-        if (!$hasOldFields) {
-            $this->info('旧字段不存在,无需迁移');
-            return 0;
-        }
-
-        // 获取所有配置
-        $configs = FarmLandUpgradeConfig::whereNotNull('consume_group_id')
-            ->orWhereNotNull('condition_group_id')
-            ->get();
-
-        $this->info("找到 {$configs->count()} 个需要迁移的配置");
-
-        // 开始迁移
-        $migratedCount = 0;
-        $errorCount = 0;
-
-        foreach ($configs as $config) {
-            $this->line('');
-            $this->line("处理配置 ID: {$config->id}, 从 {$config->from_type_id} 到 {$config->to_type_id}");
-
-            try {
-                // 如果不是干运行模式,则执行迁移
-                if (!$dryRun) {
-                    DB::beginTransaction();
-
-                    // 迁移 consume_group_id 到 materials_group_id
-                    if ($config->consume_group_id) {
-                        $this->info("  迁移 consume_group_id ({$config->consume_group_id}) 到 materials_group_id");
-                        $config->materials_group_id = $config->consume_group_id;
-                    }
-
-                    // 迁移 condition_group_id 到 conditions_group_id
-                    if ($config->condition_group_id) {
-                        $this->info("  迁移 condition_group_id ({$config->condition_group_id}) 到 conditions_group_id");
-                        $config->conditions_group_id = $config->condition_group_id;
-                    }
-
-                    // 保存配置
-                    $config->save();
-
-                    DB::commit();
-                } else {
-                    // 干运行模式,只显示将要执行的操作
-                    if ($config->consume_group_id) {
-                        $this->info("  [干运行] 将迁移 consume_group_id ({$config->consume_group_id}) 到 materials_group_id");
-                    }
-
-                    if ($config->condition_group_id) {
-                        $this->info("  [干运行] 将迁移 condition_group_id ({$config->condition_group_id}) 到 conditions_group_id");
-                    }
-                }
-
-                $migratedCount++;
-            } catch (\Exception $e) {
-                if (!$dryRun) {
-                    DB::rollBack();
-                }
-                $this->error("  迁移失败: " . $e->getMessage());
-                $errorCount++;
-            }
-        }
-
-        $this->line('');
-        $this->info("迁移完成: 成功 {$migratedCount}, 失败 {$errorCount}");
-
-        return 0;
-    }
-}

+ 22 - 0
app/Module/Farm/Database/recreate_farm_land_upgrade_configs_table.sql

@@ -0,0 +1,22 @@
+-- 重构 farm_land_upgrade_configs 表,直接使用新字段名
+-- 由于没有数据需要迁移,我们可以直接修改表结构
+
+-- 删除旧表(如果存在)
+DROP TABLE IF EXISTS `kku_farm_land_upgrade_configs`;
+
+-- 创建新表
+CREATE TABLE `kku_farm_land_upgrade_configs` (
+  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
+  `from_type_id` int(10) unsigned NOT NULL COMMENT '起始土地类型ID',
+  `to_type_id` int(10) unsigned NOT NULL COMMENT '目标土地类型ID',
+  `materials_group_id` int(10) unsigned DEFAULT NULL COMMENT '消耗组ID,关联game_consume_groups表',
+  `conditions_group_id` int(10) unsigned DEFAULT NULL COMMENT '条件组ID,关联game_condition_groups表',
+  `materials` json DEFAULT NULL COMMENT '升级所需材料(已废弃,使用materials_group_id替代)',
+  `conditions` json DEFAULT NULL COMMENT '其他升级条件(已废弃,使用conditions_group_id替代)',
+  `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
+  `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
+  PRIMARY KEY (`id`),
+  KEY `idx_from_to_type` (`from_type_id`,`to_type_id`),
+  KEY `idx_materials_group_id` (`materials_group_id`),
+  KEY `idx_conditions_group_id` (`conditions_group_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='土地升级配置';

+ 0 - 11
app/Module/Farm/Database/rename_group_id_fields_in_farm_land_upgrade_configs.sql

@@ -1,11 +0,0 @@
--- 重命名 farm_land_upgrade_configs 表中的字段
-ALTER TABLE `kku_farm_land_upgrade_configs` 
-CHANGE COLUMN `consume_group_id` `materials_group_id` int(10) unsigned DEFAULT NULL COMMENT '消耗组ID,关联game_consume_groups表',
-CHANGE COLUMN `condition_group_id` `conditions_group_id` int(10) unsigned DEFAULT NULL COMMENT '条件组ID,关联game_condition_groups表';
-
--- 更新索引
-ALTER TABLE `kku_farm_land_upgrade_configs` 
-DROP INDEX `idx_consume_group_id`,
-DROP INDEX `idx_condition_group_id`,
-ADD INDEX `idx_materials_group_id` (`materials_group_id`),
-ADD INDEX `idx_conditions_group_id` (`conditions_group_id`);

+ 29 - 39
app/Module/Farm/Docs/字段名称变更.md

@@ -13,19 +13,28 @@
 
 ## 2. 数据库变更
 
-在 `farm_land_upgrade_configs` 表中重命名了字段
+由于没有现有数据需要迁移,我们直接重新创建了 `farm_land_upgrade_configs` 表
 
 ```sql
-ALTER TABLE `kku_farm_land_upgrade_configs` 
-CHANGE COLUMN `consume_group_id` `materials_group_id` int(10) unsigned DEFAULT NULL COMMENT '消耗组ID,关联game_consume_groups表',
-CHANGE COLUMN `condition_group_id` `conditions_group_id` int(10) unsigned DEFAULT NULL COMMENT '条件组ID,关联game_condition_groups表';
-
--- 更新索引
-ALTER TABLE `kku_farm_land_upgrade_configs` 
-DROP INDEX `idx_consume_group_id`,
-DROP INDEX `idx_condition_group_id`,
-ADD INDEX `idx_materials_group_id` (`materials_group_id`),
-ADD INDEX `idx_conditions_group_id` (`conditions_group_id`);
+-- 删除旧表(如果存在)
+DROP TABLE IF EXISTS `kku_farm_land_upgrade_configs`;
+
+-- 创建新表
+CREATE TABLE `kku_farm_land_upgrade_configs` (
+  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
+  `from_type_id` int(10) unsigned NOT NULL COMMENT '起始土地类型ID',
+  `to_type_id` int(10) unsigned NOT NULL COMMENT '目标土地类型ID',
+  `materials_group_id` int(10) unsigned DEFAULT NULL COMMENT '消耗组ID,关联game_consume_groups表',
+  `conditions_group_id` int(10) unsigned DEFAULT NULL COMMENT '条件组ID,关联game_condition_groups表',
+  `materials` json DEFAULT NULL COMMENT '升级所需材料(已废弃,使用materials_group_id替代)',
+  `conditions` json DEFAULT NULL COMMENT '其他升级条件(已废弃,使用conditions_group_id替代)',
+  `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
+  `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
+  PRIMARY KEY (`id`),
+  KEY `idx_from_to_type` (`from_type_id`,`to_type_id`),
+  KEY `idx_materials_group_id` (`materials_group_id`),
+  KEY `idx_conditions_group_id` (`conditions_group_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='土地升级配置';
 ```
 
 ## 3. 代码变更
@@ -122,36 +131,17 @@ return [
 ];
 ```
 
-## 4. 数据迁移
+## 4. 使用方法
 
-为了将现有的 `consume_group_id` 和 `condition_group_id` 字段数据迁移到新的字段,我们创建了一个迁移命令:
+### 4.1 执行数据库变更
 
-```bash
-php artisan farm:migrate-field-names [--dry-run]
-```
-
-该命令会执行以下操作:
-
-1. 检查新字段是否存在
-2. 检查旧字段是否存在
-3. 获取所有需要迁移的配置
-4. 将 `consume_group_id` 的值复制到 `materials_group_id`
-5. 将 `condition_group_id` 的值复制到 `conditions_group_id`
-
-使用 `--dry-run` 参数可以查看将要执行的操作,而不实际执行。
-
-## 5. 使用方法
-
-### 5.1 执行数据库变更
-
-1. 执行 SQL 脚本,重命名字段和索引
-2. 运行迁移命令,将数据从旧字段复制到新字段
+执行 SQL 脚本,重新创建表结构:
 
 ```bash
-php artisan farm:migrate-field-names
+mysql -u username -p database_name < app/Module/Farm/Database/recreate_farm_land_upgrade_configs_table.sql
 ```
 
-### 5.2 使用新的字段名称
+### 4.2 使用新的字段名称
 
 在代码中,使用新的字段名称和关联方法:
 
@@ -163,8 +153,8 @@ $materialsGroup = $config->materialsGroup;
 $conditionsGroup = $config->conditionsGroup;
 ```
 
-## 6. 注意事项
+## 5. 注意事项
 
-1. 为了保持向后兼容性,保留了旧的关联方法,但建议使用新的方法
-2. 在迁移过程中,只会复制数据,不会删除旧字段的数据
-3. 建议在迁移前先使用 `--dry-run` 参数查看将要执行的操作,确保没有问题后再执行实际迁移
+1. 由于重新创建了表,所有现有数据将被清除。请确保在执行此操作前已备份任何重要数据。
+2. 为了保持向后兼容性,保留了旧的关联方法,但建议使用新的方法。
+3. 在新表中,`materials` 和 `conditions` 字段被标记为已废弃,建议使用 `materials_group_id` 和 `conditions_group_id` 字段替代。

+ 1 - 2
app/Module/Farm/Providers/FarmServiceProvider.php

@@ -51,8 +51,7 @@ class FarmServiceProvider extends ServiceProvider
             Commands\GenerateFarmHouseConfigJson::class,
             Commands\GenerateFarmShrineConfigJson::class,
             Commands\MigrateLandUpgradeMaterialsToConsumeGroupsCommand::class,
-            Commands\MigrateLandUpgradeConditionsToConditionGroupsCommand::class,
-            Commands\MigrateFieldNamesCommand::class
+            Commands\MigrateLandUpgradeConditionsToConditionGroupsCommand::class
         ]);