/
updated_at_linter.go
220 lines (173 loc) · 5.07 KB
/
updated_at_linter.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
// 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 linters
import (
"bytes"
"strings"
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb/types"
"github.com/erda-project/erda/pkg/database/sqllint/linterror"
"github.com/erda-project/erda/pkg/database/sqllint/rules"
"github.com/erda-project/erda/pkg/database/sqllint/script"
)
const (
updatedAt = "updated_at"
)
type UpdatedAtExistsLinter struct {
baseLinter
}
func NewUpdatedAtExistsLinter(script script.Script) rules.Rule {
return &UpdatedAtExistsLinter{newBaseLinter(script)}
}
func (l *UpdatedAtExistsLinter) Enter(in ast.Node) (ast.Node, bool) {
if l.text == "" || in.Text() != "" {
l.text = in.Text()
}
createStmt, ok := in.(*ast.CreateTableStmt)
if !ok {
return in, true
}
// check updated_at
for _, col := range createStmt.Cols {
if col.Name != nil && strings.EqualFold(col.Name.String(), updatedAt) {
return in, true
}
}
l.err = linterror.New(l.s, l.text, "missing necessary column: updated_at", func(_ []byte) bool {
return false
})
return in, true
}
func (l *UpdatedAtExistsLinter) Leave(in ast.Node) (ast.Node, bool) {
return in, true
}
func (l *UpdatedAtExistsLinter) Error() error {
return l.err
}
type UpdatedAtTypeLinter struct {
baseLinter
}
func NewUpdatedAtTypeLinter(script script.Script) rules.Rule {
return &UpdatedAtTypeLinter{newBaseLinter(script)}
}
func (l *UpdatedAtTypeLinter) Enter(in ast.Node) (ast.Node, bool) {
if l.text == "" || in.Text() != "" {
l.text = in.Text()
}
createStmt, ok := in.(*ast.CreateTableStmt)
if !ok {
return in, true
}
for _, col := range createStmt.Cols {
if col.Name == nil || !strings.EqualFold(col.Name.String(), updatedAt) {
continue
}
// 检查字段类型
if strings.EqualFold(col.Tp.String(), types.DateTimeStr) {
return in, true
}
l.err = linterror.New(l.s, l.text, "column type error: updated_at should be DATETIME", func(line []byte) bool {
return bytes.Contains(bytes.ToLower(line), []byte(updatedAt))
})
return in, true
}
return in, true
}
func (l *UpdatedAtTypeLinter) Leave(in ast.Node) (ast.Node, bool) {
return in, true
}
func (l *UpdatedAtTypeLinter) Error() error {
return l.err
}
type UpdatedAtDefaultValueLinter struct {
baseLinter
}
func NewUpdatedAtDefaultValueLinter(script script.Script) rules.Rule {
return &UpdatedAtDefaultValueLinter{newBaseLinter(script)}
}
func (l *UpdatedAtDefaultValueLinter) Enter(in ast.Node) (ast.Node, bool) {
if l.text == "" || in.Text() != "" {
l.text = in.Text()
}
createStmt, ok := in.(*ast.CreateTableStmt)
if !ok {
return in, true
}
// check updated_at
for _, col := range createStmt.Cols {
if col.Name == nil || !strings.EqualFold(col.Name.String(), updatedAt) {
continue
}
for _, opt := range col.Options {
if opt.Tp == ast.ColumnOptionDefaultValue {
if expr, ok := opt.Expr.(*ast.FuncCallExpr); ok &&
strings.EqualFold(expr.FnName.String(), "CURRENT_TIMESTAMP") {
return in, true
}
}
}
l.err = linterror.New(l.s, l.text, "default value error: updated_at defaults CURRENT_TIMESTAMP", func(line []byte) bool {
return bytes.Contains(bytes.ToLower(line), []byte("updated_at"))
})
return in, true
}
return in, true
}
func (l *UpdatedAtDefaultValueLinter) Leave(in ast.Node) (ast.Node, bool) {
return in, true
}
func (l *UpdatedAtDefaultValueLinter) Error() error {
return l.err
}
type UpdatedAtOnUpdateLinter struct {
baseLinter
}
func NewUpdatedAtOnUpdateLinter(script script.Script) rules.Rule {
return &UpdatedAtOnUpdateLinter{newBaseLinter(script)}
}
func (l *UpdatedAtOnUpdateLinter) Enter(in ast.Node) (ast.Node, bool) {
if l.text == "" || in.Text() != "" {
l.text = in.Text()
}
createStmt, ok := in.(*ast.CreateTableStmt)
if !ok {
return in, true
}
// check updated_at
for _, col := range createStmt.Cols {
if col.Name == nil || strings.ToLower(col.Name.String()) != updatedAt {
continue
}
// check default
for _, opt := range col.Options {
if opt.Tp == ast.ColumnOptionOnUpdate {
if expr, ok := opt.Expr.(*ast.FuncCallExpr); ok &&
strings.EqualFold(expr.FnName.String(), "CURRENT_TIMESTAMP") {
return in, true
}
}
}
l.err = linterror.New(l.s, l.text, "missing necessary column option: ON UPDATE CURRENT_TIMESTAMP for updated_at",
func(line []byte) bool {
return bytes.Contains(bytes.ToLower(line), []byte("updated_at"))
})
return in, true
}
return in, true
}
func (l *UpdatedAtOnUpdateLinter) Leave(in ast.Node) (ast.Node, bool) {
return in, true
}
func (l *UpdatedAtOnUpdateLinter) Error() error {
return l.err
}