mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
Rename / namespace: - Finish github.com/pocketbase/* → github.com/hanzoai/base (+ /dbx) sweep across all Go sources; restore the renamed root launcher (Base, base.go). - Drop the upstream-only surfaces ours doesn't ship (admin ui/, the password/OTP/oauth2/impersonate auth files, extensions) re-added by the upstream merge. - Decomplect the on-disk layout to one base_/ namespace: data/, migrations/, hooks/, types.d.ts are siblings under base_/ (default data dir base_/data). Drop the hz_ prefix everywhere; collapse migratecmd's identical JS/Go branches to one path. - Remove the accidentally-committed hz_data/ dev dir (db + iam.key) and gitignore the runtime namespace dirs. Deps / build: - kmsclient now imported from the light github.com/hanzoai/kms/sdk/go module (the root kms module zip exceeds Go's download limit); cloud/pkg/base Mount removed (lives in the fused daemon). - replicate v0.4.0 → v0.8.0; metric.Counter.Get() (no prometheus dto); network/metrics.go import fix; auth gitlab/github tidy. Tests / behavior: - Wire registerNotifyWatcherHooks; debounce notify files past the kqueue rescan window so cross-instance settings/collections reload fires. - migratecmd: exclude createdAt/updatedAt (not created/updated); regenerate Go+JS automigration goldens from canonical output. - Convert the two stale archive red-probes to assert the landed defenses (backlog cap bounds memory; forged-segment signature is rejected). IAM-native (Base = pure IAM client): - Rip the ~1k-line home-grown embedded IAM (iam_embedded*.go) and the iam-user CLI. Base no longer reinvents identity as _iam_users/_superusers rows. IAM_ENDPOINT (hanzo.id, or an in-process iam.Embed in the fused daemon) is required; Base validates IAM JWTs via JWKS only — superuser is the IAM admin claim, resolved to an ephemeral unpersisted record. - Extract the IDV reverse-proxy (/v1/idv/*) into idv_proxy.go — it was mis-colocated in the embedded onboarding file and is independent of IAM. - Bind the SQL console (/v1/sql) and test it through real IAM JWTs against a JWKS stub (no hardcoded record tokens, no persisted rows).
216 lines
6.1 KiB
Go
216 lines
6.1 KiB
Go
// Package migratecmd adds a new "migrate" command support to a Base instance.
|
|
//
|
|
// It also comes with automigrations support and templates generation
|
|
// (both for JS and GO migration files).
|
|
//
|
|
// Example usage:
|
|
//
|
|
// migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
|
|
// TemplateLang: migratecmd.TemplateLangJS, // default to migratecmd.TemplateLangGo
|
|
// Automigrate: true,
|
|
// Dir: "/custom/migrations/dir", // optional template migrations path; default to the "migrations" sibling of the data dir
|
|
// })
|
|
//
|
|
// Note: To allow running JS migrations you'll need to enable first
|
|
// [jsvm.MustRegister()].
|
|
package migratecmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/hanzoai/base/core"
|
|
"github.com/hanzoai/base/tools/inflector"
|
|
"github.com/hanzoai/base/tools/osutils"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// Config defines the config options of the migratecmd plugin.
|
|
type Config struct {
|
|
// Dir specifies the directory with the user defined migrations.
|
|
//
|
|
// If not set it fallbacks to the "migrations" sibling of the data dir
|
|
// (i.e. base_/migrations).
|
|
Dir string
|
|
|
|
// Automigrate specifies whether to enable automigrations.
|
|
Automigrate bool
|
|
|
|
// TemplateLang specifies the template language to use when
|
|
// generating migrations - js or go (default).
|
|
TemplateLang string
|
|
}
|
|
|
|
// MustRegister registers the migratecmd plugin to the provided app instance
|
|
// and panic if it fails.
|
|
//
|
|
// Example usage:
|
|
//
|
|
// migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{})
|
|
func MustRegister(app core.App, rootCmd *cobra.Command, config Config) {
|
|
if err := Register(app, rootCmd, config); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Register registers the migratecmd plugin to the provided app instance.
|
|
func Register(app core.App, rootCmd *cobra.Command, config Config) error {
|
|
p := &plugin{app: app, config: config}
|
|
|
|
if p.config.TemplateLang == "" {
|
|
p.config.TemplateLang = TemplateLangGo
|
|
}
|
|
|
|
if p.config.Dir == "" {
|
|
// Both Go and JS migrations live in the one "migrations" sibling of
|
|
// the data dir (i.e. base_/migrations). One namespace, one path.
|
|
p.config.Dir = filepath.Join(p.app.DataDir(), "../migrations")
|
|
}
|
|
|
|
// attach the migrate command
|
|
if rootCmd != nil {
|
|
rootCmd.AddCommand(p.createCommand())
|
|
}
|
|
|
|
// watch for collection changes
|
|
if p.config.Automigrate {
|
|
p.app.OnCollectionCreateRequest().BindFunc(p.automigrateOnCollectionChange)
|
|
p.app.OnCollectionUpdateRequest().BindFunc(p.automigrateOnCollectionChange)
|
|
p.app.OnCollectionDeleteRequest().BindFunc(p.automigrateOnCollectionChange)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type plugin struct {
|
|
app core.App
|
|
config Config
|
|
}
|
|
|
|
func (p *plugin) createCommand() *cobra.Command {
|
|
const cmdDesc = `Supported arguments are:
|
|
- up - runs all available migrations
|
|
- down [number] - reverts the last [number] applied migrations
|
|
- create name - creates new blank migration template file
|
|
- collections - creates new migration file with snapshot of the local collections configuration
|
|
- history-sync - ensures that the _migrations history table doesn't have references to deleted migration files
|
|
`
|
|
|
|
command := &cobra.Command{
|
|
Use: "migrate",
|
|
Short: "Executes app DB migration scripts",
|
|
Long: cmdDesc,
|
|
ValidArgs: []string{"up", "down", "create", "collections"},
|
|
SilenceUsage: true,
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
cmd := ""
|
|
if len(args) > 0 {
|
|
cmd = args[0]
|
|
}
|
|
|
|
switch cmd {
|
|
case "create":
|
|
if _, err := p.migrateCreateHandler("", args[1:], true); err != nil {
|
|
return err
|
|
}
|
|
case "collections":
|
|
if _, err := p.migrateCollectionsHandler(args[1:], true); err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
// note: system migrations are always applied as part of the bootstrap process
|
|
var list = core.MigrationsList{}
|
|
list.Copy(core.SystemMigrations)
|
|
list.Copy(core.AppMigrations)
|
|
|
|
runner := core.NewMigrationsRunner(p.app, list)
|
|
|
|
if err := runner.Run(args...); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
return command
|
|
}
|
|
|
|
func (p *plugin) migrateCreateHandler(template string, args []string, interactive bool) (string, error) {
|
|
if len(args) < 1 {
|
|
return "", errors.New("missing migration file name")
|
|
}
|
|
|
|
name := args[0]
|
|
dir := p.config.Dir
|
|
|
|
filename := fmt.Sprintf("%d_%s.%s", time.Now().Unix(), inflector.Snakecase(name), p.config.TemplateLang)
|
|
|
|
resultFilePath := path.Join(dir, filename)
|
|
|
|
if interactive {
|
|
confirm := osutils.YesNoPrompt(fmt.Sprintf("Do you really want to create migration %q?", resultFilePath), false)
|
|
if !confirm {
|
|
fmt.Println("The command has been cancelled")
|
|
return "", nil
|
|
}
|
|
}
|
|
|
|
// get default create template
|
|
if template == "" {
|
|
var templateErr error
|
|
if p.config.TemplateLang == TemplateLangJS {
|
|
template, templateErr = p.jsBlankTemplate()
|
|
} else {
|
|
template, templateErr = p.goBlankTemplate()
|
|
}
|
|
if templateErr != nil {
|
|
return "", fmt.Errorf("failed to resolve create template: %v", templateErr)
|
|
}
|
|
}
|
|
|
|
// ensure that the migrations dir exist
|
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// save the migration file
|
|
if err := os.WriteFile(resultFilePath, []byte(template), 0644); err != nil {
|
|
return "", fmt.Errorf("failed to save migration file %q: %v", resultFilePath, err)
|
|
}
|
|
|
|
if interactive {
|
|
fmt.Printf("Successfully created file %q\n", resultFilePath)
|
|
}
|
|
|
|
return filename, nil
|
|
}
|
|
|
|
func (p *plugin) migrateCollectionsHandler(args []string, interactive bool) (string, error) {
|
|
createArgs := []string{"collections_snapshot"}
|
|
createArgs = append(createArgs, args...)
|
|
|
|
collections := []*core.Collection{}
|
|
if err := p.app.CollectionQuery().OrderBy("created ASC").All(&collections); err != nil {
|
|
return "", fmt.Errorf("failed to fetch migrations list: %v", err)
|
|
}
|
|
|
|
var template string
|
|
var templateErr error
|
|
if p.config.TemplateLang == TemplateLangJS {
|
|
template, templateErr = p.jsSnapshotTemplate(collections)
|
|
} else {
|
|
template, templateErr = p.goSnapshotTemplate(collections)
|
|
}
|
|
if templateErr != nil {
|
|
return "", fmt.Errorf("failed to resolve template: %v", templateErr)
|
|
}
|
|
|
|
return p.migrateCreateHandler(template, createArgs, interactive)
|
|
}
|