Skip to content

Commit

Permalink
merged with develop
Browse files Browse the repository at this point in the history
  • Loading branch information
raoptimus committed Apr 10, 2021
2 parents 131bcee + 6cbaf5d commit d5f5a12
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ POSTGRES_PASSWORD=docker
POSTGRES_DSN=postgres://docker:docker@postgres:5432/docker?sslmode=disable
POSTGRES_MIGRATIONS_PATH=./db/postgresMigration/test_migrates

CLICKHOUSE_DSN=clickhouse://default:@clickhouse:9000/docker?sslmode=disable&compress=true&debug=false
CLICKHOUSE_DSN=clickhouse://default:@clickhouse:9000/docker?sslmode=disable&compress=true&debug=false&cluster=test_cluster
CLICKHOUSE_MIGRATIONS_PATH=./db/clickhouseMigration/test_migrates
28 changes: 28 additions & 0 deletions migrator/db/clickhouseMigration/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package clickhouseMigration

import (
"fmt"
"net/url"
"path"
)

func NormalizeDSN(dsn string) (string, error) {
dsnUrl, err := url.Parse(dsn)
if err != nil {
return dsn, err
}

password, _ := dsnUrl.User.Password()
hostWithPort := dsnUrl.Host
if dsnUrl.Port() == "" {
hostWithPort += ":9000"
}

return fmt.Sprintf("tcp://%s?username=%s&password=%s&database=%s&%s",
hostWithPort,
dsnUrl.User.Username(),
password,
path.Base(dsnUrl.Path),
dsnUrl.RawQuery,
), nil
}
31 changes: 31 additions & 0 deletions migrator/db/clickhouseMigration/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package clickhouseMigration

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNormalizeDSNSuccessfully(t *testing.T) {
var err error
var actualDSN, originDSN, expectedDSN string

for _, data := range getDataProvider() {
originDSN, expectedDSN = data[0], data[1]
actualDSN, err = NormalizeDSN(originDSN)
assert.NoError(t, err)
assert.Equal(t, expectedDSN, actualDSN)
}
}

func getDataProvider() [][]string {
return [][]string{
{
"clickhouse://default:@clickhouse:9000/docker?sslmode=disable&compress=true&debug=false&cluster=test_cluster",
"tcp://clickhouse:9000?username=default&password=&database=docker&sslmode=disable&compress=true&debug=false&cluster=test_cluster",
},
{
"clickhouse://user:pass@clickhouse/docker?sslmode=disable&compress=true&debug=false&cluster=test_cluster",
"tcp://clickhouse:9000?username=user&password=pass&database=docker&sslmode=disable&compress=true&debug=false&cluster=test_cluster",
},
}
}
6 changes: 5 additions & 1 deletion migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ func (s *Service) initPostgres() error {
}

func (s *Service) initClickHouse() error {
dsn := "tcp://" + strings.TrimPrefix(s.options.DSN, "clickhouse://")
dsn, err := clickhouseMigration.NormalizeDSN(s.options.DSN)
if err != nil {
return err
}
fmt.Println(dsn)
connection, err := sql.Open("clickhouse", dsn)
if err != nil {
return err
Expand Down
9 changes: 7 additions & 2 deletions migrator/migrator_clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ func TestMigrateService_ClickHouse_UpDown(t *testing.T) {
m, err := createClickhouseMigrator()
assert.NoError(t, err)

_, err = m.db.Exec("DROP DATABASE default")
_, err = m.db.Exec("DROP DATABASE docker")
assert.NoError(t, err)
_, err = m.db.Exec("CREATE DATABASE default")
_, err = m.db.Exec("CREATE DATABASE docker")
assert.NoError(t, err)

err = m.Up("2")
assert.NoError(t, err)

var count int
err = m.db.QueryRow("SELECT count(*) FROM docker.migration").Scan(&count)
assert.NoError(t, err)
assert.Equal(t, 2+1, count)
}

func createClickhouseMigrator() (*Service, error) {
Expand Down

0 comments on commit d5f5a12

Please sign in to comment.