Skip to content

Commit

Permalink
Add a fix for old fizz without Timestamps() (#373)
Browse files Browse the repository at this point in the history
* WIP fix old auto-timestamp

* Use plush AST to rewrite fizz files

* Fix case when files are already using t.Timestamps()

* Fix Windows line breaks issue
  • Loading branch information
stanislas-m authored Apr 8, 2019
1 parent 6c35dde commit a3d19f6
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 29 deletions.
2 changes: 1 addition & 1 deletion fix/anko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func Test_Anko(t *testing.T) {
r := require.New(t)
box := packr.New("./fixtures", "./fixtures")
box := packr.New("./fixtures/anko", "./fixtures/anko")
err := box.Walk(func(path string, info packr.File) error {
if strings.HasPrefix(path, "pass") {
t.Run(path, testPass(path, info))
Expand Down
55 changes: 55 additions & 0 deletions fix/auto_timestamps_off.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
33 changes: 33 additions & 0 deletions fix/auto_timestamps_off_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fix

import (
"io/ioutil"
"strings"
"testing"

packr "github.com/gobuffalo/packr/v2"
"github.com/stretchr/testify/require"
)

func Test_AutoTimestampsOff(t *testing.T) {
r := require.New(t)
box := packr.New("./fixtures/auto_timestamps_off/raw", "./fixtures/auto_timestamps_off/raw")
boxPatched := packr.New("./fixtures/auto_timestamps_off/patched", "./fixtures/auto_timestamps_off/patched")

err := box.Walk(func(path string, info packr.File) error {
t.Run(path, func(tt *testing.T) {
rr := require.New(tt)
b, err := ioutil.ReadAll(info)
rr.NoError(err)

body := string(b)
patched, err := AutoTimestampsOff(body)
rr.NoError(err)
expected, err := boxPatched.FindString(path)
rr.NoError(err)
rr.Equal(strings.Replace(expected, "\r", "", -1), patched)
})
return nil
})
r.NoError(err)
}
File renamed without changes.
11 changes: 11 additions & 0 deletions fix/fixtures/auto_timestamps_off/patched/0001_nominal.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create_table("users") {
t.Column("id", "int", {primary: true})
t.Column("name", "string", {})
t.Column("user_name", "string", {"size": 100})
t.Column("alive", "boolean", {"null": true})
t.Column("birth_date", "timestamp", {"null": true})
t.Column("bio", "text", {"null": true})
t.Column("price", "numeric", {"null": true, "default": "1.00"})
t.Column("email", "string", {"default": "[email protected]", "size": 50})
t.Timestamps()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
create_table("users_2", {"timestamps": false}) {
t.Column("id", "int", {primary: true})
t.Column("name", "string", {})
t.Column("user_name", "string", {"size": 100})
t.Column("alive", "boolean", {"null": true})
t.Column("birth_date", "timestamp", {"null": true})
t.Column("bio", "text", {"null": true})
t.Column("price", "numeric", {"null": true, "default": "1.00"})
t.Column("email", "string", {"default": "[email protected]", "size": 50})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create_table("users_3") {
t.Column("id", "int", {primary: true})
t.Column("name", "string", {})
t.Column("user_name", "string", {"size": 100})
t.Column("alive", "boolean", {"null": true})
t.Column("birth_date", "timestamp", {"null": true})
t.Column("bio", "text", {"null": true})
t.Column("price", "numeric", {"null": true, "default": "1.00"})
t.Column("email", "string", {"default": "[email protected]", "size": 50})
t.DisableTimestamps()
}
11 changes: 11 additions & 0 deletions fix/fixtures/auto_timestamps_off/patched/0004_already_patched.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create_table("users_4") {
t.Column("id", "int", {primary: true})
t.Column("name", "string", {})
t.Column("user_name", "string", {"size": 100})
t.Column("alive", "boolean", {"null": true})
t.Column("birth_date", "timestamp", {"null": true})
t.Column("bio", "text", {"null": true})
t.Column("price", "numeric", {"null": true, "default": "1.00"})
t.Column("email", "string", {"default": "[email protected]", "size": 50})
t.Timestamps()
}
10 changes: 10 additions & 0 deletions fix/fixtures/auto_timestamps_off/raw/0001_nominal.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
create_table("users") {
t.Column("id", "int", {primary: true})
t.Column("name", "string", {})
t.Column("user_name", "string", {"size": 100})
t.Column("alive", "boolean", {"null": true})
t.Column("birth_date", "timestamp", {"null": true})
t.Column("bio", "text", {"null": true})
t.Column("price", "numeric", {"null": true, "default": "1.00"})
t.Column("email", "string", {"default": "[email protected]", "size": 50})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
create_table("users_2", {"timestamps": false}) {
t.Column("id", "int", {primary: true})
t.Column("name", "string", {})
t.Column("user_name", "string", {"size": 100})
t.Column("alive", "boolean", {"null": true})
t.Column("birth_date", "timestamp", {"null": true})
t.Column("bio", "text", {"null": true})
t.Column("price", "numeric", {"null": true, "default": "1.00"})
t.Column("email", "string", {"default": "[email protected]", "size": 50})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create_table("users_3") {
t.Column("id", "int", {primary: true})
t.Column("name", "string", {})
t.Column("user_name", "string", {"size": 100})
t.Column("alive", "boolean", {"null": true})
t.Column("birth_date", "timestamp", {"null": true})
t.Column("bio", "text", {"null": true})
t.Column("price", "numeric", {"null": true, "default": "1.00"})
t.Column("email", "string", {"default": "[email protected]", "size": 50})
t.DisableTimestamps()
}
11 changes: 11 additions & 0 deletions fix/fixtures/auto_timestamps_off/raw/0004_already_patched.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create_table("users_4") {
t.Column("id", "int", {primary: true})
t.Column("name", "string", {})
t.Column("user_name", "string", {"size": 100})
t.Column("alive", "boolean", {"null": true})
t.Column("birth_date", "timestamp", {"null": true})
t.Column("bio", "text", {"null": true})
t.Column("price", "numeric", {"null": true, "default": "1.00"})
t.Column("email", "string", {"default": "[email protected]", "size": 50})
t.Timestamps()
}
70 changes: 42 additions & 28 deletions soda/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,52 @@ var fixCmd = &cobra.Command{
if info == nil {
return nil
}
ext := strings.ToLower(filepath.Ext(path))
if ext != ".fizz" {
return nil
}
return fixFizz(path)
})
},
}

b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
func fixFizz(path string) error {
ext := strings.ToLower(filepath.Ext(path))
if ext != ".fizz" {
return nil
}

content := string(b)
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}

fixed, err := fix.Anko(content)
if err != nil {
return err
}
if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
f, err := os.Create(path)
if err != nil {
return err
}
if _, err := f.WriteString(fixed); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
}
content := string(b)

return nil
})
},
// Old anko format
fixed, err := fix.Anko(content)
if err != nil {
return err
}
if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
content = fixed
}

// Rewrite migrations to use t.Timestamps() if necessary
fixed, err = fix.AutoTimestampsOff(content)
if err != nil {
return err
}

if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
f, err := os.Create(path)
if err != nil {
return err
}
if _, err := f.WriteString(fixed); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
}
return nil
}

func init() {
Expand Down

0 comments on commit a3d19f6

Please sign in to comment.