From 600ef9d4457fd1cac573dcc94369a6a651191cb5 Mon Sep 17 00:00:00 2001 From: surtur Date: Sun, 13 Aug 2023 16:57:19 +0200 Subject: [PATCH] go: add a way to display upcoming db migrations --- modules/db/db.go | 20 ++++++++++++++++++++ run.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/modules/db/db.go b/modules/db/db.go index 920cc5f..102369a 100644 --- a/modules/db/db.go +++ b/modules/db/db.go @@ -6,6 +6,7 @@ package db import ( "context" "fmt" + "os" "git.dotya.ml/mirre-mt/pcmt/ent" "git.dotya.ml/mirre-mt/pcmt/ent/migrate" @@ -90,6 +91,25 @@ func IsSetUp(ctx context.Context, client *ent.Client) (bool, error) { return false, nil } +// PrintMigration prints the upcoming migration to stdout. +func PrintMigration(ctx context.Context, client *ent.Client) error { + slogger := ctx.Value(CtxKey{}).(*slogging.Slogger) + log := *slogger + + log.Logger = log.With( + slog.Group("pcmt extra", slog.String("module", "modules/db")), + ) + + log.Info("printing the upcoming migration to stdout") + + if err := client.Schema.WriteTo(ctx, os.Stdout); err != nil { + log.Errorf("failed to print schema changes: %v", err) + return err + } + + return nil +} + // SetUp attempts to automatically migrate DB schema and creates the set-up // record indicating that the DB has been set up. Optionally and only if the DB // has not been set up prior, it creates and admin user with the initPasswd and diff --git a/run.go b/run.go index b244684..12b29cf 100644 --- a/run.go +++ b/run.go @@ -60,15 +60,16 @@ along with this program. If not, see .` ) var ( - hostFlag = flag.String("host", "unset", "host address to listen on") - portFlag = flag.Int("port", 0, "TCP port to listen on") - configFlag = flag.String("config", "config.dhall", "Default path of the config file") - configIsPathFlag = flag.Bool("configIsPath", true, "Whether the provided config is path or raw config") - develFlag = flag.Bool("devel", false, "Run the application in dev mode, connect to a local browser-sync instance for hot-reloading") - licenseFlag = flag.Bool("license", false, "Print licensing information and exit") - versionFlag = flag.Bool("version", false, "Print version and exit") - importFlag = flag.String("import", "", "Path to import breach data from") - version = "dev" + hostFlag = flag.String("host", "unset", "host address to listen on") + portFlag = flag.Int("port", 0, "TCP port to listen on") + configFlag = flag.String("config", "config.dhall", "Default path of the config file") + configIsPathFlag = flag.Bool("configIsPath", true, "Whether the provided config is path or raw config") + develFlag = flag.Bool("devel", false, "Run the application in dev mode, connect to a local browser-sync instance for hot-reloading") + licenseFlag = flag.Bool("license", false, "Print licensing information and exit") + versionFlag = flag.Bool("version", false, "Print version and exit") + importFlag = flag.String("import", "", "Path to import breach data from") + printMigrationFlag = flag.Bool("printMigration", false, "Print to stdout what is about to happen during the following database migration (mainly useful for debugging") + version = "dev" // the global logger. slogger *slogging.Slogger // local logger instance. @@ -88,13 +89,21 @@ func run() error { //nolint:gocognit return nil } + // skip printing program header. + skipHeader := false + var doingImport bool if importFlag != nil && *importFlag != "" { doingImport = true + skipHeader = true } - if !doingImport { + if *printMigrationFlag { + skipHeader = true + } + + if !skipHeader { printHeader() } @@ -180,6 +189,11 @@ func run() error { //nolint:gocognit ctx := context.WithValue(context.Background(), moddb.CtxKey{}, slogger) + if *printMigrationFlag { + log.Debug("printing the following migration to stdout") + return moddb.PrintMigration(ctx, db) + } + log.Info("ensuring the db is set up and attempting to automatically migrate db schema") // make sure the database is set up and optionally creates an administrator