Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom logger option to schema tool #1943

Merged
merged 4 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tools/common/schema/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ package schema

import (
"github.com/urfave/cli"

"go.temporal.io/server/common/log"
)

// SetupFromConfig sets up schema tables based on the given config
Expand Down Expand Up @@ -58,6 +60,7 @@ func newUpdateConfig(cli *cli.Context) (*UpdateConfig, error) {
config := new(UpdateConfig)
config.SchemaDir = cli.String(CLIOptSchemaDir)
config.TargetVersion = cli.String(CLIOptTargetVersion)
config.Logger = log.NewCLILogger()

if err := validateUpdateConfig(config); err != nil {
return nil, err
Expand All @@ -71,6 +74,7 @@ func newSetupConfig(cli *cli.Context) (*SetupConfig, error) {
config.InitialVersion = cli.String(CLIOptVersion)
config.DisableVersioning = cli.Bool(CLIOptDisableVersioning)
config.Overwrite = cli.Bool(CLIOptOverwrite)
config.Logger = log.NewCLILogger()

if err := validateSetupConfig(config); err != nil {
return nil, err
Expand All @@ -94,6 +98,9 @@ func validateSetupConfig(config *SetupConfig) error {
}
config.InitialVersion = ver
}
if config.Logger == nil {
return NewConfigError("missing logger")
}
return nil
}

Expand All @@ -108,6 +115,9 @@ func validateUpdateConfig(config *UpdateConfig) error {
}
config.TargetVersion = ver
}
if config.Logger == nil {
return NewConfigError("missing logger")
}
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions tools/common/schema/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.temporal.io/server/common/log"
)

type (
Expand All @@ -51,6 +52,9 @@ func (s *HandlerTestSuite) TestValidateSetupConfig() {
config := new(SetupConfig)
s.assertValidateSetupFails(config)

config.Logger = log.NewNoopLogger()
s.assertValidateSetupFails(config)

config.InitialVersion = "0.1"
config.DisableVersioning = true
config.SchemaFilePath = ""
Expand Down Expand Up @@ -87,6 +91,9 @@ func (s *HandlerTestSuite) TestValidateUpdateConfig() {
config := new(UpdateConfig)
s.assertValidateUpdateFails(config)

config.Logger = log.NewNoopLogger()
s.assertValidateUpdateFails(config)

config.SchemaDir = "/tmp"
config.TargetVersion = "abc"
s.assertValidateUpdateFails(config)
Expand Down
33 changes: 21 additions & 12 deletions tools/common/schema/setuptask.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
package schema

import (
"log"
"fmt"
"path/filepath"

"github.com/blang/semver/v4"

"go.temporal.io/server/common/log/tag"
)

// SetupTask represents a task
Expand All @@ -49,7 +51,8 @@ func newSetupSchemaTask(db DB, config *SetupConfig) *SetupTask {
// Run executes the task
func (task *SetupTask) Run() error {
config := task.config
log.Printf("Starting schema setup, config=%+v\n", config)
logger := config.Logger
logger.Info("Starting schema setup", tag.NewAnyTag("config", config))

if config.Overwrite {
err := task.db.DropAllTables()
Expand All @@ -59,7 +62,7 @@ func (task *SetupTask) Run() error {
}

if !config.DisableVersioning {
log.Printf("Setting up version tables\n")
logger.Debug("Setting up version tables")
if err := task.db.CreateSchemaVersionTables(); err != nil {
return err
}
Expand All @@ -75,14 +78,14 @@ func (task *SetupTask) Run() error {
return err
}

log.Println("----- Creating types and tables -----")
logger.Debug("----- Creating types and tables -----")
for _, stmt := range stmts {
log.Println(rmspaceRegex.ReplaceAllString(stmt, " "))
logger.Debug(rmspaceRegex.ReplaceAllString(stmt, " "))
if err := task.db.Exec(stmt); err != nil {
return err
}
}
log.Println("----- Done -----")
logger.Debug("----- Done -----")
}

if !config.DisableVersioning {
Expand All @@ -93,31 +96,37 @@ func (task *SetupTask) Run() error {

currVerParsed, err := semver.ParseTolerant(currVer)
if err != nil {
log.Fatalf("Unable to parse current version %s: %v\n", currVer, err)
logger.Fatal("Unable to parse current version",
tag.NewStringTag("current version", currVer),
tag.Error(err),
)
}

initialVersionParsed, err := semver.ParseTolerant(config.InitialVersion)
if err != nil {
log.Fatalf("Unable to parse initial version %s: %v\n", config.InitialVersion, err)
logger.Fatal("Unable to parse initial version",
tag.NewStringTag("initial version", config.InitialVersion),
tag.Error(err),
)
}

if currVerParsed.GT(initialVersionParsed) {
log.Printf("Current database schema version %v is greater than initial schema version %v. Skip version upgrade\n", currVer, config.InitialVersion)
logger.Debug(fmt.Sprintf("Current database schema version %v is greater than initial schema version %v. Skip version upgrade", currVer, config.InitialVersion))
} else {
log.Printf("Setting initial schema version to %v\n", config.InitialVersion)
logger.Debug(fmt.Sprintf("Setting initial schema version to %v", config.InitialVersion))
err := task.db.UpdateSchemaVersion(config.InitialVersion, config.InitialVersion)
if err != nil {
return err
}
log.Printf("Updating schema update log\n")
logger.Debug("Updating schema update log")
err = task.db.WriteSchemaUpdateLog("0", config.InitialVersion, "", "initial version")
if err != nil {
return err
}
}
}

log.Println("Schema setup complete")
logger.Info("Schema setup complete")

return nil
}
4 changes: 4 additions & 0 deletions tools/common/schema/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ package schema
import (
"fmt"
"regexp"

"go.temporal.io/server/common/log"
)

type (
Expand All @@ -42,6 +44,7 @@ type (
TargetVersion string
SchemaDir string
IsDryRun bool
Logger log.Logger
}
// SetupConfig holds the config
// params need by the SetupTask
Expand All @@ -50,6 +53,7 @@ type (
InitialVersion string
Overwrite bool // overwrite previous data
DisableVersioning bool // do not use schema versioning
Logger log.Logger
jlegrone marked this conversation as resolved.
Show resolved Hide resolved
}

// DB is the database interface that's required to be implemented
Expand Down
21 changes: 12 additions & 9 deletions tools/common/schema/updatetask.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"sort"
"strings"

"go.temporal.io/server/common/log/tag"
)

type (
Expand Down Expand Up @@ -90,8 +91,9 @@ func newUpdateSchemaTask(db DB, config *UpdateConfig) *UpdateTask {
// Run executes the task
func (task *UpdateTask) Run() error {
config := task.config
logger := config.Logger

log.Printf("UpdateSchemeTask started, config=%+v\n", config)
logger.Info("UpdateSchemeTask started", tag.NewAnyTag("config", config))

if config.IsDryRun {
if err := task.setupDryrunDatabase(); err != nil {
Expand All @@ -114,15 +116,15 @@ func (task *UpdateTask) Run() error {
return err
}

log.Printf("UpdateSchemeTask done\n")
logger.Info("UpdateSchemeTask done")

return nil
}

func (task *UpdateTask) executeUpdates(currVer string, updates []changeSet) error {

logger := task.config.Logger
if len(updates) == 0 {
log.Printf("found zero updates from current version %v", currVer)
logger.Debug(fmt.Sprintf("found zero updates from current version %v", currVer))
return nil
}

Expand All @@ -137,23 +139,24 @@ func (task *UpdateTask) executeUpdates(currVer string, updates []changeSet) erro
return err
}

log.Printf("Schema updated from %v to %v\n", currVer, cs.version)
logger.Debug(fmt.Sprintf("Schema updated from %v to %v", currVer, cs.version))
currVer = cs.version
}

return nil
}

func (task *UpdateTask) execStmts(ver string, stmts []string) error {
log.Printf("---- Executing updates for version %v ----\n", ver)
logger := task.config.Logger
logger.Debug(fmt.Sprintf("---- Executing updates for version %v ----", ver))
for _, stmt := range stmts {
log.Println(rmspaceRegex.ReplaceAllString(stmt, " "))
logger.Debug(rmspaceRegex.ReplaceAllString(stmt, " "))
e := task.db.Exec(stmt)
if e != nil {
return fmt.Errorf("error executing statement:%v", e)
}
}
log.Printf("---- Done ----\n")
logger.Debug("---- Done ----")
return nil
}

Expand Down