Files
summercms/lagoon/commands.go
Jakub Zych 4ee4c4a2fc feat(03-01): add ServeMux groups, JWT verifier, and serve command
Named middleware resolves at boot, HS256 tokens are pinned with required
exp/sub, and both binaries expose a signal-aware serve command.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 20:04:13 +02:00

87 lines
2.4 KiB
Go

package lagoon
import (
"context"
"fmt"
"git.golem15.com/golem15/summercms/backpack"
"git.golem15.com/golem15/summercms/bonfire"
"git.golem15.com/golem15/summercms/party"
"gorm.io/gorm"
)
// RuntimeCommands returns migrate, migrate:rollback and migrate:status.
// Serve is registered separately via surf.ServeCommand.
func RuntimeCommands(app *backpack.App, plugins []party.Plugin) []bonfire.Command {
return []bonfire.Command{
{
Name: "migrate",
Description: "Run plugin migrations in dependency order",
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
return withDB(ctx, app, func(gdb *gorm.DB) error {
if err := Migrate(gdb, plugins); err != nil {
return err
}
out.Success("migrations applied")
return nil
})
},
},
{
Name: "migrate:rollback",
Description: "Roll back the last migration of a plugin",
Flags: []bonfire.Flag{{
Name: "plugin",
Description: "Plugin ID whose last migration to roll back",
}},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
plugin, _ := in.Flag("plugin")
return withDB(ctx, app, func(gdb *gorm.DB) error {
if err := RollbackLast(gdb, plugins, plugin); err != nil {
return err
}
if plugin == "" {
plugin = lastMigrationPlugin(plugins)
}
out.Success(fmt.Sprintf("rolled back last migration of %s", plugin))
return nil
})
},
},
{
Name: "migrate:status",
Description: "Show per-plugin migration history",
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
return withDB(ctx, app, func(gdb *gorm.DB) error {
rows, err := Status(gdb, plugins)
if err != nil {
return err
}
tableRows := make([][]string, 0, len(rows))
for _, row := range rows {
ids := "(none)"
if len(row.IDs) > 0 {
ids = fmt.Sprintf("%d: %s", len(row.IDs), row.IDs[len(row.IDs)-1])
}
tableRows = append(tableRows, []string{row.Plugin, row.Table, ids})
}
out.Table([]string{"plugin", "table", "applied"}, tableRows)
return nil
})
},
},
}
}
func withDB(ctx context.Context, app *backpack.App, fn func(*gorm.DB) error) error {
sqlDB, gdb, err := OpenFromApp(ctx, app)
if err != nil {
return err
}
defer sqlDB.Close()
if err := Publish(app, sqlDB, gdb); err != nil {
return err
}
return fn(gdb)
}