Skip to content

Commit

Permalink
[Backport 5.1] Migrator: db dsns behind SRC_LOG_LEVEL (#56836)
Browse files Browse the repository at this point in the history
Co-authored-by: Noah S-C <noah@sourcegraph.com>
Co-authored-by: Joe Chen <joe@sourcegraph.com>
Co-authored-by: Warren Gifford <warren@sourcegraph.com>
  • Loading branch information
4 people committed Sep 20, 2023
1 parent e847ffa commit 9238146
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/database/connections/live/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions internal/database/connections/live/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/sourcegraph/sourcegraph/internal/database/migration/runner"
"github.com/sourcegraph/sourcegraph/internal/database/migration/schemas"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/observation"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/output"
Expand All @@ -18,6 +19,7 @@ func RunnerFromDSNs(out *output.Output, logger log.Logger, dsns map[string]strin
}

func RunnerFromDSNsWithSchemas(out *output.Output, logger log.Logger, dsns map[string]string, appName string, newStore StoreFactory, availableSchemas []*schemas.Schema) (*runner.Runner, error) {
var verbose = env.LogLevel == "dbug"
frontendSchema, ok := schemaByName(availableSchemas, "frontend")
if !ok {
return nil, errors.Newf("no available schema matches %q", "frontend")
Expand All @@ -33,13 +35,22 @@ func RunnerFromDSNsWithSchemas(out *output.Output, logger log.Logger, dsns map[s
factory func(observationCtx *observation.Context, dsn, appName string) (*sql.DB, error),
) runner.StoreFactory {
return func(ctx context.Context) (runner.Store, error) {
pending := out.Pending(output.Styledf(output.StylePending, "Attempting connection to %s", dsns[name]))
var pending output.Pending
if verbose {
pending = out.Pending(output.Styledf(output.StylePending, "Attempting connection to %s: %s", schema.Name, dsns[name]))
} else {
pending = out.Pending(output.Styledf(output.StylePending, "Attempting connection to %s", schema.Name))
}
db, err := factory(observation.NewContext(logger), dsns[name], appName)
if err != nil {
pending.Destroy()
return nil, err
}
pending.Complete(output.Emojif(output.EmojiSuccess, "Connection to %q succeeded", dsns[name]))
if verbose {
pending.Complete(output.Emojif(output.EmojiSuccess, "Connection to %s: %s succeeded", schema.Name, dsns[name]))
} else {
pending.Complete(output.Emojif(output.EmojiSuccess, "Connection to %s succeeded", schema.Name))
}

return initStore(ctx, newStore, db, schema)
}
Expand Down
1 change: 1 addition & 0 deletions internal/database/migration/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions internal/database/migration/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/database/migration/schemas"
"github.com/sourcegraph/sourcegraph/internal/database/migration/store"
"github.com/sourcegraph/sourcegraph/internal/database/postgresdsn"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/observation"
"github.com/sourcegraph/sourcegraph/lib/output"
)
Expand All @@ -24,13 +25,15 @@ func NewRunnerWithSchemas(
if err != nil {
return nil, err
}
var verbose = env.LogLevel == "dbug"

var dsnsStrings []string
for schema, dsn := range dsns {
dsnsStrings = append(dsnsStrings, schema+" => "+dsn)
}

out.WriteLine(output.Linef(output.EmojiInfo, output.StyleGrey, "Connection DSNs used: %s", strings.Join(dsnsStrings, ", ")))
if verbose {
out.WriteLine(output.Linef(output.EmojiInfo, output.StyleGrey, " Connection DSNs used: %s", strings.Join(dsnsStrings, ", ")))
}

storeFactory := func(db *sql.DB, migrationsTable string) connections.Store {
return connections.NewStoreShim(store.NewWithDB(observationCtx, db, migrationsTable))
Expand Down

0 comments on commit 9238146

Please sign in to comment.