-
Notifications
You must be signed in to change notification settings - Fork 375
/
service.go
143 lines (129 loc) · 3.59 KB
/
service.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package migrator
import (
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"gorm.io/gorm"
)
// Module is the list of Script
type Module struct {
// Name is the name of the module
Name string
// Scripts contains all sql or python scripts in the module
Scripts []*Script
// PythonRequirementsText is the text of python requirements file in the module.
// it is used to install dependencies of python package by "pip install -r requirements.txt -v"
PythonRequirementsText []byte
}
// BaselineEqualCloud check baseline script schema is equal with cloud schema or not
func (m *Module) BaselineEqualCloud(tx *gorm.DB) *Equal {
tableNames := m.BaselineTableNames()
cloud := NewSchema()
for _, tableName := range tableNames {
raw := "SHOW CREATE TABLE " + tableName
this := tx.Raw(raw)
if err := this.Error; err != nil {
return &Equal{
equal: false,
reason: fmt.Sprintf("failed to exec %s", raw),
}
}
var _ig, create string
if err := this.Row().Scan(&_ig, &create); err != nil {
return &Equal{
equal: false,
reason: fmt.Sprintf("failed to Scan create table stmt, raw: %s", raw),
}
}
node, err := parser.New().ParseOneStmt(create, "", "")
if err != nil {
return &Equal{
equal: false,
reason: fmt.Sprintf("failed to ParseOneStmt: %s, raw: %s", create, raw),
}
}
node.Accept(cloud)
}
return m.BaselineSchema().Equal(cloud)
}
func (m *Module) Schema() *Schema {
schema := NewSchema()
for _, script := range m.Scripts {
for _, ddl := range script.DDLNodes() {
ddl.Accept(schema)
}
}
return schema
}
func (m *Module) BaselineSchema() *Schema {
schema := NewSchema()
for _, script := range m.Scripts {
if !script.IsBaseline() {
continue
}
for _, ddl := range script.DDLNodes() {
ddl.Accept(schema)
}
}
return schema
}
func (m *Module) TableNames() []string {
var names []string
for _, script := range m.Scripts {
for _, ddl := range script.DDLNodes() {
if create, ok := ddl.(*ast.CreateTableStmt); ok {
if create.Table != nil {
names = append(names, create.Table.Name.String())
}
}
}
}
return names
}
func (m *Module) BaselineTableNames() []string {
var names []string
for _, script := range m.Scripts {
if !script.IsBaseline() {
continue
}
for _, ddl := range script.DDLNodes() {
create, ok := ddl.(*ast.CreateTableStmt)
if !ok || create.Table == nil {
continue
}
names = append(names, create.Table.Name.String())
}
}
return names
}
func (m *Module) Sort() {
sort.Slice(m.Scripts, func(i, j int) bool {
return strings.TrimSuffix(m.Scripts[i].GetName(), filepath.Ext(m.Scripts[i].GetName())) <
strings.TrimSuffix(m.Scripts[j].GetName(), filepath.Ext(m.Scripts[j].GetName()))
})
sort.Slice(m.Scripts, func(i, j int) bool {
return m.Scripts[i].IsBaseline() && !m.Scripts[j].IsBaseline()
})
}
func (m *Module) Filenames() []string {
var names []string
for _, script := range m.Scripts {
names = append(names, script.GetName())
}
return names
}