Skip to content

Commit 9f7e815

Browse files
authored
Merge pull request #73 from lukaszbudnik/dev-v4.0
migrator v4.0
2 parents c0804c1 + 574ab8f commit 9f7e815

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2402
-2027
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ services:
1212
- mysql
1313

1414
go:
15-
- "1.10"
1615
- "1.11"
1716
- "1.12"
1817
- "1.13"

DOCKER.md

Lines changed: 0 additions & 47 deletions
This file was deleted.

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.11.2-alpine3.8 as builder
1+
FROM golang:1.13.5-alpine3.10 as builder
22

33
MAINTAINER Łukasz Budnik [email protected]
44

@@ -14,7 +14,7 @@ RUN cd /go/src/github.com/lukaszbudnik/migrator && \
1414
GIT_COMMIT_SHA=$(git rev-list -1 HEAD) && \
1515
go build -ldflags "-X main.GitCommitDate=$GIT_COMMIT_DATE -X main.GitCommitSha=$GIT_COMMIT_SHA -X main.GitBranch=$GIT_BRANCH"
1616

17-
FROM alpine:3.8
17+
FROM alpine:3.10
1818
COPY --from=builder /go/src/github.com/lukaszbudnik/migrator/migrator /bin
1919

2020
VOLUME ["/data"]

README.md

Lines changed: 461 additions & 114 deletions
Large diffs are not rendered by default.

TestDockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM golang:1.11.2-alpine3.8 as builder
1+
FROM golang:1.13.5-alpine3.10 as builder
22

33
MAINTAINER Łukasz Budnik [email protected]
44

5-
# use "--build-arg SOURCE_BRANCH=migrator-v3" to override at build time
6-
# docker build -f TestDockerfile --build-arg SOURCE_BRANCH=migrator-v3 -t migratortest:v1 .
5+
# use "--build-arg SOURCE_BRANCH=dev" to override at build time
6+
# docker build -f TestDockerfile --build-arg SOURCE_BRANCH=dev -t migrator-local:dev .
77
ARG SOURCE_BRANCH=master
88

99
# git is required
@@ -25,7 +25,7 @@ RUN cd /go/src/github.com/lukaszbudnik/migrator && \
2525
GIT_COMMIT_SHA=$(git rev-list -1 HEAD) && \
2626
go build -ldflags "-X main.GitCommitDate=$GIT_COMMIT_DATE -X main.GitCommitSha=$GIT_COMMIT_SHA -X main.GitBranch=$GIT_BRANCH"
2727

28-
FROM alpine:3.8
28+
FROM alpine:3.10
2929
COPY --from=builder /go/src/github.com/lukaszbudnik/migrator/migrator /bin
3030

3131
VOLUME ["/data"]

common/common.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"runtime"
78
)
89

9-
// RequestIDKey is used together with context for setting/getting X-Request-Id
10+
// RequestIDKey is used together with context for setting/getting X-Request-ID
1011
type RequestIDKey struct{}
1112

12-
// ActionKey is used together with context for setting/getting current action
13-
type ActionKey struct{}
14-
1513
// LogError logs error message
1614
func LogError(ctx context.Context, format string, a ...interface{}) string {
1715
return logLevel(ctx, "ERROR", format, a...)
@@ -22,16 +20,29 @@ func LogInfo(ctx context.Context, format string, a ...interface{}) string {
2220
return logLevel(ctx, "INFO", format, a...)
2321
}
2422

25-
// LogPanic logs error message and panics
23+
// LogPanic logs error message
2624
func LogPanic(ctx context.Context, format string, a ...interface{}) string {
27-
message := logLevel(ctx, "PANIC", format, a...)
28-
panic(message)
25+
return logLevel(ctx, "PANIC", format, a...)
26+
}
27+
28+
// Log logs message with a given level with no request context
29+
func Log(level string, format string, a ...interface{}) string {
30+
_, file, line, _ := runtime.Caller(2)
31+
32+
message := fmt.Sprintf(format, a...)
33+
34+
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC)
35+
log.Printf("[%v:%v] %v %v", file, line, level, message)
36+
return message
2937
}
3038

3139
func logLevel(ctx context.Context, level string, format string, a ...interface{}) string {
40+
_, file, line, _ := runtime.Caller(2)
41+
3242
requestID := ctx.Value(RequestIDKey{})
33-
action := ctx.Value(ActionKey{})
3443
message := fmt.Sprintf(format, a...)
35-
log.Printf("%v %v [%v] - %v", level, action, requestID, message)
44+
45+
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC)
46+
log.Printf("[%v:%v] %v requestId=%v %v", file, line, level, requestID, message)
3647
return message
3748
}

common/common_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,33 @@ package common
22

33
import (
44
"context"
5-
"runtime"
6-
"strings"
75
"testing"
86

97
"github.com/stretchr/testify/assert"
108
)
119

1210
func newTestContext() context.Context {
13-
pc, _, _, _ := runtime.Caller(1)
14-
details := runtime.FuncForPC(pc)
15-
1611
ctx := context.TODO()
1712
ctx = context.WithValue(ctx, RequestIDKey{}, "123")
18-
ctx = context.WithValue(ctx, ActionKey{}, strings.Replace(details.Name(), "github.com/lukaszbudnik/migrator/common.", "", -1))
1913
return ctx
2014
}
2115

2216
func TestLogInfo(t *testing.T) {
23-
message := LogInfo(newTestContext(), "format no params")
24-
assert.Equal(t, "format no params", message)
17+
message := LogInfo(newTestContext(), "success")
18+
assert.Equal(t, "success", message)
2519
}
2620

2721
func TestLogError(t *testing.T) {
28-
message := LogError(newTestContext(), "format no params: %v", 123)
29-
assert.Equal(t, "format no params: 123", message)
22+
message := LogError(newTestContext(), "param=%v", 123)
23+
assert.Equal(t, "param=123", message)
3024
}
3125

3226
func TestLogPanic(t *testing.T) {
33-
assert.Panics(t, func() {
34-
LogPanic(newTestContext(), "format no params: %v", 123)
35-
})
27+
message := LogPanic(newTestContext(), "param=%v", 123456)
28+
assert.Equal(t, "param=123456", message)
29+
}
30+
31+
func TestLog(t *testing.T) {
32+
message := Log("INFO", "param=%v", 456)
33+
assert.Equal(t, "param=456", message)
3634
}

config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"reflect"
77
"strings"
88

9-
"gopkg.in/validator.v2"
9+
"github.com/go-playground/validator"
1010
"gopkg.in/yaml.v2"
1111
)
1212

1313
// Config represents Migrator's yaml configuration file
1414
type Config struct {
15-
BaseDir string `yaml:"baseDir" validate:"nonzero"`
16-
Driver string `yaml:"driver" validate:"nonzero"`
17-
DataSource string `yaml:"dataSource" validate:"nonzero"`
15+
BaseDir string `yaml:"baseDir" validate:"required"`
16+
Driver string `yaml:"driver" validate:"required"`
17+
DataSource string `yaml:"dataSource" validate:"required"`
1818
TenantSelectSQL string `yaml:"tenantSelectSQL,omitempty"`
1919
TenantInsertSQL string `yaml:"tenantInsertSQL,omitempty"`
2020
SchemaPlaceHolder string `yaml:"schemaPlaceHolder,omitempty"`
@@ -24,7 +24,6 @@ type Config struct {
2424
TenantScripts []string `yaml:"tenantScripts,omitempty"`
2525
Port string `yaml:"port,omitempty"`
2626
WebHookURL string `yaml:"webHookURL,omitempty"`
27-
WebHookTemplate string `yaml:"webHookTemplate,omitempty"`
2827
WebHookHeaders []string `yaml:"webHookHeaders,omitempty"`
2928
}
3029

@@ -52,7 +51,8 @@ func FromBytes(contents []byte) (*Config, error) {
5251
return nil, err
5352
}
5453

55-
if err := validator.Validate(config); err != nil {
54+
validate := validator.New()
55+
if err := validate.Struct(config); err != nil {
5656
return nil, err
5757
}
5858

config/config_test.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@ package config
22

33
import (
44
"fmt"
5-
"io/ioutil"
6-
"log"
75
"os"
86
"testing"
97

8+
"github.com/go-playground/validator"
109
"github.com/stretchr/testify/assert"
11-
"gopkg.in/validator.v2"
1210
"gopkg.in/yaml.v2"
1311
)
1412

15-
func noopLogger() *log.Logger {
16-
log := log.New(ioutil.Discard, "", 0)
17-
return log
18-
}
19-
2013
func TestFromFile(t *testing.T) {
2114
config, err := FromFile("../test/migrator-test.yaml")
2215
assert.Nil(t, err)
@@ -29,7 +22,6 @@ func TestFromFile(t *testing.T) {
2922
assert.Equal(t, "8811", config.Port)
3023
assert.Equal(t, "{schema}", config.SchemaPlaceHolder)
3124
assert.Equal(t, "https://slack.com/api/api.test", config.WebHookURL)
32-
assert.Equal(t, `{"text": "{text}","icon_emoji": ":white_check_mark:"}`, config.WebHookTemplate)
3325
assert.Equal(t, []string{"Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l", "Content-Type: application/json", "X-CustomHeader: value1,value2"}, config.WebHookHeaders)
3426
}
3527

@@ -46,12 +38,11 @@ func TestWithEnvFromFile(t *testing.T) {
4638
assert.Equal(t, []string{"tenants"}, config.TenantMigrations)
4739
assert.Equal(t, []string{"public", "ref", "config"}, config.SingleMigrations)
4840
assert.Equal(t, os.Getenv("SHLVL"), config.WebHookURL)
49-
assert.Equal(t, os.Getenv("TERM"), config.WebHookTemplate)
5041
assert.Equal(t, fmt.Sprintf("X-Security-Token: %v", os.Getenv("USER")), config.WebHookHeaders[0])
5142
}
5243

5344
func TestConfigString(t *testing.T) {
54-
config := &Config{"/opt/app/migrations", "postgres", "user=p dbname=db host=localhost", "select abc", "insert into table", ":tenant", []string{"ref"}, []string{"tenants"}, []string{"procedures"}, []string{}, "8181", "https://hooks.slack.com/services/TTT/BBB/XXX", "{json: text}", []string{}}
45+
config := &Config{"/opt/app/migrations", "postgres", "user=p dbname=db host=localhost", "select abc", "insert into table", ":tenant", []string{"ref"}, []string{"tenants"}, []string{"procedures"}, []string{}, "8181", "https://hooks.slack.com/services/TTT/BBB/XXX", []string{}}
5546
// check if go naming convention applies
5647
expected := `baseDir: /opt/app/migrations
5748
driver: postgres
@@ -66,16 +57,15 @@ tenantMigrations:
6657
singleScripts:
6758
- procedures
6859
port: "8181"
69-
webHookURL: https://hooks.slack.com/services/TTT/BBB/XXX
70-
webHookTemplate: '{json: text}'`
60+
webHookURL: https://hooks.slack.com/services/TTT/BBB/XXX`
7161
actual := fmt.Sprintf("%v", config)
7262
assert.Equal(t, expected, actual)
7363
}
7464

7565
func TestConfigReadFromEmptyFileError(t *testing.T) {
7666
config, err := FromFile("../test/empty.yaml")
7767
assert.Nil(t, config)
78-
assert.IsType(t, (validator.ErrorMap)(nil), err, "Should error because of validation errors")
68+
assert.IsType(t, (validator.ValidationErrors)(nil), err, "Should error because of validation errors")
7969
}
8070

8171
func TestConfigReadFromNonExistingFileError(t *testing.T) {
@@ -85,7 +75,7 @@ func TestConfigReadFromNonExistingFileError(t *testing.T) {
8575
}
8676

8777
func TestConfigFromWrongSyntaxFile(t *testing.T) {
88-
config, err := FromFile("../README.md")
78+
config, err := FromFile("../Dockerfile")
8979
assert.Nil(t, config)
90-
assert.IsType(t, (*yaml.TypeError)(nil), err, "Should panic because of wrong yaml syntax")
80+
assert.IsType(t, (*yaml.TypeError)(nil), err, "Should error because of wrong yaml syntax")
9181
}

0 commit comments

Comments
 (0)