From 03a8e73271588714d5fdf47493982481600d51d8 Mon Sep 17 00:00:00 2001 From: mashiike Date: Thu, 30 Mar 2023 16:37:16 +0900 Subject: [PATCH 1/3] test: add last execution time delta test --- executer_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/executer_test.go b/executer_test.go index 5557bcf..12ae6cf 100644 --- a/executer_test.go +++ b/executer_test.go @@ -5,6 +5,7 @@ import ( "context" _ "embed" "fmt" + "log" "strings" "sync/atomic" "testing" @@ -112,6 +113,7 @@ func TestExecuterExecute(t *testing.T) { }) err = e.Execute(bytes.NewReader(testSQL)) require.NoError(t, err) - require.True(t, time.Since(e.LastExecuteTime()) < time.Hour) + log.Println("LastExecuteTime:", e.LastExecuteTime()) + require.InDelta(t, time.Since(e.LastExecuteTime()), 0, float64(5*time.Minute)) require.EqualValues(t, 1, count) } From d556b3961c1e4cfea98c6379ca4748fbf51c3a46 Mon Sep 17 00:00:00 2001 From: mashiike Date: Thu, 30 Mar 2023 16:40:32 +0900 Subject: [PATCH 2/3] fix: add locale config option --- config.go | 10 +++++++++- executer_test.go | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 7c13db6..3cee685 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "net/url" "strings" "sync" "time" @@ -25,6 +26,7 @@ type Config struct { Host string Port int Database string + Locale string PasswordSSMParameterName string Fetcher *SSMParameterFetcher @@ -62,13 +64,19 @@ func (c *Config) GetDSN(ctx context.Context) (string, error) { return "", err } } + params := make(url.Values) + params.Set("parseTime", "true") + if c.Locale != "" { + params.Set("loc", c.Locale) + } return fmt.Sprintf( - "%s:%s@tcp(%s:%d)/%s?parseTime=true", + "%s:%s@tcp(%s:%d)/%s?%s", c.User, password, c.Host, c.Port, c.Database, + params.Encode(), ), nil } diff --git a/executer_test.go b/executer_test.go index 12ae6cf..1249578 100644 --- a/executer_test.go +++ b/executer_test.go @@ -97,6 +97,7 @@ var testSQL []byte func TestExecuterExecute(t *testing.T) { conf := mysqlbatch.NewDefaultConfig() conf.Password = "mysqlbatch" + conf.Locale = "Asia/Tokyo" e, err := mysqlbatch.New(context.Background(), conf) require.NoError(t, err) defer e.Close() From 48b05bfa1adbcb884df6985d9fb0562524226320 Mon Sep 17 00:00:00 2001 From: mashiike Date: Thu, 30 Mar 2023 16:46:12 +0900 Subject: [PATCH 3/3] feat: location parameter --- cmd/mysqlbatch/main.go | 5 +++++ config.go | 6 +++--- executer_test.go | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/mysqlbatch/main.go b/cmd/mysqlbatch/main.go index 80545eb..a5b6c66 100644 --- a/cmd/mysqlbatch/main.go +++ b/cmd/mysqlbatch/main.go @@ -41,6 +41,7 @@ func main() { flag.StringVar(&conf.Password, "password", "", "") flag.StringVar(&conf.Host, "h", "127.0.0.1", "host (default 127.0.0.1)") flag.StringVar(&conf.Host, "host", "", "") + flag.StringVar(&conf.Location, "location", "", "timezone of mysql database system") flag.StringVar(&conf.PasswordSSMParameterName, "password-ssm-parameter-name", "", "pasword ssm parameter name") flag.VisitAll(flagx.EnvToFlagWithPrefix("MYSQLBATCH_")) flag.Parse() @@ -103,6 +104,7 @@ type payload struct { Port *int `json:"port,omitempty"` Host *string `json:"host,omitempty"` Database *string `json:"database,omitempty"` + Location *string `json:"Location,omitempty"` PasswordSSMParameterName *string `json:"password_ssm_parameter_name,omitempty"` } @@ -138,6 +140,9 @@ func (h *handler) Invoke(ctx context.Context, p *payload) (*response, error) { if p.PasswordSSMParameterName != nil { conf.PasswordSSMParameterName = *p.PasswordSSMParameterName } + if p.Location != nil { + conf.Location = *p.Location + } executer, err := mysqlbatch.New(ctx, &conf) if err != nil { return nil, err diff --git a/config.go b/config.go index 3cee685..fb166a0 100644 --- a/config.go +++ b/config.go @@ -26,7 +26,7 @@ type Config struct { Host string Port int Database string - Locale string + Location string PasswordSSMParameterName string Fetcher *SSMParameterFetcher @@ -66,8 +66,8 @@ func (c *Config) GetDSN(ctx context.Context) (string, error) { } params := make(url.Values) params.Set("parseTime", "true") - if c.Locale != "" { - params.Set("loc", c.Locale) + if c.Location != "" { + params.Set("loc", c.Location) } return fmt.Sprintf( "%s:%s@tcp(%s:%d)/%s?%s", diff --git a/executer_test.go b/executer_test.go index 1249578..6c78eae 100644 --- a/executer_test.go +++ b/executer_test.go @@ -97,7 +97,7 @@ var testSQL []byte func TestExecuterExecute(t *testing.T) { conf := mysqlbatch.NewDefaultConfig() conf.Password = "mysqlbatch" - conf.Locale = "Asia/Tokyo" + conf.Location = "Asia/Tokyo" e, err := mysqlbatch.New(context.Background(), conf) require.NoError(t, err) defer e.Close()