-
Notifications
You must be signed in to change notification settings - Fork 18
/
clickhouse_migration.go
269 lines (243 loc) · 6.89 KB
/
clickhouse_migration.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package Ch
import (
"github.com/kokizzu/gotro/A"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/S"
)
var DEBUG = true
func Descr(args ...any) {
if DEBUG {
L.ParentDescribe(args...)
}
}
// https://clickhouse.tech/docs/en/engines/table-engines/
const MergeTree = `MergeTree`
const ReplacingMergeTree = `ReplacingMergeTree`
const CodecLz4hc = `CODEC(LZ4HC)`
const SummingMergeTree = `SummingMergeTree`
const AggregatingMergeTree = `AggregatingMergeTree`
const CollapsingMergeTree = `CollapsingMergeTree`
const VersionedCollapsingMergeTree = `VersionedCollapsingMergeTree`
const GraphiteMergeTree = `GraphiteMergeTree`
const TinyLog = `TinyLog`
const StripeLog = `StripeLog`
const Log = `Log`
const ODBC = `ODBC`
const JDBC = `JDBC`
const MySQL = `MySQL`
const MongoDB = `MongoDB`
const HDFS = `HDFS`
const S3 = `S3`
const Kafka = `Kafka`
const EmbeddedRocksDB = `EmbeddedRocksDB`
const RabbitMQ = `RabbitMQ`
const PostgreSQL = `PostgreSQL`
const Distributed = `Distributed`
const MaterializedView = `MaterializedView`
const Dictionary = `Dictionary`
const Merge = `Merge`
const File = `File`
const Null = `Null`
const Set = `Set`
const Join = `Join`
const URL = `URL`
const View = `View`
const Memory = `Memory`
const Buffer = `Buffer`
type TableName string
type MaterializedViewName string
/*
SELECT DISTINCT alias_to
FROM system.data_type_families
ORDER BY alias_to ASC
*/
type DataType string
const (
DateTime DataType = `DateTime`
DateTime64 DataType = `DateTime64`
Decimal DataType = `Decimal`
FixedString DataType = `FixedString`
Float32 DataType = `Float32`
Float64 DataType = `Float64`
IPv4 DataType = `IPv4`
IPv6 DataType = `IPv6`
Int16 DataType = `Int16`
Int32 DataType = `Int32`
Int64 DataType = `Int64`
Int8 DataType = `Int8`
String DataType = `String`
UInt16 DataType = `UInt16`
UInt32 DataType = `UInt32`
UInt64 DataType = `UInt64`
UInt8 DataType = `UInt8`
)
type TableProp struct {
Fields []Field
Engine string
Partitions []string
Orders []string
DefaultCodec string
}
type Field struct {
Name string
Type DataType
}
type MVProp struct {
SourceTable string
SourceColumns []string
Engine string
Partitions []string
Orders []string
}
var TypeToConst = map[DataType]string{
DateTime: `Ch.DateTime`,
DateTime64: `Ch.DateTime64`,
Decimal: `Ch.Decimal`,
FixedString: `Ch.FixedString`,
Float32: `Ch.Float32`,
Float64: `Ch.Float64`,
IPv4: `Ch.IPv4`,
IPv6: `Ch.IPv6`,
Int16: `Ch.Int16`,
Int32: `Ch.Int32`,
Int64: `Ch.Int64`,
Int8: `Ch.Int8`,
String: `Ch.String`,
UInt16: `Ch.UInt16`,
UInt32: `Ch.UInt32`,
UInt64: `Ch.UInt64`,
}
func (a *Adapter) CreateTable(tableName TableName, props *TableProp) bool {
query := `
CREATE TABLE IF NOT EXISTS ` + string(tableName) + ` (`
for idx, field := range props.Fields {
query += field.Name + ` ` + string(field.Type) + ` ` + props.DefaultCodec
if idx < len(props.Fields)-1 {
query += `,`
}
}
query += `
) ENGINE = ` + props.Engine + `()`
if len(props.Partitions) > 0 {
query += `
PARTITION BY (` + A.StrJoin(props.Partitions, `, `) + `)`
}
query += `
ORDER BY (` + A.StrJoin(props.Orders, `, `) + `)`
_, err := a.Exec(query)
return !L.IsError(err, `Exec: `+query)
}
func (a *Adapter) UpsertTable(tableName TableName, props *TableProp) bool {
if props.Engine == `` {
props.Engine = ReplacingMergeTree
}
if props.DefaultCodec == `` {
props.DefaultCodec = CodecLz4hc
}
Descr(`CreateTable`, tableName)
if !a.CreateTable(tableName, props) {
return false
}
Descr(`AlterMissingColumns`, tableName)
return a.AlterMissingColumns(tableName, props)
}
func (a *Adapter) MigrateTables(tables map[TableName]*TableProp) {
Descr(`---MigrateTables-Start----------------------------------------------------`)
defer Descr(`---MigrateTables-End--------------------------------------------------------`)
for name, props := range tables {
//Descr(name, props)
a.UpsertTable(name, props)
}
}
func (a *Adapter) AlterMissingColumns(tableName TableName, props *TableProp) bool {
query := `
SELECT
"name",
"type",
"position"
FROM system.columns
WHERE "table" = $1
ORDER BY "position"`
rows, err := a.Query(query, tableName)
if L.IsError(err, `a.Query error: `+query) {
return false
}
pos := 0
hasError := false
for rows.Next() {
name := ``
typ := ``
err := rows.Scan(&name, &typ, &pos)
if L.IsError(err, `rows.Scan(&name, &typ, &pos) error: `+query) {
return false
}
if pos-1 < len(props.Fields) {
idx := pos - 1
field := props.Fields[idx]
if name != field.Name {
query := `ALTER TABLE ` + string(tableName) + ` RENAME COLUMN ` + name + ` TO ` + field.Name
_, err := a.Exec(query)
hasError = hasError || L.IsError(err, query)
}
if typ != string(field.Type) {
query := `ALTER TABLE ` + string(tableName) + ` MODIFY COLUMN ` + field.Name + ` ` + string(field.Type)
_, err := a.Exec(query)
hasError = hasError || L.IsError(err, query)
}
}
}
for ; pos < len(props.Fields); pos++ {
field := props.Fields[pos]
query := `ALTER TABLE ` + string(tableName) + ` ADD COLUMN ` + field.Name + ` ` + string(field.Type) + ` ` + props.DefaultCodec
_, err := a.Exec(query)
hasError = hasError || L.IsError(err, query)
}
return !hasError
}
func CheckClickhouseTables(cConn *Adapter, tables map[TableName]*TableProp) {
for tableName, props := range tables {
tableName := string(tableName)
resp, err := cConn.Query(`SELECT column_name, data_type FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ` + S.Z(tableName))
L.PanicIf(err, `please run TABLE migration on %v for clickhouse`, tableName)
defer resp.Close()
// fetch all columns
columnNameTypes := map[string]string{}
for resp.Next() {
var columnName, dataType string
err := resp.Scan(&columnName, &dataType)
L.PanicIf(err, `please run COLUMN migration on %v for clickhouse: %v`, tableName)
columnNameTypes[columnName] = dataType
}
// check data type match
for _, field := range props.Fields {
existingType := columnNameTypes[field.Name]
if existingType != string(field.Type) {
L.Panic(`please run COLUMN_TYPE %v migration on %v for clickhouse: %v <> %v`, field.Name, tableName, existingType, field.Type)
}
}
}
}
func (a *Adapter) CreateMaterializedViews(mvName MaterializedViewName, props *MVProp) bool {
query := `
CREATE MATERIALIZED VIEW IF NOT EXISTS ` + string(mvName)
query += `
ENGINE = ` + props.Engine + `()`
if len(props.Partitions) > 0 {
query += `
PARTITION BY (` + A.StrJoin(props.Partitions, `, `) + `)`
}
query += `
ORDER BY (` + A.StrJoin(props.Orders, `, `) + `)`
query += `
AS SELECT `
for idx, column := range props.SourceColumns {
query += column
if idx < len(props.SourceColumns)-1 {
query += `, `
}
}
query += `
FROM ` + props.SourceTable
_, err := a.Exec(query)
return !L.IsError(err, `Exec: `+query)
}