-
Notifications
You must be signed in to change notification settings - Fork 13
创建模块 自动导入数据库 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
创建模块 自动导入数据库 #25
Conversation
WalkthroughThe module creation command now automatically runs migrations by invoking MigrateUp.Run(ctx) after creating a module. Previous user-facing migration tips and related log messages were removed. Console output is simplified to only show the success message. No exported/public signatures changed. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant CLI as Module CLI
participant Creator as ModuleCreator
participant Migrator as Migrator
User->>CLI: run module create
CLI->>Creator: Create(ctx, name, options)
Creator-->>CLI: result (success/failure)
alt creation success
CLI->>Migrator: MigrateUp.Run(ctx)
Migrator-->>CLI: migration result
alt migration success
CLI-->>User: print "module created" success message
else migration failure
CLI-->>User: print error (migration failed)
end
else creation failure
CLI-->>User: print error (creation failed)
end
note over CLI: Previous manual migration tip removed
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
modules/system/cmd/module.go (1)
236-239: Wrapf(nil) 将返回 nil,已存在模块时不会报错此处
err很可能为 nil,gerror.Wrapf(err, ...)将返回 nil,导致“模块已存在”被当作成功流程静默返回。- if gfile.Exists(modulePath) { - return gerror.Wrapf(err, "模块 '%s' 已存在", moduleName) - } + if gfile.Exists(modulePath) { + return gerror.Newf("模块 '%s' 已存在", moduleName) + }
🧹 Nitpick comments (1)
modules/system/cmd/module.go (1)
273-275: 行为一致性建议:导入后也自动迁移或提供开关
module:create已自动迁移,而module:import仍仅打印“请运行 migrate:up”的提示,行为不一致。建议:导入后同样自动迁移,或提供--no-migrate开关以便在 CI/离线环境中跳过。- tipMsg := "提示: 请运行 'go run main.go migrate:up' 命令开启应用" - g.Log().Infof(ctx, tipMsg) + if err := MigrateUp.Run(ctx); err != nil { + return gerror.Wrapf(err, "导入后自动执行数据库迁移失败") + } + g.Log().Infof(ctx, "模块 '%s' 导入成功,数据库迁移已完成", moduleName)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
modules/system/cmd/module.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
modules/system/cmd/module.go (1)
modules/system/cmd/migrate.go (1)
MigrateUp(27-60)
| // 自动添加到数据库 | ||
| MigrateUp.Run(ctx) | ||
| // 同时直接输出到控制台,确保用户能看到 | ||
| fmt.Printf("\n%s\n%s\n\n", successMsg, tipMsg) | ||
| fmt.Printf("\n%s\n\n", successMsg) | ||
| return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
必须处理迁移失败并避免误导性的“成功”输出
当前忽略了 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.
| // 自动添加到数据库 | |
| 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.
Summary by CodeRabbit