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 Location option #14

Merged
merged 3 commits into from
Mar 30, 2023
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
5 changes: 5 additions & 0 deletions cmd/mysqlbatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"net/url"
"strings"
"sync"
"time"
Expand All @@ -25,6 +26,7 @@ type Config struct {
Host string
Port int
Database string
Location string

PasswordSSMParameterName string
Fetcher *SSMParameterFetcher
Expand Down Expand Up @@ -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.Location != "" {
params.Set("loc", c.Location)
}
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
}

Expand Down
5 changes: 4 additions & 1 deletion executer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
_ "embed"
"fmt"
"log"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -96,6 +97,7 @@ var testSQL []byte
func TestExecuterExecute(t *testing.T) {
conf := mysqlbatch.NewDefaultConfig()
conf.Password = "mysqlbatch"
conf.Location = "Asia/Tokyo"
e, err := mysqlbatch.New(context.Background(), conf)
require.NoError(t, err)
defer e.Close()
Expand All @@ -112,6 +114,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)
}