forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
281 lines (249 loc) · 9.36 KB
/
schema.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
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tmutils
import (
"crypto/md5"
"encoding/hex"
"fmt"
"regexp"
"strings"
"github.com/youtube/vitess/go/vt/concurrency"
tabletmanagerdatapb "github.com/youtube/vitess/go/vt/proto/tabletmanagerdata"
)
// This file contains helper methods to deal with Schema information.
const (
// TableBaseTable indicates the table type is a base table.
TableBaseTable = "BASE TABLE"
// TableView indicates the table type is a view.
TableView = "VIEW"
)
// TableDefinitionGetColumn returns the index of a column inside a
// TableDefinition.
func TableDefinitionGetColumn(td *tabletmanagerdatapb.TableDefinition, name string) (index int, ok bool) {
lowered := strings.ToLower(name)
for i, n := range td.Columns {
if lowered == strings.ToLower(n) {
return i, true
}
}
return -1, false
}
// TableDefinitions is a list of TableDefinition, for sorting
type TableDefinitions []*tabletmanagerdatapb.TableDefinition
// Len returns TableDefinitions length.
func (tds TableDefinitions) Len() int {
return len(tds)
}
// Swap used for sorting TableDefinitions.
func (tds TableDefinitions) Swap(i, j int) {
tds[i], tds[j] = tds[j], tds[i]
}
// FilterTables returns a copy which includes only whitelisted tables
// (tables), no blacklisted tables (excludeTables) and optionally
// views (includeViews).
func FilterTables(sd *tabletmanagerdatapb.SchemaDefinition, tables, excludeTables []string, includeViews bool) (*tabletmanagerdatapb.SchemaDefinition, error) {
copy := *sd
copy.TableDefinitions = make([]*tabletmanagerdatapb.TableDefinition, 0, len(sd.TableDefinitions))
// Build a list of regexp to match table names against.
// We only use regexps if the name starts and ends with '/'.
// Otherwise the entry in the arrays is nil, and we use the original
// table name.
var tableRegexps []*regexp.Regexp
if len(tables) > 0 {
tableRegexps = make([]*regexp.Regexp, len(tables))
for i, table := range tables {
if len(table) > 2 && strings.HasPrefix(table, "/") && strings.HasSuffix(table, "/") {
table = table[1 : len(table)-1]
var err error
tableRegexps[i], err = regexp.Compile(table)
if err != nil {
return nil, fmt.Errorf("cannot compile regexp %v for table: %v", table, err)
}
}
}
}
var excludeTableRegexps []*regexp.Regexp
if len(excludeTables) > 0 {
excludeTableRegexps = make([]*regexp.Regexp, len(excludeTables))
for i, table := range excludeTables {
if len(table) > 2 && strings.HasPrefix(table, "/") && strings.HasSuffix(table, "/") {
table = table[1 : len(table)-1]
var err error
excludeTableRegexps[i], err = regexp.Compile(table)
if err != nil {
return nil, fmt.Errorf("cannot compile regexp %v for excludeTable: %v", table, err)
}
}
}
}
for _, table := range sd.TableDefinitions {
// Check it's a table we want.
if len(tables) > 0 {
foundMatch := false
for i, tableRegexp := range tableRegexps {
if tableRegexp == nil {
// Not a regexp, just compare in a
// case insensitive way.
if strings.EqualFold(tables[i], table.Name) {
foundMatch = true
break
}
} else {
if tableRegexp.MatchString(table.Name) {
foundMatch = true
break
}
}
}
if !foundMatch {
continue
}
}
excluded := false
for i, tableRegexp := range excludeTableRegexps {
if tableRegexp == nil {
// Not a regexp, just compare in a
// case insensitive way.
if strings.EqualFold(excludeTables[i], table.Name) {
excluded = true
break
}
} else {
if tableRegexp.MatchString(table.Name) {
excluded = true
break
}
}
}
if excluded {
continue
}
if !includeViews && table.Type == TableView {
continue
}
copy.TableDefinitions = append(copy.TableDefinitions, table)
}
// Regenerate hash over tables because it may have changed.
if copy.Version != "" {
GenerateSchemaVersion(©)
}
return ©, nil
}
// GenerateSchemaVersion return a unique schema version string based on
// its TableDefinitions.
func GenerateSchemaVersion(sd *tabletmanagerdatapb.SchemaDefinition) {
hasher := md5.New()
for _, td := range sd.TableDefinitions {
if _, err := hasher.Write([]byte(td.Schema)); err != nil {
panic(err) // extremely unlikely
}
}
sd.Version = hex.EncodeToString(hasher.Sum(nil))
}
// SchemaDefinitionGetTable returns TableDefinition for a given table name.
func SchemaDefinitionGetTable(sd *tabletmanagerdatapb.SchemaDefinition, table string) (td *tabletmanagerdatapb.TableDefinition, ok bool) {
for _, td := range sd.TableDefinitions {
if td.Name == table {
return td, true
}
}
return nil, false
}
// SchemaDefinitionToSQLStrings converts a SchemaDefinition to an array of SQL strings. The array contains all
// the SQL statements needed for creating the database, tables, and views - in that order.
// All SQL statements will have {{.DatabaseName}} in place of the actual db name.
func SchemaDefinitionToSQLStrings(sd *tabletmanagerdatapb.SchemaDefinition) []string {
sqlStrings := make([]string, 0, len(sd.TableDefinitions)+1)
createViewSQL := make([]string, 0, len(sd.TableDefinitions))
sqlStrings = append(sqlStrings, sd.DatabaseSchema)
for _, td := range sd.TableDefinitions {
if td.Type == TableView {
createViewSQL = append(createViewSQL, td.Schema)
} else {
lines := strings.Split(td.Schema, "\n")
for i, line := range lines {
if strings.HasPrefix(line, "CREATE TABLE `") {
lines[i] = strings.Replace(line, "CREATE TABLE `", "CREATE TABLE `{{.DatabaseName}}`.`", 1)
}
}
sqlStrings = append(sqlStrings, strings.Join(lines, "\n"))
}
}
return append(sqlStrings, createViewSQL...)
}
// DiffSchema generates a report on what's different between two SchemaDefinitions
// including views.
func DiffSchema(leftName string, left *tabletmanagerdatapb.SchemaDefinition, rightName string, right *tabletmanagerdatapb.SchemaDefinition, er concurrency.ErrorRecorder) {
if left == nil && right == nil {
return
}
if left == nil || right == nil {
er.RecordError(fmt.Errorf("%v and %v are different, %s: %v, %s: %v", leftName, rightName, leftName, left, rightName, right))
return
}
if left.DatabaseSchema != right.DatabaseSchema {
er.RecordError(fmt.Errorf("%v and %v don't agree on database creation command:\n%v\n differs from:\n%v", leftName, rightName, left.DatabaseSchema, right.DatabaseSchema))
}
leftIndex := 0
rightIndex := 0
for leftIndex < len(left.TableDefinitions) && rightIndex < len(right.TableDefinitions) {
// extra table on the left side
if left.TableDefinitions[leftIndex].Name < right.TableDefinitions[rightIndex].Name {
er.RecordError(fmt.Errorf("%v has an extra table named %v", leftName, left.TableDefinitions[leftIndex].Name))
leftIndex++
continue
}
// extra table on the right side
if left.TableDefinitions[leftIndex].Name > right.TableDefinitions[rightIndex].Name {
er.RecordError(fmt.Errorf("%v has an extra table named %v", rightName, right.TableDefinitions[rightIndex].Name))
rightIndex++
continue
}
// same name, let's see content
if left.TableDefinitions[leftIndex].Schema != right.TableDefinitions[rightIndex].Schema {
er.RecordError(fmt.Errorf("%v and %v disagree on schema for table %v:\n%v\n differs from:\n%v", leftName, rightName, left.TableDefinitions[leftIndex].Name, left.TableDefinitions[leftIndex].Schema, right.TableDefinitions[rightIndex].Schema))
}
if left.TableDefinitions[leftIndex].Type != right.TableDefinitions[rightIndex].Type {
er.RecordError(fmt.Errorf("%v and %v disagree on table type for table %v:\n%v\n differs from:\n%v", leftName, rightName, left.TableDefinitions[leftIndex].Name, left.TableDefinitions[leftIndex].Type, right.TableDefinitions[rightIndex].Type))
}
leftIndex++
rightIndex++
}
for leftIndex < len(left.TableDefinitions) {
if left.TableDefinitions[leftIndex].Type == TableBaseTable {
er.RecordError(fmt.Errorf("%v has an extra table named %v", leftName, left.TableDefinitions[leftIndex].Name))
}
if left.TableDefinitions[leftIndex].Type == TableView {
er.RecordError(fmt.Errorf("%v has an extra view named %v", leftName, left.TableDefinitions[leftIndex].Name))
}
leftIndex++
}
for rightIndex < len(right.TableDefinitions) {
if right.TableDefinitions[rightIndex].Type == TableBaseTable {
er.RecordError(fmt.Errorf("%v has an extra table named %v", rightName, right.TableDefinitions[rightIndex].Name))
}
if right.TableDefinitions[rightIndex].Type == TableView {
er.RecordError(fmt.Errorf("%v has an extra view named %v", rightName, right.TableDefinitions[rightIndex].Name))
}
rightIndex++
}
}
// DiffSchemaToArray diffs two schemas and return the schema diffs if there is any.
func DiffSchemaToArray(leftName string, left *tabletmanagerdatapb.SchemaDefinition, rightName string, right *tabletmanagerdatapb.SchemaDefinition) (result []string) {
er := concurrency.AllErrorRecorder{}
DiffSchema(leftName, left, rightName, right, &er)
if er.HasErrors() {
return er.ErrorStrings()
}
return nil
}
// SchemaChange contains all necessary information to apply a schema change.
// It should not be sent over the wire, it's just a set of parameters.
type SchemaChange struct {
SQL string
Force bool
AllowReplication bool
BeforeSchema *tabletmanagerdatapb.SchemaDefinition
AfterSchema *tabletmanagerdatapb.SchemaDefinition
}