yao/cmd/migrate.go
Max 8fdd1a3a6c feat(robot): add workspace support to robot management
- Introduced a new `workspace` field across various robot-related structures, including `CreateRobotRequest`, `UpdateRobotRequest`, and `RobotResponse`, allowing for better organization and management of robots within specific workspaces.
- Updated database queries and response mappings to accommodate the new workspace field, ensuring seamless integration with existing functionalities.
- Enhanced agent execution context to include workspace information, improving the contextual awareness of agents during operations.
- Added tests to validate the creation and updating of robots with workspace data, ensuring robust functionality and backward compatibility.
2026-03-28 16:59:52 +08:00

121 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"fmt"
"os"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/yaoapp/gou/model"
"github.com/yaoapp/gou/process"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/engine"
"github.com/yaoapp/yao/share"
)
var name string
var force bool = false
var resetModel bool = false
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: L("Update database schema"),
Long: L("Update database schema"),
Run: func(cmd *cobra.Command, args []string) {
defer func() {
err := exception.Catch(recover())
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
}
}()
Boot()
if !force && config.Conf.Mode == "production" {
fmt.Println(color.WhiteString(L("TRY:")), color.GreenString("%s migrate --force", share.BUILDNAME))
exception.New(L("Migrate is not allowed on production mode."), 403).Throw()
}
// 仅加载 Application、DB 连接和 Model含自动 migrate不启动完整 Engine
loadWarnings, err := engine.LoadForMigrate(config.Conf)
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
os.Exit(1)
}
if len(loadWarnings) > 0 {
for _, warning := range loadWarnings {
fmt.Println(color.YellowString("[%s] %s", warning.Widget, warning.Error))
}
}
if name != "" {
mod, has := model.Models[name]
if !has {
fmt.Println(color.RedString(L("Model: %s does not exits"), name))
return
}
fmt.Print(color.WhiteString(fmt.Sprintf(L("Update schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name)) + "\t")
if resetModel {
err := mod.DropTable()
if err != nil {
fmt.Print(color.RedString(fmt.Sprintf(L("FAILURE\n%s"), err.Error())) + "\n")
return
}
}
err := mod.Migrate(false)
if err != nil {
fmt.Print(color.RedString(fmt.Sprintf(L("FAILURE\n%s"), err.Error())) + "\n")
return
}
fmt.Print(color.GreenString(L("SUCCESS")) + "\n")
return
}
// Do Stuff Here
for _, mod := range model.Models {
fmt.Print(color.WhiteString(fmt.Sprintf(L("Update schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name)) + "\t")
if resetModel {
err := mod.DropTable()
if err != nil {
fmt.Print(color.RedString(fmt.Sprintf(L("FAILURE\n%s"), err.Error())) + "\n")
continue
}
}
err := mod.Migrate(false)
if err != nil {
fmt.Print(color.RedString(fmt.Sprintf(L("FAILURE\n%s"), err.Error())) + "\n")
continue
}
fmt.Print(color.GreenString(L("SUCCESS")) + "\n")
}
// After Migrate Hook
if share.App.AfterMigrate != "" {
option := map[string]any{"force": force, "reset": resetModel, "mode": config.Conf.Mode}
p, err := process.Of(share.App.AfterMigrate, option)
if err != nil {
fmt.Println(color.RedString(L("AfterMigrate: %s %v"), share.App.AfterMigrate, err))
return
}
_, err = p.Exec()
if err != nil {
fmt.Println(color.RedString(L("AfterMigrate: %s %v"), share.App.AfterMigrate, err))
}
}
// fmt.Println(color.GreenString(L("✨DONE✨")))
},
}
func init() {
migrateCmd.PersistentFlags().StringVarP(&name, "name", "n", "", L("Model name"))
migrateCmd.PersistentFlags().BoolVarP(&force, "force", "", false, L("Force migrate"))
migrateCmd.PersistentFlags().BoolVarP(&resetModel, "reset", "", false, L("Drop the table if exist"))
}