forked from hidu/mysql-schema-sync
/
schema.go
136 lines (127 loc) · 3.61 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
package sqlite
import (
"regexp"
"strings"
"github.com/admpub/mysql-schema-sync/internal"
)
var (
indexReg = regexp.MustCompile("^(CREATE (?:UNIQUE )?INDEX )(IF NOT EXISTS )?[`\"]([^`\"]+)[`\"]")
index2Reg = regexp.MustCompile("^CONSTRAINT [`\"]([^`\"]+)[`\"] UNIQUE")
foreignKeyReg = regexp.MustCompile("^CONSTRAINT [`\"]([^`\"]+)[`\"] FOREIGN KEY.+ REFERENCES [`\"]([^`\"]+)[`\"] ")
)
func parseDbIndexLine(line string) *internal.DbIndex {
line = strings.TrimSpace(line)
idx := &internal.DbIndex{
SQL: line,
RelationTables: []string{},
}
//CREATE UNIQUE INDEX `UNQ_forever_process3_name` ON `forever_process3`(`name`,`name2`)
//CREATE INDEX `forever_process3_en` ON `forever_process3`(`en`)
indexMatches := indexReg.FindStringSubmatch(line)
if len(indexMatches) > 0 {
if len(indexMatches[2]) == 0 {
idx.SQL = indexMatches[1] + `IF NOT EXISTS ` + strings.TrimPrefix(line, indexMatches[1]) // 强制加“IF NOT EXISTS”
}
idx.IndexType = internal.IndexTypeIndex
idx.Name = indexMatches[3]
return idx
}
indexMatches = index2Reg.FindStringSubmatch(line)
if len(indexMatches) > 0 {
idx.IndexType = internal.IndexTypeIndex
idx.Name = indexMatches[1]
return idx
}
//PRIMARY KEY ("id"),
//CONSTRAINT "wid" FOREIGN KEY ("wid") REFERENCES "forever_process" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION,
//CONSTRAINT "idx" UNIQUE ("idx") ON CONFLICT FAIL
foreignMatches := foreignKeyReg.FindStringSubmatch(line)
if len(foreignMatches) > 0 {
idx.IndexType = internal.IndexTypeForeignKey
idx.Name = foreignMatches[1]
idx.AddRelationTable(foreignMatches[2])
return idx
}
return nil
}
// ParseSchema parse table's schema
func ParseSchema(schema string) *internal.MySchema {
schema = strings.TrimSpace(schema)
lines := strings.Split(schema, "\n")
mys := internal.NewSchema(schema)
var hasPrimaryKey bool
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
line = strings.TrimRight(line, ",")
line = strings.TrimRight(line, " ")
var quote string
switch line[0] {
case '`':
quote = "`"
case '"':
quote = `"`
}
//println(`[`, line, `]`)
var idx *internal.DbIndex
var isPK bool
if len(quote) > 0 {
index := strings.Index(line[1:], quote)
name := line[1 : index+1]
mys.Fields[name] = line
if !hasPrimaryKey && strings.HasSuffix(line, ` PRIMARY KEY`) {
isPK = true
}
} else if !hasPrimaryKey && strings.HasPrefix(line, `PRIMARY KEY `) {
isPK = true
}
if isPK {
idx = &internal.DbIndex{
SQL: line,
RelationTables: []string{},
IndexType: internal.IndexTypePrimary,
Name: "PRIMARY KEY",
}
hasPrimaryKey = true
} else {
idx = parseDbIndexLine(line)
}
if idx == nil {
continue
}
switch idx.IndexType {
case internal.IndexTypeForeignKey:
mys.ForeignAll[idx.Name] = idx
default:
mys.IndexAll[idx.Name] = idx
}
}
return mys
}
var backets = regexp.MustCompile(`\([^()]+(,[^()]+)+\)`)
func FormatSchema(schema string) string {
tempCleaned := strings.TrimRight(schema, "\r\n")
if !strings.Contains(tempCleaned, "\n") {
p := strings.Index(schema, `(`)
var replaced bool
if p > 0 {
schema2 := backets.ReplaceAllStringFunc(schema[p+1:], func(s string) string {
replaced = true
return strings.ReplaceAll(s, `,`, `#comma#`)
})
schema = schema[:p] + "(\n" + schema2
}
p = strings.LastIndex(schema, `)`)
if p > 0 {
schema = schema[:p] + "\n" + schema[p:]
}
lines := strings.Split(schema, ",")
schema = strings.Join(lines, ",\n")
if replaced {
schema = strings.ReplaceAll(schema, `#comma#`, `,`)
}
}
return schema
}