Skip to content

Commit eccbaef

Browse files
committed
chore: cleanup db url parsing
1 parent 84dcb02 commit eccbaef

2 files changed

Lines changed: 17 additions & 25 deletions

File tree

cmd/migrate_cmd.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"embed"
5-
"fmt"
65
"net/url"
76
"os"
87

@@ -23,12 +22,12 @@ var migrateCmd = cobra.Command{
2322

2423
func migrate(cmd *cobra.Command, args []string) {
2524
globalConfig := loadGlobalConfig(cmd.Context())
25+
u, err := url.Parse(globalConfig.DB.URL)
26+
if err != nil {
27+
logrus.Fatalf("%+v", errors.Wrap(err, "parsing db connection url"))
28+
}
2629

2730
if globalConfig.DB.Driver == "" && globalConfig.DB.URL != "" {
28-
u, err := url.Parse(globalConfig.DB.URL)
29-
if err != nil {
30-
logrus.Fatalf("%+v", errors.Wrap(err, "parsing db connection url"))
31-
}
3231
globalConfig.DB.Driver = u.Scheme
3332
}
3433

@@ -53,16 +52,12 @@ func migrate(cmd *cobra.Command, args []string) {
5352
}
5453
}
5554

56-
u, _ := url.Parse(globalConfig.DB.URL)
57-
processedUrl := globalConfig.DB.URL
58-
if len(u.Query()) != 0 {
59-
processedUrl = fmt.Sprintf("%s&application_name=gotrue_migrations", processedUrl)
60-
} else {
61-
processedUrl = fmt.Sprintf("%s?application_name=gotrue_migrations", processedUrl)
62-
}
55+
q := u.Query()
56+
q.Add("application_name", "auth_migrations")
57+
u.RawQuery = q.Encode()
6358
deets := &pop.ConnectionDetails{
6459
Dialect: globalConfig.DB.Driver,
65-
URL: processedUrl,
60+
URL: u.String(),
6661
}
6762
deets.Options = map[string]string{
6863
"migration_table_name": "schema_migrations",

internal/indexworker/indexworker.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,21 @@ const (
3333
// Returns an error either from index creation failure (partial or complete) or if the advisory lock
3434
// could not be acquired.
3535
func CreateIndexes(ctx context.Context, config *conf.GlobalConfiguration, le *logrus.Entry) error {
36+
u, err := url.Parse(config.DB.URL)
37+
if err != nil {
38+
le.WithError(err).Fatal("Error parsing db connection url")
39+
}
40+
3641
if config.DB.Driver == "" && config.DB.URL != "" {
37-
u, err := url.Parse(config.DB.URL)
38-
if err != nil {
39-
le.Fatalf("Error parsing db connection url: %+v", err)
40-
}
4142
config.DB.Driver = u.Scheme
4243
}
4344

44-
u, _ := url.Parse(config.DB.URL)
45-
processedUrl := config.DB.URL
46-
if len(u.Query()) != 0 {
47-
processedUrl = fmt.Sprintf("%s&application_name=auth_index_worker", processedUrl)
48-
} else {
49-
processedUrl = fmt.Sprintf("%s?application_name=auth_index_worker", processedUrl)
50-
}
45+
q := u.Query()
46+
q.Add("application_name", "auth_index_worker")
47+
u.RawQuery = q.Encode()
5148
deets := &pop.ConnectionDetails{
5249
Dialect: config.DB.Driver,
53-
URL: processedUrl,
50+
URL: u.String(),
5451
}
5552
deets.Options = map[string]string{
5653
"Namespace": config.DB.Namespace,

0 commit comments

Comments
 (0)