-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddlm2s.go
238 lines (227 loc) · 7.3 KB
/
ddlm2s.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
package ddlm2s
import (
"fmt"
"sort"
"strings"
"github.com/jinzhu/inflection"
"github.com/k0kubun/pp"
"github.com/knocknote/vitess-sqlparser/sqlparser"
)
func Convert(sqls string, debug, enableInterleave bool) {
var all CreateStatements
for _, sql := range strings.Split(sqls, ";") {
c := convert(sql, debug, enableInterleave)
if c != nil {
all = append(all, c)
}
}
if enableInterleave {
sort.Slice(all, func(i, j int) bool { return all[i].InterLeaveDepth(all) < all[j].InterLeaveDepth(all) })
}
var updatedAll CreateStatements
for _, c := range all {
if enableInterleave {
c.updatePrimaryKeyByInterleaveParent(updatedAll)
}
updatedAll = append(updatedAll, c)
c.Print(debug)
}
}
func convert(sql string, debug, enableInterleave bool) *CreateStatement {
sql = strings.Replace(sql, "\n", "", -1)
if sql == "" {
return nil
}
stmt, err := sqlparser.Parse(sql)
if err != nil {
panic(err)
}
stmtC := stmt.(*sqlparser.CreateTable)
if debug {
fmt.Println("------------------------before-----------------------------")
pp.Print(stmtC)
}
tableName := fmt.Sprintf("%s", stmtC.DDL.NewName.Name)
stmtC.Columns = updateColumns(stmtC, tableName)
constraints, indices := updateConstraints(stmtC.Constraints, tableName, enableInterleave)
stmtC.Constraints = constraints
return &CreateStatement{
stmt: stmtC,
indices: indices,
}
}
// https://cloud.google.com/solutions/migrating-mysql-to-spanner?hl=ja#supported_data_types
func convertType(mysqlType string) string {
if strings.HasPrefix(mysqlType, "int") ||
strings.HasPrefix(mysqlType, "bigint") ||
strings.HasPrefix(mysqlType, "mediumint") ||
strings.HasPrefix(mysqlType, "smallint") ||
strings.HasPrefix(mysqlType, "tinyint") {
return "INT64"
}
if strings.HasPrefix(mysqlType, "bool") {
return "BOOL"
}
if strings.HasPrefix(mysqlType, "float") || strings.HasPrefix(mysqlType, "double") {
return "FLOAT64"
}
if strings.HasPrefix(mysqlType, "decimal") || strings.HasPrefix(mysqlType, "numeric") {
panic("spanner dont support storing numeric data. please use float.")
}
if strings.HasPrefix(mysqlType, "bit") {
return "BYTES"
}
if mysqlType == "date" {
return "DATE"
}
if mysqlType == "datetime" || mysqlType == "timestamp" {
return "TIMESTAMP"
}
if strings.HasPrefix(mysqlType, "char") {
return buildSppnerTypeWithLength(mysqlType, "char", "STRING", 1)
}
if strings.HasPrefix(mysqlType, "varchar") {
return buildSppnerTypeWithLength(mysqlType, "varchar", "STRING", 1)
}
if strings.HasPrefix(mysqlType, "binary") {
return buildSppnerTypeWithLength(mysqlType, "binary", "BYTES", 1)
}
if strings.HasPrefix(mysqlType, "varbinary") {
return buildSppnerTypeWithLength(mysqlType, "varbinary", "BYTES", 1)
}
if strings.HasPrefix(mysqlType, "blob") {
return buildSppnerTypeWithLength(mysqlType, "blob", "BYTES", 65535)
}
if strings.HasPrefix(mysqlType, "tinyblob") {
return buildSppnerTypeWithLength(mysqlType, "blob", "BYTES", 255)
}
if strings.HasPrefix(mysqlType, "text") {
return buildSppnerTypeWithLength(mysqlType, "text", "STRING", 65535)
}
if strings.HasPrefix(mysqlType, "tinytext") {
return buildSppnerTypeWithLength(mysqlType, "text", "STRING", 255)
}
if strings.HasPrefix(mysqlType, "enum") {
// mysql does not have explicit enum element size limt.
// https://dev.mysql.com/doc/refman/5.6/ja/limits-frm-file.html
// this size is my temporary
return "STRING(5)"
}
if strings.HasPrefix(mysqlType, "set") {
// mysql does not have explicit set element size limt.
// https://dev.mysql.com/doc/refman/5.6/ja/limits-frm-file.html
// this size is my temporary
return "ARRAY<STRING(5)>"
}
if strings.HasPrefix(mysqlType, "longblob") || strings.HasPrefix(mysqlType, "mediumblob") {
panic("spanner dont support large blob.please use gcs and have uri column.")
}
if strings.HasPrefix(mysqlType, "longtext") || strings.HasPrefix(mysqlType, "mediumtext") {
panic("spanner dont support large text.please use gcs and have uri column.")
}
if strings.HasPrefix(mysqlType, "json") {
panic("spanner dont support large text.please use gcs and have uri column.")
}
panic("unexpected type.")
return ""
}
func buildSppnerTypeWithLength(orgType, mysqlBaseType, spannerBaseType string, mysqlDefaultLength int) string {
if strings.Contains(orgType, "(") {
return strings.Replace(orgType, mysqlBaseType, spannerBaseType, 1)
} else {
return fmt.Sprintf("%s(%d)", spannerBaseType, mysqlDefaultLength)
}
}
func updateColumns(stmt *sqlparser.CreateTable, tableName string) []*sqlparser.ColumnDef {
columns := stmt.Columns
var newColumns []*sqlparser.ColumnDef
for _, column := range columns {
var options []*sqlparser.ColumnOption
for _, option := range column.Options {
// TODO to index column attribute (ie. UNIQUE)
if option.Type == sqlparser.ColumnOptionAutoIncrement {
continue
}
if option.Type == sqlparser.ColumnOptionDefaultValue {
continue
}
options = append(options, option)
}
column.Options = options
if column.Name == "id" {
column.Name = fmt.Sprintf("%s_id", inflection.Singular(tableName))
}
column.Type = convertType(column.Type)
newColumns = append(newColumns, column)
}
return newColumns
}
func isPkForeiginKey(column *sqlparser.ColumnDef, stmt *sqlparser.CreateTable) bool {
for _, constraint := range stmt.Constraints {
if constraint.Type == sqlparser.ConstraintForeignKey {
for _, key := range constraint.Keys {
strKey := fmt.Sprintf("%v", key)
if strKey == column.Name {
for _, rkey := range constraint.Reference.Keys {
strRKey := fmt.Sprintf("%v", rkey)
if strRKey == "id" {
return true
}
}
}
}
}
}
return false
}
func updateConstraints(constraints []*sqlparser.Constraint, tableName string, enableInterleave bool) ([]*sqlparser.Constraint, []Index) {
var newConstraints []*sqlparser.Constraint
var indices []Index
// spanner table has only one InterLeave.
// ddlm2s convert first fk to interleave clause.
// other fk converted to index.
var interleavedFk *sqlparser.Constraint
CONSTRAINTSFOR:
for _, constraint := range constraints {
switch constraint.Type {
case sqlparser.ConstraintPrimaryKey:
keys := []sqlparser.ColIdent{}
for _, key := range constraint.Keys {
strKey := fmt.Sprintf("%v", key)
if strKey == "id" {
keys = append(keys, sqlparser.NewColIdent(fmt.Sprintf("%s_id", inflection.Singular(tableName))))
} else {
keys = append(keys, key)
}
}
constraint.Keys = keys
case sqlparser.ConstraintKey, sqlparser.ConstraintIndex, sqlparser.ConstraintUniq, sqlparser.ConstraintUniqKey, sqlparser.ConstraintUniqIndex:
indices = append(indices, NewIndex(constraint, tableName))
continue
case sqlparser.ConstraintForeignKey:
if !enableInterleave {
continue
}
if interleavedFk != nil {
indices = append(indices, NewIndex(constraint, tableName))
continue
}
for _, rkey := range constraint.Reference.Keys {
strRKey := fmt.Sprintf("%v", rkey)
if strRKey != "id" {
continue CONSTRAINTSFOR
}
}
interleavedFk = constraint
constraint = &sqlparser.Constraint{
Type: sqlparser.ConstraintInterleave,
Name: constraint.Reference.Name,
Keys: []sqlparser.ColIdent{},
}
case sqlparser.ConstraintFulltext:
panic("spanner dont support FULLTEXT")
}
newConstraints = append(newConstraints, constraint)
}
return newConstraints, indices
}