go: add a way to display upcoming db migrations
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2023-08-13 16:57:19 +02:00
parent 096b486dd8
commit 600ef9d445
Signed by: wanderer
SSH Key Fingerprint: SHA256:MdCZyJ2sHLltrLBp0xQO0O1qTW9BT/xl5nXkDvhlMCI
2 changed files with 44 additions and 10 deletions

@ -6,6 +6,7 @@ package db
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"git.dotya.ml/mirre-mt/pcmt/ent" "git.dotya.ml/mirre-mt/pcmt/ent"
"git.dotya.ml/mirre-mt/pcmt/ent/migrate" "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 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 // 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 // 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 // has not been set up prior, it creates and admin user with the initPasswd and

34
run.go

@ -60,15 +60,16 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.`
) )
var ( var (
hostFlag = flag.String("host", "unset", "host address to listen on") hostFlag = flag.String("host", "unset", "host address to listen on")
portFlag = flag.Int("port", 0, "TCP port 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") 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") 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") 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") licenseFlag = flag.Bool("license", false, "Print licensing information and exit")
versionFlag = flag.Bool("version", false, "Print version and exit") versionFlag = flag.Bool("version", false, "Print version and exit")
importFlag = flag.String("import", "", "Path to import breach data from") importFlag = flag.String("import", "", "Path to import breach data from")
version = "dev" 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. // the global logger.
slogger *slogging.Slogger slogger *slogging.Slogger
// local logger instance. // local logger instance.
@ -88,13 +89,21 @@ func run() error { //nolint:gocognit
return nil return nil
} }
// skip printing program header.
skipHeader := false
var doingImport bool var doingImport bool
if importFlag != nil && *importFlag != "" { if importFlag != nil && *importFlag != "" {
doingImport = true doingImport = true
skipHeader = true
} }
if !doingImport { if *printMigrationFlag {
skipHeader = true
}
if !skipHeader {
printHeader() printHeader()
} }
@ -180,6 +189,11 @@ func run() error { //nolint:gocognit
ctx := context.WithValue(context.Background(), moddb.CtxKey{}, slogger) 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") 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 // make sure the database is set up and optionally creates an administrator