-
Notifications
You must be signed in to change notification settings - Fork 18
/
clickhouse_migration_test.go
165 lines (157 loc) · 3.73 KB
/
clickhouse_migration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package Ch
import (
"database/sql"
"fmt"
"log"
"os"
"testing"
_ "github.com/ClickHouse/clickhouse-go/v2"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/assert"
"github.com/kokizzu/gotro/L"
)
var globalPool *dockertest.Pool
func prepareDb(onReady func(db *sql.DB) int) {
const dockerRepo = `yandex/clickhouse-server`
const dockerVer = `latest`
const chPort = `9000/tcp`
const dbDriver = "clickhouse"
const dbConnStr = "tcp://127.0.0.1:%s?debug=true"
var err error
if globalPool == nil {
globalPool, err = dockertest.NewPool("")
if err != nil {
log.Printf("Could not connect to docker: %s\n", err)
return
}
}
resource, err := globalPool.Run(dockerRepo, dockerVer, []string{})
if err != nil {
log.Printf("Could not start resource: %s\n", err)
return
}
var db *sql.DB
if err := globalPool.Retry(func() error {
var err error
connStr := fmt.Sprintf(dbConnStr, resource.GetPort(chPort))
reconnect = func() *sql.DB {
db, err = sql.Open(dbDriver, connStr)
L.IsError(err, `sql.Open: `+connStr)
return db
}
reconnect()
if err != nil {
return err
}
return db.Ping()
}); err != nil {
log.Printf("Could not connect to docker: %s\n", err)
return
}
code := onReady(db)
if err := globalPool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
os.Exit(code)
}
var dbConn *sql.DB
var reconnect func() *sql.DB
func TestMain(m *testing.M) {
prepareDb(func(db *sql.DB) int {
dbConn = db
if db != nil {
return m.Run()
}
return 0
})
}
func TestMigration(t *testing.T) {
a := Adapter{dbConn, reconnect}
const tableName = `test1`
t.Run(`create test must ok`, func(t *testing.T) {
ok := a.UpsertTable(tableName, &TableProp{
Fields: []Field{
{`id`, UInt64},
{`nom`, String},
},
Orders: []string{`id`},
Engine: ReplacingMergeTree,
})
assert.True(t, ok)
})
t.Run(`rename column must ok`, func(t *testing.T) {
ok := a.UpsertTable(tableName, &TableProp{
Fields: []Field{
{`id`, UInt64},
{`name`, String}, // from nom
},
Orders: []string{`id`},
Engine: ReplacingMergeTree,
})
assert.True(t, ok)
})
t.Run(`add column must ok`, func(t *testing.T) {
ok := a.UpsertTable(tableName, &TableProp{
Fields: []Field{
{`id`, UInt64},
{`name`, String},
{`age`, Int32}, // 1 more col
},
Orders: []string{`id`},
Engine: ReplacingMergeTree,
})
assert.True(t, ok)
})
t.Run(`change non PK column data type must ok`, func(t *testing.T) {
ok := a.UpsertTable(tableName, &TableProp{
Fields: []Field{
{`id`, UInt64},
{`name`, String},
{`age`, Float32}, // to float32
},
Orders: []string{`id`},
Engine: ReplacingMergeTree,
})
assert.True(t, ok)
})
t.Run(`add 2 columns must ok`, func(t *testing.T) {
ok := a.UpsertTable(tableName, &TableProp{
Fields: []Field{
{`id`, UInt64},
{`name`, String},
{`age`, Float32},
{`a`, Int32}, // 2 more cols
{`b`, Int64},
},
Orders: []string{`id`},
Engine: ReplacingMergeTree,
})
assert.True(t, ok)
})
t.Run(`create materialized view must ok`, func(t *testing.T) {
const mvName = `test_mv`
const mvSourceTable = `mv_source_table`
ok := a.UpsertTable(mvSourceTable, &TableProp{
Fields: []Field{
{`id`, UInt32},
{`created_at`, DateTime},
{`name`, String},
},
Engine: ReplacingMergeTree,
Partitions: []string{`id`},
Orders: []string{`id`, `created_at`},
})
assert.True(t, ok)
ok = a.CreateMaterializedViews(mvName, &MVProp{
SourceTable: mvSourceTable,
SourceColumns: []string{
`toDate(toHour(created_at)) AS created_at_hour`,
`*`,
},
Engine: ReplacingMergeTree,
Partitions: []string{`id`},
Orders: []string{`id`, `created_at_hour`},
})
assert.True(t, ok)
})
}