-
Notifications
You must be signed in to change notification settings - Fork 6
/
table_validator.go
98 lines (77 loc) · 2.63 KB
/
table_validator.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
package schema
import (
"context"
"fmt"
)
// Check the validity of the table
type tableValidator struct {
myTable *Table
}
func (x *tableValidator) validate(ctx context.Context, clientMeta *ClientMeta, parentTable *Table, myTable *Table) *Diagnostics {
x.myTable = myTable
diagnostics := NewDiagnostics()
// table name
tableNameLimit := 60
if myTable.TableName == "" {
diagnostics.AddErrorMsg(x.buildMsg("table name must not be empty"))
} else if len(myTable.TableName) > tableNameLimit {
diagnostics.AddErrorMsg(x.buildMsg(fmt.Sprintf("table name length cannot greater than %d", tableNameLimit)))
}
// table description
//if myTable.Description == "" {
// diagnostics.AddErrorMsg(x.buildMsg("it is recommended to add description for table"))
//}
// table name uniq
columnNameSet := make(map[string]struct{}, 0)
for _, column := range myTable.Columns {
if _, exists := columnNameSet[column.ColumnName]; exists {
diagnostics.AddErrorMsg(x.buildMsg("cannot have columns with the same name"))
}
columnNameSet[column.ColumnName] = struct{}{}
// column
diagnostics.AddDiagnostics(column.Runtime().validator.validate(ctx, clientMeta, parentTable, myTable, column))
}
if myTable.Options != nil {
if myTable.Options.PrimaryKeys != nil {
for _, columnName := range myTable.Options.PrimaryKeys {
if !myTable.runtime.ContainsColumnName(columnName) {
diagnostics.AddErrorMsg(x.buildMsg(fmt.Sprintf("PrimaryKeys: table %s does not contain column %s", myTable.TableName, columnName)))
}
}
}
if myTable.Options.Indexes != nil {
for _, tableIndex := range myTable.Options.Indexes {
for _, columnName := range tableIndex.ColumnNames {
if !myTable.runtime.ContainsColumnName(columnName) {
diagnostics.AddErrorMsg(x.buildMsg(fmt.Sprintf("Index: table %s does not contain column %s", myTable.TableName, columnName)))
}
}
}
}
// do not validate fk, because can not access provider in here
//if myTable.Options.ForeignKeys != nil {
// // check foreign keys exists
// for _, fk := range myTable.Options.ForeignKeys {
// diagnostics.Add(x.validateForeignKey(fk))
// }
//}
}
if parentTable != nil {
// do what?
}
// sub table recursive check
for _, subTable := range myTable.SubTables {
diagnostics.AddDiagnostics(subTable.runtime.validator.validate(ctx, clientMeta, myTable, subTable))
}
return diagnostics
}
//func (x *tableValidator) validateForeignKey(fk *TableForeignKey) *Diagnostics {
//
// // fk table exists
//
//
// return nil
//}
func (x *tableValidator) buildMsg(msg string) string {
return fmt.Sprintf("table %s validate error: %s", x.myTable.TableName, msg)
}