Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions modules/system/cmd/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ var (

// 使用更明显的方式输出成功信息和提示
successMsg := fmt.Sprintf("模块 '%s' 创建成功!", moduleName)
tipMsg := "提示: 请运行 'go run main.go migrate:up' 命令开启应用"

// 记录到日志
g.Log().Info(ctx, successMsg)
g.Log().Info(ctx, tipMsg)

// 自动添加到数据库
MigrateUp.Run(ctx)
// 同时直接输出到控制台,确保用户能看到
fmt.Printf("\n%s\n%s\n\n", successMsg, tipMsg)
fmt.Printf("\n%s\n\n", successMsg)
return nil
Comment on lines +83 to 87
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

必须处理迁移失败并避免误导性的“成功”输出

当前忽略了 MigrateUp.Run(ctx) 的返回错误;即使迁移失败也会继续打印“创建成功”,导致误导。建议捕获并返回错误,并在迁移成功后再输出成功信息。

-			// 自动添加到数据库
-			MigrateUp.Run(ctx)
-			// 同时直接输出到控制台,确保用户能看到
-			fmt.Printf("\n%s\n\n", successMsg)
+			// 自动执行数据库迁移,并处理错误
+			if err := MigrateUp.Run(ctx); err != nil {
+				g.Log().Errorf(ctx, "自动执行数据库迁移失败: %+v", err)
+				return err
+			}
+			// 迁移成功后再输出成功信息
+			fmt.Printf("\n%s\n\n", successMsg)

另外两点(可选优化):

  • 将 Line 82 的 g.Log().Info(ctx, successMsg) 移到迁移成功之后,并将 successMsg 改为“创建成功,数据库迁移已完成”以与行为一致。
  • 若仅希望应用此次新建模块生成的 1 个 up 迁移,考虑直接使用同包内迁移函数而非嵌套运行命令:m := migrateDB(ctx); defer m.Close(); if err := m.Steps(1); err != nil { return err },避免把其它未执行的历史迁移一并执行。
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 自动添加到数据库
MigrateUp.Run(ctx)
// 同时直接输出到控制台,确保用户能看到
fmt.Printf("\n%s\n%s\n\n", successMsg, tipMsg)
fmt.Printf("\n%s\n\n", successMsg)
return nil
// 自动执行数据库迁移,并处理错误
if err := MigrateUp.Run(ctx); err != nil {
g.Log().Errorf(ctx, "自动执行数据库迁移失败: %+v", err)
return err
}
// 迁移成功后再输出成功信息
fmt.Printf("\n%s\n\n", successMsg)
return nil
🤖 Prompt for AI Agents
In modules/system/cmd/module.go around lines 83 to 87, MigrateUp.Run(ctx) is
called without checking its error so the code prints a misleading success
message even when migration fails; capture the error returned by
MigrateUp.Run(ctx), return it on failure, and only log/print the successMsg
after the migration completes successfully; also move the g.Log().Info(ctx,
successMsg) call to after the successful migration and update successMsg to
"创建成功,数据库迁移已完成"; optionally, if you want to only apply the newly created up
migration instead of running all pending migrations, use the package migration
helper (e.g., get a migrateDB(ctx) instance, defer Close(), and call Steps(1)
and handle its error) instead of invoking the full MigrateUp command.

},
}
Expand Down