From 656d5b08aea9ac98886cdabd641876acec12eee6 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Wed, 3 Apr 2019 16:51:08 +0200 Subject: [PATCH 1/4] WIP fix old auto-timestamp --- fix/anko_test.go | 2 +- fix/auto_timestamps_off.go | 47 +++++++++++++++++++ fix/auto_timestamps_off_test.go | 28 +++++++++++ .../pass/0001_with_raw_backticks.anko.fizz | 0 .../{ => anko}/pass/0002_happy.plush.fizz | 0 .../0001_auto_timestamps.fizz | 21 +++++++++ 6 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 fix/auto_timestamps_off.go create mode 100644 fix/auto_timestamps_off_test.go rename fix/fixtures/{ => anko}/pass/0001_with_raw_backticks.anko.fizz (100%) rename fix/fixtures/{ => anko}/pass/0002_happy.plush.fizz (100%) create mode 100644 fix/fixtures/auto_timestamps_off/0001_auto_timestamps.fizz diff --git a/fix/anko_test.go b/fix/anko_test.go index f724d573..c843d578 100644 --- a/fix/anko_test.go +++ b/fix/anko_test.go @@ -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)) diff --git a/fix/auto_timestamps_off.go b/fix/auto_timestamps_off.go new file mode 100644 index 00000000..85347f95 --- /dev/null +++ b/fix/auto_timestamps_off.go @@ -0,0 +1,47 @@ +package fix + +import ( + "fmt" + + "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 + for _, a := range args { + fmt.Println(a) + } + for _, bs := range function.Block.Statements { + bstmt := bs.(*ast.ExpressionStatement) + if f, ok := bstmt.Expression.(*ast.CallExpression); ok { + fmt.Printf("%T\n", f) + } + } + function.Block.Statements = append(function.Block.Statements, ts) + } + } + } + + fmt.Println(p.Statements) + + return p.String(), nil +} diff --git a/fix/auto_timestamps_off_test.go b/fix/auto_timestamps_off_test.go new file mode 100644 index 00000000..95f27a75 --- /dev/null +++ b/fix/auto_timestamps_off_test.go @@ -0,0 +1,28 @@ +package fix + +import ( + "io/ioutil" + "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", "./fixtures/auto_timestamps_off") + + 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) + _, err = AutoTimestampsOff(body) + rr.NoError(err) + }) + return nil + }) + r.NoError(err) +} diff --git a/fix/fixtures/pass/0001_with_raw_backticks.anko.fizz b/fix/fixtures/anko/pass/0001_with_raw_backticks.anko.fizz similarity index 100% rename from fix/fixtures/pass/0001_with_raw_backticks.anko.fizz rename to fix/fixtures/anko/pass/0001_with_raw_backticks.anko.fizz diff --git a/fix/fixtures/pass/0002_happy.plush.fizz b/fix/fixtures/anko/pass/0002_happy.plush.fizz similarity index 100% rename from fix/fixtures/pass/0002_happy.plush.fizz rename to fix/fixtures/anko/pass/0002_happy.plush.fizz diff --git a/fix/fixtures/auto_timestamps_off/0001_auto_timestamps.fizz b/fix/fixtures/auto_timestamps_off/0001_auto_timestamps.fizz new file mode 100644 index 00000000..ab940d26 --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/0001_auto_timestamps.fizz @@ -0,0 +1,21 @@ +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": "foo@example.com", "size": 50}) +} + +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": "foo@example.com", "size": 50}) +} \ No newline at end of file From e9d44f1054eb18a65a23290a5ca0a5c9fa819003 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Fri, 5 Apr 2019 17:55:51 +0200 Subject: [PATCH 2/4] Use plush AST to rewrite fizz files --- fix/auto_timestamps_off.go | 21 ++++++++---- fix/auto_timestamps_off_test.go | 8 +++-- .../0001_auto_timestamps.fizz | 21 ------------ .../patched/0001_auto_timestamps.fizz | 32 ++++++++++++++++++ .../raw/0001_auto_timestamps.fizz | 33 +++++++++++++++++++ 5 files changed, 85 insertions(+), 30 deletions(-) delete mode 100644 fix/fixtures/auto_timestamps_off/0001_auto_timestamps.fizz create mode 100644 fix/fixtures/auto_timestamps_off/patched/0001_auto_timestamps.fizz create mode 100644 fix/fixtures/auto_timestamps_off/raw/0001_auto_timestamps.fizz diff --git a/fix/auto_timestamps_off.go b/fix/auto_timestamps_off.go index 85347f95..9851e2e3 100644 --- a/fix/auto_timestamps_off.go +++ b/fix/auto_timestamps_off.go @@ -1,7 +1,7 @@ package fix import ( - "fmt" + "strings" "github.com/gobuffalo/plush/ast" "github.com/gobuffalo/plush/parser" @@ -27,21 +27,28 @@ func AutoTimestampsOff(content string) (string, error) { if function, ok := stmt.Expression.(*ast.CallExpression); ok { if function.Function.TokenLiteral() == "create_table" { args := function.Arguments - for _, a := range args { - fmt.Println(a) + 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 { - fmt.Printf("%T\n", f) + if f.Function.String() == "t.DisableTimestamps" { + enableTimestamps = false + } } } - function.Block.Statements = append(function.Block.Statements, ts) + if enableTimestamps { + function.Block.Statements = append(function.Block.Statements, ts) + } } } } - fmt.Println(p.Statements) - return p.String(), nil } diff --git a/fix/auto_timestamps_off_test.go b/fix/auto_timestamps_off_test.go index 95f27a75..06f918c1 100644 --- a/fix/auto_timestamps_off_test.go +++ b/fix/auto_timestamps_off_test.go @@ -10,7 +10,8 @@ import ( func Test_AutoTimestampsOff(t *testing.T) { r := require.New(t) - box := packr.New("./fixtures/auto_timestamps_off", "./fixtures/auto_timestamps_off") + 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) { @@ -19,8 +20,11 @@ func Test_AutoTimestampsOff(t *testing.T) { rr.NoError(err) body := string(b) - _, err = AutoTimestampsOff(body) + patched, err := AutoTimestampsOff(body) rr.NoError(err) + expected, err := boxPatched.FindString(path) + rr.NoError(err) + rr.Equal(expected, patched) }) return nil }) diff --git a/fix/fixtures/auto_timestamps_off/0001_auto_timestamps.fizz b/fix/fixtures/auto_timestamps_off/0001_auto_timestamps.fizz deleted file mode 100644 index ab940d26..00000000 --- a/fix/fixtures/auto_timestamps_off/0001_auto_timestamps.fizz +++ /dev/null @@ -1,21 +0,0 @@ -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": "foo@example.com", "size": 50}) -} - -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": "foo@example.com", "size": 50}) -} \ No newline at end of file diff --git a/fix/fixtures/auto_timestamps_off/patched/0001_auto_timestamps.fizz b/fix/fixtures/auto_timestamps_off/patched/0001_auto_timestamps.fizz new file mode 100644 index 00000000..9c0ba02f --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/patched/0001_auto_timestamps.fizz @@ -0,0 +1,32 @@ +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": "foo@example.com", "size": 50}) + t.Timestamps() +} +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": "foo@example.com", "size": 50}) +} +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": "foo@example.com", "size": 50}) + t.DisableTimestamps() +} diff --git a/fix/fixtures/auto_timestamps_off/raw/0001_auto_timestamps.fizz b/fix/fixtures/auto_timestamps_off/raw/0001_auto_timestamps.fizz new file mode 100644 index 00000000..aca5a7f7 --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/raw/0001_auto_timestamps.fizz @@ -0,0 +1,33 @@ +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": "foo@example.com", "size": 50}) +} + +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": "foo@example.com", "size": 50}) +} + +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": "foo@example.com", "size": 50}) + t.DisableTimestamps() +} \ No newline at end of file From 149c4dc40ac172ff88df74f4e2c07b3a57c325c7 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Fri, 5 Apr 2019 21:04:38 +0200 Subject: [PATCH 3/4] Fix case when files are already using t.Timestamps() --- fix/auto_timestamps_off.go | 3 +- .../patched/0001_auto_timestamps.fizz | 32 --------- .../patched/0001_nominal.fizz | 11 +++ .../patched/0002_with_disabled_option.fizz | 10 +++ .../patched/0003_with_disable_timestamps.fizz | 11 +++ .../patched/0004_already_patched.fizz | 11 +++ .../raw/0001_auto_timestamps.fizz | 33 --------- .../auto_timestamps_off/raw/0001_nominal.fizz | 10 +++ .../raw/0002_with_disabled_option.fizz | 10 +++ .../raw/0003_with_disable_timestamps.fizz | 11 +++ .../raw/0004_already_patched.fizz | 11 +++ soda/cmd/fix.go | 70 +++++++++++-------- 12 files changed, 129 insertions(+), 94 deletions(-) delete mode 100644 fix/fixtures/auto_timestamps_off/patched/0001_auto_timestamps.fizz create mode 100644 fix/fixtures/auto_timestamps_off/patched/0001_nominal.fizz create mode 100644 fix/fixtures/auto_timestamps_off/patched/0002_with_disabled_option.fizz create mode 100644 fix/fixtures/auto_timestamps_off/patched/0003_with_disable_timestamps.fizz create mode 100644 fix/fixtures/auto_timestamps_off/patched/0004_already_patched.fizz delete mode 100644 fix/fixtures/auto_timestamps_off/raw/0001_auto_timestamps.fizz create mode 100644 fix/fixtures/auto_timestamps_off/raw/0001_nominal.fizz create mode 100644 fix/fixtures/auto_timestamps_off/raw/0002_with_disabled_option.fizz create mode 100644 fix/fixtures/auto_timestamps_off/raw/0003_with_disable_timestamps.fizz create mode 100644 fix/fixtures/auto_timestamps_off/raw/0004_already_patched.fizz diff --git a/fix/auto_timestamps_off.go b/fix/auto_timestamps_off.go index 9851e2e3..7beb378d 100644 --- a/fix/auto_timestamps_off.go +++ b/fix/auto_timestamps_off.go @@ -38,7 +38,8 @@ func AutoTimestampsOff(content string) (string, error) { for _, bs := range function.Block.Statements { bstmt := bs.(*ast.ExpressionStatement) if f, ok := bstmt.Expression.(*ast.CallExpression); ok { - if f.Function.String() == "t.DisableTimestamps" { + fs := f.Function.String() + if fs == "t.DisableTimestamps" || fs == "t.Timestamps" { enableTimestamps = false } } diff --git a/fix/fixtures/auto_timestamps_off/patched/0001_auto_timestamps.fizz b/fix/fixtures/auto_timestamps_off/patched/0001_auto_timestamps.fizz deleted file mode 100644 index 9c0ba02f..00000000 --- a/fix/fixtures/auto_timestamps_off/patched/0001_auto_timestamps.fizz +++ /dev/null @@ -1,32 +0,0 @@ -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": "foo@example.com", "size": 50}) - t.Timestamps() -} -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": "foo@example.com", "size": 50}) -} -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": "foo@example.com", "size": 50}) - t.DisableTimestamps() -} diff --git a/fix/fixtures/auto_timestamps_off/patched/0001_nominal.fizz b/fix/fixtures/auto_timestamps_off/patched/0001_nominal.fizz new file mode 100644 index 00000000..12329f68 --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/patched/0001_nominal.fizz @@ -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": "foo@example.com", "size": 50}) + t.Timestamps() +} diff --git a/fix/fixtures/auto_timestamps_off/patched/0002_with_disabled_option.fizz b/fix/fixtures/auto_timestamps_off/patched/0002_with_disabled_option.fizz new file mode 100644 index 00000000..1b456174 --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/patched/0002_with_disabled_option.fizz @@ -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": "foo@example.com", "size": 50}) +} diff --git a/fix/fixtures/auto_timestamps_off/patched/0003_with_disable_timestamps.fizz b/fix/fixtures/auto_timestamps_off/patched/0003_with_disable_timestamps.fizz new file mode 100644 index 00000000..15602cbd --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/patched/0003_with_disable_timestamps.fizz @@ -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": "foo@example.com", "size": 50}) + t.DisableTimestamps() +} diff --git a/fix/fixtures/auto_timestamps_off/patched/0004_already_patched.fizz b/fix/fixtures/auto_timestamps_off/patched/0004_already_patched.fizz new file mode 100644 index 00000000..3eddb3d8 --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/patched/0004_already_patched.fizz @@ -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": "foo@example.com", "size": 50}) + t.Timestamps() +} diff --git a/fix/fixtures/auto_timestamps_off/raw/0001_auto_timestamps.fizz b/fix/fixtures/auto_timestamps_off/raw/0001_auto_timestamps.fizz deleted file mode 100644 index aca5a7f7..00000000 --- a/fix/fixtures/auto_timestamps_off/raw/0001_auto_timestamps.fizz +++ /dev/null @@ -1,33 +0,0 @@ -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": "foo@example.com", "size": 50}) -} - -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": "foo@example.com", "size": 50}) -} - -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": "foo@example.com", "size": 50}) - t.DisableTimestamps() -} \ No newline at end of file diff --git a/fix/fixtures/auto_timestamps_off/raw/0001_nominal.fizz b/fix/fixtures/auto_timestamps_off/raw/0001_nominal.fizz new file mode 100644 index 00000000..d0b7b06d --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/raw/0001_nominal.fizz @@ -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": "foo@example.com", "size": 50}) +} diff --git a/fix/fixtures/auto_timestamps_off/raw/0002_with_disabled_option.fizz b/fix/fixtures/auto_timestamps_off/raw/0002_with_disabled_option.fizz new file mode 100644 index 00000000..1b456174 --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/raw/0002_with_disabled_option.fizz @@ -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": "foo@example.com", "size": 50}) +} diff --git a/fix/fixtures/auto_timestamps_off/raw/0003_with_disable_timestamps.fizz b/fix/fixtures/auto_timestamps_off/raw/0003_with_disable_timestamps.fizz new file mode 100644 index 00000000..15602cbd --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/raw/0003_with_disable_timestamps.fizz @@ -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": "foo@example.com", "size": 50}) + t.DisableTimestamps() +} diff --git a/fix/fixtures/auto_timestamps_off/raw/0004_already_patched.fizz b/fix/fixtures/auto_timestamps_off/raw/0004_already_patched.fizz new file mode 100644 index 00000000..3eddb3d8 --- /dev/null +++ b/fix/fixtures/auto_timestamps_off/raw/0004_already_patched.fizz @@ -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": "foo@example.com", "size": 50}) + t.Timestamps() +} diff --git a/soda/cmd/fix.go b/soda/cmd/fix.go index 6ce1b78f..75feea23 100644 --- a/soda/cmd/fix.go +++ b/soda/cmd/fix.go @@ -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() { From 9ea5295cd09dfd983a923a30041663e775f041e3 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Fri, 5 Apr 2019 21:34:16 +0200 Subject: [PATCH 4/4] Fix Windows line breaks issue --- fix/auto_timestamps_off_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fix/auto_timestamps_off_test.go b/fix/auto_timestamps_off_test.go index 06f918c1..4438e635 100644 --- a/fix/auto_timestamps_off_test.go +++ b/fix/auto_timestamps_off_test.go @@ -2,6 +2,7 @@ package fix import ( "io/ioutil" + "strings" "testing" packr "github.com/gobuffalo/packr/v2" @@ -24,7 +25,7 @@ func Test_AutoTimestampsOff(t *testing.T) { rr.NoError(err) expected, err := boxPatched.FindString(path) rr.NoError(err) - rr.Equal(expected, patched) + rr.Equal(strings.Replace(expected, "\r", "", -1), patched) }) return nil })