-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a fix for old fizz without Timestamps() (#373)
* 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
1 parent
6c35dde
commit a3d19f6
Showing
14 changed files
with
216 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
11 changes: 11 additions & 0 deletions
11
fix/fixtures/auto_timestamps_off/patched/0001_nominal.fizz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
10 changes: 10 additions & 0 deletions
10
fix/fixtures/auto_timestamps_off/patched/0002_with_disabled_option.fizz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
11 changes: 11 additions & 0 deletions
11
fix/fixtures/auto_timestamps_off/patched/0003_with_disable_timestamps.fizz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
fix/fixtures/auto_timestamps_off/patched/0004_already_patched.fizz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
10 changes: 10 additions & 0 deletions
10
fix/fixtures/auto_timestamps_off/raw/0002_with_disabled_option.fizz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
11 changes: 11 additions & 0 deletions
11
fix/fixtures/auto_timestamps_off/raw/0003_with_disable_timestamps.fizz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
fix/fixtures/auto_timestamps_off/raw/0004_already_patched.fizz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters