Skip to content

Commit

Permalink
Merge pull request #73 from lukaszbudnik/dev-v4.0
Browse files Browse the repository at this point in the history
migrator v4.0
  • Loading branch information
lukaszbudnik authored Jan 7, 2020
2 parents c0804c1 + 574ab8f commit 9f7e815
Show file tree
Hide file tree
Showing 41 changed files with 2,402 additions and 2,027 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ services:
- mysql

go:
- "1.10"
- "1.11"
- "1.12"
- "1.13"
Expand Down
47 changes: 0 additions & 47 deletions DOCKER.md

This file was deleted.

4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.11.2-alpine3.8 as builder
FROM golang:1.13.5-alpine3.10 as builder

MAINTAINER Łukasz Budnik [email protected]

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

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

VOLUME ["/data"]
Expand Down
575 changes: 461 additions & 114 deletions README.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions TestDockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM golang:1.11.2-alpine3.8 as builder
FROM golang:1.13.5-alpine3.10 as builder

MAINTAINER Łukasz Budnik [email protected]

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

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

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

VOLUME ["/data"]
Expand Down
29 changes: 20 additions & 9 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"context"
"fmt"
"log"
"runtime"
)

// RequestIDKey is used together with context for setting/getting X-Request-Id
// RequestIDKey is used together with context for setting/getting X-Request-ID
type RequestIDKey struct{}

// ActionKey is used together with context for setting/getting current action
type ActionKey struct{}

// LogError logs error message
func LogError(ctx context.Context, format string, a ...interface{}) string {
return logLevel(ctx, "ERROR", format, a...)
Expand All @@ -22,16 +20,29 @@ func LogInfo(ctx context.Context, format string, a ...interface{}) string {
return logLevel(ctx, "INFO", format, a...)
}

// LogPanic logs error message and panics
// LogPanic logs error message
func LogPanic(ctx context.Context, format string, a ...interface{}) string {
message := logLevel(ctx, "PANIC", format, a...)
panic(message)
return logLevel(ctx, "PANIC", format, a...)
}

// Log logs message with a given level with no request context
func Log(level string, format string, a ...interface{}) string {
_, file, line, _ := runtime.Caller(2)

message := fmt.Sprintf(format, a...)

log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC)
log.Printf("[%v:%v] %v %v", file, line, level, message)
return message
}

func logLevel(ctx context.Context, level string, format string, a ...interface{}) string {
_, file, line, _ := runtime.Caller(2)

requestID := ctx.Value(RequestIDKey{})
action := ctx.Value(ActionKey{})
message := fmt.Sprintf(format, a...)
log.Printf("%v %v [%v] - %v", level, action, requestID, message)

log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC)
log.Printf("[%v:%v] %v requestId=%v %v", file, line, level, requestID, message)
return message
}
24 changes: 11 additions & 13 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,33 @@ package common

import (
"context"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func newTestContext() context.Context {
pc, _, _, _ := runtime.Caller(1)
details := runtime.FuncForPC(pc)

ctx := context.TODO()
ctx = context.WithValue(ctx, RequestIDKey{}, "123")
ctx = context.WithValue(ctx, ActionKey{}, strings.Replace(details.Name(), "github.com/lukaszbudnik/migrator/common.", "", -1))
return ctx
}

func TestLogInfo(t *testing.T) {
message := LogInfo(newTestContext(), "format no params")
assert.Equal(t, "format no params", message)
message := LogInfo(newTestContext(), "success")
assert.Equal(t, "success", message)
}

func TestLogError(t *testing.T) {
message := LogError(newTestContext(), "format no params: %v", 123)
assert.Equal(t, "format no params: 123", message)
message := LogError(newTestContext(), "param=%v", 123)
assert.Equal(t, "param=123", message)
}

func TestLogPanic(t *testing.T) {
assert.Panics(t, func() {
LogPanic(newTestContext(), "format no params: %v", 123)
})
message := LogPanic(newTestContext(), "param=%v", 123456)
assert.Equal(t, "param=123456", message)
}

func TestLog(t *testing.T) {
message := Log("INFO", "param=%v", 456)
assert.Equal(t, "param=456", message)
}
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"reflect"
"strings"

"gopkg.in/validator.v2"
"github.com/go-playground/validator"
"gopkg.in/yaml.v2"
)

// Config represents Migrator's yaml configuration file
type Config struct {
BaseDir string `yaml:"baseDir" validate:"nonzero"`
Driver string `yaml:"driver" validate:"nonzero"`
DataSource string `yaml:"dataSource" validate:"nonzero"`
BaseDir string `yaml:"baseDir" validate:"required"`
Driver string `yaml:"driver" validate:"required"`
DataSource string `yaml:"dataSource" validate:"required"`
TenantSelectSQL string `yaml:"tenantSelectSQL,omitempty"`
TenantInsertSQL string `yaml:"tenantInsertSQL,omitempty"`
SchemaPlaceHolder string `yaml:"schemaPlaceHolder,omitempty"`
Expand All @@ -24,7 +24,6 @@ type Config struct {
TenantScripts []string `yaml:"tenantScripts,omitempty"`
Port string `yaml:"port,omitempty"`
WebHookURL string `yaml:"webHookURL,omitempty"`
WebHookTemplate string `yaml:"webHookTemplate,omitempty"`
WebHookHeaders []string `yaml:"webHookHeaders,omitempty"`
}

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

if err := validator.Validate(config); err != nil {
validate := validator.New()
if err := validate.Struct(config); err != nil {
return nil, err
}

Expand Down
22 changes: 6 additions & 16 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ package config

import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"

"github.com/go-playground/validator"
"github.com/stretchr/testify/assert"
"gopkg.in/validator.v2"
"gopkg.in/yaml.v2"
)

func noopLogger() *log.Logger {
log := log.New(ioutil.Discard, "", 0)
return log
}

func TestFromFile(t *testing.T) {
config, err := FromFile("../test/migrator-test.yaml")
assert.Nil(t, err)
Expand All @@ -29,7 +22,6 @@ func TestFromFile(t *testing.T) {
assert.Equal(t, "8811", config.Port)
assert.Equal(t, "{schema}", config.SchemaPlaceHolder)
assert.Equal(t, "https://slack.com/api/api.test", config.WebHookURL)
assert.Equal(t, `{"text": "{text}","icon_emoji": ":white_check_mark:"}`, config.WebHookTemplate)
assert.Equal(t, []string{"Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l", "Content-Type: application/json", "X-CustomHeader: value1,value2"}, config.WebHookHeaders)
}

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

func TestConfigString(t *testing.T) {
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{}}
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{}}
// check if go naming convention applies
expected := `baseDir: /opt/app/migrations
driver: postgres
Expand All @@ -66,16 +57,15 @@ tenantMigrations:
singleScripts:
- procedures
port: "8181"
webHookURL: https://hooks.slack.com/services/TTT/BBB/XXX
webHookTemplate: '{json: text}'`
webHookURL: https://hooks.slack.com/services/TTT/BBB/XXX`
actual := fmt.Sprintf("%v", config)
assert.Equal(t, expected, actual)
}

func TestConfigReadFromEmptyFileError(t *testing.T) {
config, err := FromFile("../test/empty.yaml")
assert.Nil(t, config)
assert.IsType(t, (validator.ErrorMap)(nil), err, "Should error because of validation errors")
assert.IsType(t, (validator.ValidationErrors)(nil), err, "Should error because of validation errors")
}

func TestConfigReadFromNonExistingFileError(t *testing.T) {
Expand All @@ -85,7 +75,7 @@ func TestConfigReadFromNonExistingFileError(t *testing.T) {
}

func TestConfigFromWrongSyntaxFile(t *testing.T) {
config, err := FromFile("../README.md")
config, err := FromFile("../Dockerfile")
assert.Nil(t, config)
assert.IsType(t, (*yaml.TypeError)(nil), err, "Should panic because of wrong yaml syntax")
assert.IsType(t, (*yaml.TypeError)(nil), err, "Should error because of wrong yaml syntax")
}
Loading

0 comments on commit 9f7e815

Please sign in to comment.