Skip to content

Commit

Permalink
WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
efritz committed Sep 15, 2024
1 parent bbc0127 commit f400cd8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
54 changes: 37 additions & 17 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"net/url"
"os"

"github.com/go-nacelle/nacelle/v2"
"github.com/go-nacelle/config/v3"
"github.com/go-nacelle/log/v2"
"github.com/go-nacelle/pgutil"
"github.com/spf13/cobra"
)
Expand All @@ -25,10 +26,14 @@ var (
migrationDirectory string
databaseURL string
defaultDatabaseURL = pgutil.BuildDatabaseURL()
logger = nacelle.NewNilLogger() // TODO
logger log.Logger
)

func init() {
if err := initLogger(); err != nil {
panic(err)
}

masked, err := maskDatabasePassword(defaultDatabaseURL)
if err != nil {
panic(err)
Expand All @@ -49,6 +54,36 @@ func init() {
)
}

func initLogger() (err error) {
cfg := config.NewConfig(config.NewEnvSourcer("PGUTIL"))
if err := cfg.Init(); err != nil {
return err
}

c := &log.Config{}
if err := cfg.Load(c); err != nil {
return err
}

logger, err = log.InitLogger(c)
return err
}

func maskDatabasePassword(databaseURL string) (string, error) {
parsedURL, err := url.Parse(databaseURL)
if err != nil {
return "", fmt.Errorf("failed to parse database URL: %w", err)
}

if parsedURL.User != nil {
if _, ok := parsedURL.User.Password(); ok {
parsedURL.User = url.UserPassword(parsedURL.User.Username(), "xxxxx")
}
}

return parsedURL.String(), nil
}

func dial() (pgutil.DB, error) {
if databaseURL == "" {
databaseURL = defaultDatabaseURL
Expand All @@ -70,18 +105,3 @@ func runner() (*pgutil.Runner, error) {

return runner, nil
}

func maskDatabasePassword(databaseURL string) (string, error) {
parsedURL, err := url.Parse(databaseURL)
if err != nil {
return "", fmt.Errorf("failed to parse database URL: %w", err)
}

if parsedURL.User != nil {
if _, ok := parsedURL.User.Password(); ok {
parsedURL.User = url.UserPassword(parsedURL.User.Username(), "xxxxx")
}
}

return parsedURL.String(), nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/go-nacelle/pgutil
go 1.21

require (
github.com/go-nacelle/config/v2 v2.0.1
github.com/go-nacelle/log/v2 v2.0.1
github.com/go-nacelle/nacelle/v2 v2.1.1
github.com/google/go-cmp v0.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-nacelle/config/v2 v2.0.1 h1:/mBAe1xkaRYD+WFrG/DsOvlnj8bu8Ull33BWBQgS4ls=
github.com/go-nacelle/config/v2 v2.0.1/go.mod h1:NdcZcMmA2yqSjSZRNOD9tm3AifkrEYnmZKhjJjKMQB4=
github.com/go-nacelle/config/v3 v3.0.0 h1:YseTUtwKC21rgWNdBh+f0A5ANnh7kSMqGkB2JHI0aJ8=
github.com/go-nacelle/config/v3 v3.0.0/go.mod h1:cj+WGluCHOR/5gup6dx3wunXsEGEbxmCiB6cYa9iNYQ=
github.com/go-nacelle/log/v2 v2.0.1 h1:vOkiKz/pZannZIpH2yDV03hUKbtr6uLvrWklk8N6eR0=
Expand Down
5 changes: 3 additions & 2 deletions migration_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,16 @@ func (r *Runner) applyDefinitions(ctx context.Context, definitions []Definition,
}

if len(migrationsToApply) == 0 {
r.logger.Info("No migrations to apply")
r.logger.Info("Migrations are in expected state")
upToDate = true
return nil
}

for _, definition := range migrationsToApply {
if definition.IndexMetadata != nil {
if definition.IndexMetadata != nil && !reverse {
// We can't perform CIC while holding a lock or else we'll deadlock.
// Capture this definition to be applied outside of the lock we're holding.
// We can skip this check for reverse application as CIC are illegal in down migrations.
cicDefinition = &definition
return nil
}
Expand Down

0 comments on commit f400cd8

Please sign in to comment.