/
scripts.go
384 lines (341 loc) · 10.5 KB
/
scripts.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package migrator
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/pingcap/parser/ast"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"github.com/erda-project/erda/pkg/database/sqllint"
"github.com/erda-project/erda/pkg/database/sqllint/rules"
"github.com/erda-project/erda/pkg/database/sqlparser/pygrator"
)
const (
patchesModuleName = ".patches"
patchInit = "patch.sql"
)
type ScriptsParameters interface {
// Workdir gets pipeline node workdir
Workdir() string
// MigrationDir gets migration scripts direction from repo, like .dice/migrations or 4.1/sqls
MigrationDir() string
// Modules is the modules for installing.
// if is nil, to install all modules in the MigrationDir()
Modules() []string
Rules() []rules.Ruler
}
// Scripts is the set of Module
type Scripts struct {
Workdir string
Dirname string
ServicesNames []string
Services map[string]*Module
Patches *Module
rulers []rules.Ruler
markPending bool
destructive int
destructiveText string
}
// NewScripts range the directory
func NewScripts(parameters ScriptsParameters) (*Scripts, error) {
var (
modulesNames []string
services = make(map[string]*Module, 0)
)
dirname := filepath.Join(parameters.Workdir(), parameters.MigrationDir())
modulesInfos, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, errors.Wrapf(err, "failed to ReadDir %s", dirname)
}
var (
moduleList = parameters.Modules()
modules = map[string]bool{patchesModuleName: true}
)
for _, moduleName := range moduleList {
if moduleName != "" {
modules[moduleName] = true
}
}
for _, moduleInfo := range modulesInfos {
if !moduleInfo.IsDir() {
continue
}
// specified modules and this service is in specified modules then to continue
if _, ok := modules[moduleInfo.Name()]; len(moduleList) > 0 && !ok {
continue
}
var module Module
module.Name = moduleInfo.Name()
modulesNames = append(modulesNames, moduleInfo.Name())
dirname := filepath.Join(parameters.Workdir(), parameters.MigrationDir(), moduleInfo.Name())
serviceDirInfos, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, errors.Wrapf(err, "failed to ReadDir %s", dirname)
}
for _, fileInfo := range serviceDirInfos {
if fileInfo.IsDir() {
continue
}
// read requirements.txt
if strings.EqualFold(fileInfo.Name(), pygrator.RequirementsFilename) {
module.PythonRequirementsText, err = ioutil.ReadFile(filepath.Join(parameters.Workdir(), parameters.MigrationDir(), moduleInfo.Name(), fileInfo.Name()))
if err != nil {
return nil, err
}
}
// read script (.sql or .py)
if ext := filepath.Ext(fileInfo.Name()); strings.EqualFold(ext, string(ScriptTypeSQL)) || strings.EqualFold(ext, string(ScriptTypePython)) {
script, err := NewScript(parameters.Workdir(), filepath.Join(parameters.MigrationDir(), moduleInfo.Name(), fileInfo.Name()))
if err != nil {
return nil, errors.Wrap(err, "failed to NewScript")
}
module.Scripts = append(module.Scripts, script)
}
}
module.Sort()
services[moduleInfo.Name()] = &module
}
var scritps = &Scripts{
Workdir: parameters.Workdir(),
Dirname: parameters.MigrationDir(),
ServicesNames: modulesNames,
Services: services,
rulers: parameters.Rules(),
markPending: false,
destructive: 0,
}
if module, ok := scritps.Services[patchesModuleName]; ok {
scritps.Patches = module
delete(scritps.Services, patchesModuleName)
} else {
scritps.Patches = new(Module)
}
return scritps, nil
}
func (s *Scripts) Get(serviceName string) ([]*Script, bool) {
module, ok := s.Services[serviceName]
return module.Scripts, ok
}
func (s *Scripts) GetPatches() *Module {
return s.Patches
}
func (s *Scripts) GetScriptByFilename(filename string) (*Module, *Script, bool) {
for _, mod := range s.Services {
script, ok := mod.GetScriptByFilename(filename)
if ok {
return mod, script, true
}
}
return nil, nil, false
}
func (s *Scripts) Lint() error {
if !s.markPending {
return errors.New("scripts did not mark if is pending, please mark it and then do Lint")
}
linter := sqllint.New(s.rulers...)
for moduleName, module := range s.Services {
for _, script := range module.Scripts {
if !script.isBase && script.Pending && script.Type == ScriptTypeSQL {
if err := linter.Input(script.Rawtext, filepath.Join(s.Dirname, moduleName, script.GetName())); err != nil {
return err
}
}
}
}
if len(linter.Errors()) == 0 {
return nil
}
_, _ = fmt.Fprintln(os.Stdout, linter.Report())
for src, es := range linter.Errors() {
logrus.Println(src)
for _, err := range es {
_, _ = fmt.Fprintln(os.Stdout, err)
}
_, _ = fmt.Fprintln(os.Stdout)
}
return errors.New("many lint errors")
}
func (s *Scripts) AlterPermissionLint() error {
for moduleName, module := range s.Services {
if moduleName == patchesModuleName {
continue
}
tableNames := make(map[string]bool)
for _, script := range module.Scripts {
ref := &referencer{tableNames: tableNames, err: nil}
for _, ddl := range script.DDLNodes() {
if ddl.Accept(ref); ref.err != nil {
return errors.Wrapf(ref.err, "filename: %s, text: %s",
filepath.Join(s.Dirname, moduleName, script.GetName()), ddl.Text())
}
}
for _, dml := range script.DMLNodes() {
if dml.Accept(ref); ref.err != nil {
return errors.Wrapf(ref.err, "filename: %s, text: %s",
filepath.Join(s.Dirname, moduleName, script.GetName()), dml.Text())
}
}
}
}
return nil
}
func (s *Scripts) MarkPending(tx *gorm.DB) {
for _, module := range s.Services {
for i := range module.Scripts {
var record HistoryModel
if tx := tx.Where(map[string]interface{}{"filename": module.Scripts[i].GetName()}).
First(&record); tx.Error != nil || tx.RowsAffected == 0 {
module.Scripts[i].Pending = true
} else {
module.Scripts[i].Pending = false
module.Scripts[i].Record = &record
}
}
}
s.markPending = true
}
func (s *Scripts) IgnoreMarkPending() {
s.markPending = true
}
func (s *Scripts) InstalledChangesLint(ctx *context.Context, db *gorm.DB) error {
var (
patchesList []string
missingPatchesList []string
)
for moduleName, module := range s.Services {
for _, script := range module.Scripts {
if script.Record == nil {
script.Record = new(HistoryModel)
}
db := db.Where(map[string]interface{}{"filename": script.GetName()}).First(script.Record)
if db.Error != nil {
continue
}
if script.Checksum() != script.Record.Checksum {
logrus.Warnf("the installed file is changed on local, filename: %s, expected checksum: %s, actual checksum: %s",
script.GetName(), script.Checksum(), script.Record.Checksum)
filename := patchPrefix + script.GetName()
if _, ok := s.Patches.GetScriptByFilename(filename); ok {
logrus.Infof("found patch file and append it to the list, filename: %s", filename)
patchesList = append(patchesList, filename)
} else {
logrus.Errorf("missing path file, filename: %s", filename)
missingPatchesList = append(missingPatchesList, filepath.Join(moduleName, script.GetName()))
}
}
}
}
if len(missingPatchesList) > 0 {
return errors.Errorf("these installed script is changed on local and mising paches: %s", strings.Join(missingPatchesList, ","))
}
*ctx = context.WithValue(*ctx, patchesKey, patchesList)
return nil
}
// SameNameLint lint whether there is same script name in different directories
func (s *Scripts) SameNameLint() error {
// m's key is script file name, value is module name
var m = make(map[string]string)
for curModuleName, module := range s.Services {
for _, script := range module.Scripts {
if moduleName, ok := m[script.GetName()]; ok {
return errors.Errorf("not allowed same script name in different directory, filename: %s, modules: %s, %s",
script.GetName(), curModuleName, moduleName)
} else {
m[script.GetName()] = curModuleName
}
}
}
return nil
}
func (s *Scripts) HasDestructiveOperationInPending() (string, bool) {
if s.destructive == 1 {
return s.destructiveText, true
}
if s.destructive == -1 {
return "", false
}
s.destructive = -1
for _, module := range s.Services {
for _, script := range module.Scripts {
if !script.Pending || script.IsBaseline() {
continue
}
for _, node := range script.Nodes {
switch stmt := node.(type) {
case *ast.DropDatabaseStmt, *ast.DropTableStmt, *ast.TruncateTableStmt:
s.destructive = 1
s.destructiveText = node.Text()
return s.destructiveText, true
case *ast.AlterTableStmt:
for _, spec := range stmt.Specs {
switch spec.Tp {
case ast.AlterTableDropColumn:
s.destructive = 1
s.destructiveText = node.Text()
return s.destructiveText, true
}
}
}
}
}
}
return "", false
}
func (s *Scripts) FreshBaselineModules(db *gorm.DB) map[string]*Module {
var modules = make(map[string]*Module)
for name, mod := range s.Services {
mod := mod.FilterFreshBaseline(db)
if len(mod.Scripts) > 0 {
modules[name] = mod
}
}
return modules
}
type referencer struct {
tableNames map[string]bool
err error
}
func (r *referencer) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch stmt := in.(type) {
case *ast.CreateTableStmt:
r.tableNames[stmt.Table.Name.String()] = true
return in, true
case *ast.TableName:
// if the referenced table name is not in the tableNames, error happened
if _, ok := r.tableNames[stmt.Name.String()]; !ok {
r.err = errors.Errorf("the table you referenced is not exists, may it not created in this module directory")
}
return in, true
default:
return in, false
}
}
func (r *referencer) Leave(in ast.Node) (out ast.Node, visitNext bool) {
if r.err != nil {
return in, false
}
if _, ok := in.(*ast.TableName); ok {
return in, false
}
if _, ok := in.(*ast.CreateTableStmt); ok {
return in, false
}
return in, true
}