go: add a way to display upcoming db migrations
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
096b486dd8
commit
600ef9d445
@ -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
|
||||
|
16
run.go
16
run.go
@ -68,6 +68,7 @@ var (
|
||||
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
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user