forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_timestamps_off.go
55 lines (49 loc) · 1.44 KB
/
auto_timestamps_off.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
package fix
import (
"strings"
"github.com/gobuffalo/plush/ast"
"github.com/gobuffalo/plush/parser"
)
// AutoTimestampsOff adds a t.Timestamps() statement to fizz migrations
// when they still use the implicit auto-timestamp old fizz feature.
func AutoTimestampsOff(content string) (string, error) {
var p *ast.Program
var err error
if p, err = parser.Parse("<% " + content + "%>"); err != nil {
return "", err
}
var pt *ast.Program
if pt, err = parser.Parse("<% t.Timestamps() %>"); err != nil {
return "", err
}
ts := pt.Statements[0].(*ast.ExpressionStatement)
for _, s := range p.Statements {
stmt := s.(*ast.ExpressionStatement)
if function, ok := stmt.Expression.(*ast.CallExpression); ok {
if function.Function.TokenLiteral() == "create_table" {
args := function.Arguments
enableTimestamps := true
if len(args) > 1 {
if v, ok := args[1].(*ast.HashLiteral); ok {
if strings.Contains(v.String(), `"timestamps": false`) {
enableTimestamps = false
}
}
}
for _, bs := range function.Block.Statements {
bstmt := bs.(*ast.ExpressionStatement)
if f, ok := bstmt.Expression.(*ast.CallExpression); ok {
fs := f.Function.String()
if fs == "t.DisableTimestamps" || fs == "t.Timestamps" {
enableTimestamps = false
}
}
}
if enableTimestamps {
function.Block.Statements = append(function.Block.Statements, ts)
}
}
}
}
return p.String(), nil
}