From 282f5af0e75bb36c86a1d4806a7496378144de9b Mon Sep 17 00:00:00 2001 From: Fs02 Date: Wed, 23 Feb 2022 15:10:58 +0900 Subject: [PATCH 1/4] extract migrator package --- cmd/rel/internal/migrate.go | 4 +- cmd/rel/internal/testdata/rel-1545325035.go | 71 ++++++ go.mod | 4 +- go.sum | 17 +- migrator/migrator_test.go | 230 -------------------- 5 files changed, 81 insertions(+), 245 deletions(-) create mode 100644 cmd/rel/internal/testdata/rel-1545325035.go delete mode 100644 migrator/migrator_test.go diff --git a/cmd/rel/internal/migrate.go b/cmd/rel/internal/migrate.go index 981745ac..e46b4827 100644 --- a/cmd/rel/internal/migrate.go +++ b/cmd/rel/internal/migrate.go @@ -26,7 +26,7 @@ import ( _ "{{.Driver}}" db "{{.Adapter}}" "github.com/go-rel/rel" - "github.com/go-rel/rel/migrator" + "github.com/go-rel/migration" "{{.Package}}" ) @@ -72,7 +72,7 @@ func main() { var ( repo = rel.New(adapter) - m = migrator.New(repo) + m = migration.New(repo) ) log.SetFlags(0) diff --git a/cmd/rel/internal/testdata/rel-1545325035.go b/cmd/rel/internal/testdata/rel-1545325035.go new file mode 100644 index 00000000..db073375 --- /dev/null +++ b/cmd/rel/internal/testdata/rel-1545325035.go @@ -0,0 +1,71 @@ + +package main + +import ( + "context" + "log" + "strings" + "time" + + _ "github.com/mattn/go-sqlite3" + db "github.com/go-rel/sqlite3" + "github.com/go-rel/rel" + "github.com/go-rel/migration" + + "github.com/go-rel/rel/cmd/rel/internal/testdata/migrations" +) + +var ( + shutdowns []func() error +) + +func logger(ctx context.Context, op string, message string) func(err error) { + // no op for rel functions. + if strings.HasPrefix(op, "rel-") { + return func(error) {} + } + + if op == "migrate" || op == "rollback" { + log.Print("Running: ", op, " ", message) + } + + t := time.Now() + return func(err error) { + duration := time.Since(t) + if op == "migrate" || op == "rollback" { + log.Print("=> Done: ", op, " ", message, " in ", duration) + } else if false { + log.Print("\t[duration: ", duration, " op: ", op, "] ", message) + } + + if err != nil { + log.Println("\tError: ", op, " ", err) + } + } +} + +func main() { + var ( + ctx = context.Background() + ) + + adapter, err := db.Open(":memory:") + if err != nil { + log.Fatal(err) + } + + var ( + repo = rel.New(adapter) + m = migrator.New(repo) + ) + + log.SetFlags(0) + repo.Instrumentation(logger) + m.Instrumentation(logger) + + + m.Register(1, migrations.MigrateCreateSamples, migrations.RollbackCreateSamples) + + + m.Migrate(ctx) +} diff --git a/go.mod b/go.mod index cb92fa5a..9fbb5f82 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,11 @@ module github.com/go-rel/rel require ( - github.com/go-rel/reltest v0.6.0 github.com/jinzhu/inflection v1.0.0 + github.com/onsi/ginkgo v1.15.0 // indirect + github.com/onsi/gomega v1.10.5 // indirect github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e + github.com/stretchr/objx v0.3.0 // indirect github.com/stretchr/testify v1.7.0 github.com/subosito/gotenv v1.2.0 ) diff --git a/go.sum b/go.sum index deece817..7bbb9ad1 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +1,9 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/go-rel/rel v0.28.0/go.mod h1:zaIYPmM3AfJrh0xBmm7KoVKRgTNvr0cgZfcJ88gVA2U= -github.com/go-rel/rel v0.30.0/go.mod h1:Wu6HB6x0oIiEOdUJClwmn7ByCmdL73U5gItAYObyyuo= -github.com/go-rel/rel v0.31.0/go.mod h1:fCMlDiwDSSBSoZQ6ovUyjSqS2FfpLnhwr6tW9TgnhC8= -github.com/go-rel/reltest v0.4.0/go.mod h1:3udgrKCZGCMFWc8k+RH975gUdUFPE/yspG2iWN9gjqU= -github.com/go-rel/reltest v0.5.0/go.mod h1:onixPXWMVL+KILH7OdUufKDm1KN0H6FzXOP7OoNC9qM= -github.com/go-rel/reltest v0.6.0 h1:mzF49w6jo1eyvfbpogL6BvxCuJrMwsjD+49qI9gCyEs= -github.com/go-rel/reltest v0.6.0/go.mod h1:4jR+L2vn7qe6NDW1carPomp2SlbTsQ9dqkxC+n4db+k= -github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= @@ -26,8 +19,6 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -42,8 +33,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e h1:zWKUYT07mGmVBH+9UgnHXd/ekCK99C8EbDSAt5qsjXE= github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As= +github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= diff --git a/migrator/migrator_test.go b/migrator/migrator_test.go deleted file mode 100644 index deab2fff..00000000 --- a/migrator/migrator_test.go +++ /dev/null @@ -1,230 +0,0 @@ -package migrator - -import ( - "context" - "errors" - "testing" - - "github.com/go-rel/rel" - "github.com/go-rel/reltest" - "github.com/stretchr/testify/assert" -) - -func TestMigrator(t *testing.T) { - var ( - ctx = context.TODO() - repo = reltest.New() - migrator = New(repo) - ) - - t.Run("Register", func(t *testing.T) { - migrator.Register(20200829084000, - func(schema *rel.Schema) { - schema.CreateTable("users", func(t *rel.Table) { - t.ID("id") - }) - }, - func(schema *rel.Schema) { - schema.DropTable("users") - }, - ) - - migrator.Register(20200828100000, - func(schema *rel.Schema) { - schema.CreateTable("tags", func(t *rel.Table) { - t.ID("id") - }) - - schema.Do(func(repo rel.Repository) error { - assert.NotNil(t, repo) - return nil - }) - }, - func(schema *rel.Schema) { - schema.DropTable("tags") - }, - ) - - migrator.Register(20200829115100, - func(schema *rel.Schema) { - schema.CreateTable("books", func(t *rel.Table) { - t.ID("id") - }) - }, - func(schema *rel.Schema) { - schema.DropTable("books") - }, - ) - - assert.Len(t, migrator.versions, 3) - assert.Equal(t, 20200829084000, migrator.versions[0].Version) - assert.Equal(t, 20200828100000, migrator.versions[1].Version) - assert.Equal(t, 20200829115100, migrator.versions[2].Version) - - repo.AssertExpectations(t) - }) - - t.Run("Migrate", func(t *testing.T) { - repo.ExpectFindAll(rel.UsePrimary().SortAsc("version")). - Result(versions{{ID: 1, Version: 20200829115100}}) - - repo.ExpectTransaction(func(repo *reltest.Repository) { - repo.ExpectInsert().For(&version{Version: 20200828100000}) - }) - - repo.ExpectTransaction(func(repo *reltest.Repository) { - repo.ExpectInsert().For(&version{Version: 20200829084000}) - }) - - migrator.Migrate(ctx) - repo.AssertExpectations(t) - }) - - t.Run("Rollback", func(t *testing.T) { - repo.ExpectFindAll(rel.UsePrimary().SortAsc("version")). - Result(versions{ - {ID: 1, Version: 20200828100000}, - {ID: 2, Version: 20200829084000}, - }) - - assert.Equal(t, 20200829084000, migrator.versions[1].Version) - - repo.ExpectTransaction(func(repo *reltest.Repository) { - repo.ExpectDelete().For(&migrator.versions[1]) - }) - - migrator.Rollback(ctx) - repo.AssertExpectations(t) - }) -} - -func TestMigrator_Sync(t *testing.T) { - var ( - ctx = context.TODO() - repo = reltest.New() - nfn = func(schema *rel.Schema) {} - ) - - tests := []struct { - name string - applied versions - synced versions - isPanic bool - }{ - { - name: "all migrated", - applied: versions{ - {ID: 1, Version: 1}, - {ID: 2, Version: 2}, - {ID: 3, Version: 3}, - }, - synced: versions{ - {ID: 1, Version: 1, applied: true}, - {ID: 2, Version: 2, applied: true}, - {ID: 3, Version: 3, applied: true}, - }, - }, - { - name: "not migrated", - applied: versions{}, - synced: versions{ - {ID: 0, Version: 1, applied: false}, - {ID: 0, Version: 2, applied: false}, - {ID: 0, Version: 3, applied: false}, - }, - }, - { - name: "first not migrated", - applied: versions{ - {ID: 2, Version: 2}, - {ID: 3, Version: 3}, - }, - synced: versions{ - {ID: 0, Version: 1, applied: false}, - {ID: 2, Version: 2, applied: true}, - {ID: 3, Version: 3, applied: true}, - }, - }, - { - name: "middle not migrated", - applied: versions{ - {ID: 1, Version: 1}, - {ID: 3, Version: 3}, - }, - synced: versions{ - {ID: 1, Version: 1, applied: true}, - {ID: 0, Version: 2, applied: false}, - {ID: 3, Version: 3, applied: true}, - }, - }, - { - name: "last not migrated", - applied: versions{ - {ID: 1, Version: 1}, - {ID: 2, Version: 2}, - }, - synced: versions{ - {ID: 1, Version: 1, applied: true}, - {ID: 2, Version: 2, applied: true}, - {ID: 0, Version: 3, applied: false}, - }, - }, - { - name: "broken migration", - applied: versions{ - {ID: 1, Version: 1}, - {ID: 2, Version: 2}, - {ID: 3, Version: 3}, - {ID: 4, Version: 4}, - }, - synced: versions{ - {ID: 1, Version: 1, applied: true}, - {ID: 2, Version: 2, applied: true}, - {ID: 3, Version: 3, applied: true}, - }, - isPanic: true, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - migrator := New(repo) - migrator.Register(3, nfn, nfn) - migrator.Register(2, nfn, nfn) - migrator.Register(1, nfn, nfn) - - repo.ExpectFindAll(rel.UsePrimary().SortAsc("version")).Result(test.applied) - - if test.isPanic { - assert.Panics(t, func() { - migrator.sync(ctx) - }) - } else { - assert.NotPanics(t, func() { - migrator.sync(ctx) - }) - - assert.Equal(t, test.synced, migrator.versions) - } - - repo.AssertExpectations(t) - }) - } -} - -func TestMigrator_Instrumentation(t *testing.T) { - var ( - ctx = context.TODO() - repo = reltest.New() - m = New(repo) - ) - - m.Instrumentation(func(context.Context, string, string) func(error) { return nil }) - m.instrumenter.Observe(ctx, "test", "test") -} - -func TestCheck(t *testing.T) { - assert.Panics(t, func() { - check(errors.New("error")) - }) -} From 76c4b99a25e151969558e4794596115bc5150f3d Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 23 Feb 2022 06:13:02 +0000 Subject: [PATCH 2/4] Format code with gofmt --- cmd/rel/internal/testdata/rel-1545325035.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cmd/rel/internal/testdata/rel-1545325035.go b/cmd/rel/internal/testdata/rel-1545325035.go index db073375..160761f3 100644 --- a/cmd/rel/internal/testdata/rel-1545325035.go +++ b/cmd/rel/internal/testdata/rel-1545325035.go @@ -1,4 +1,3 @@ - package main import ( @@ -7,10 +6,10 @@ import ( "strings" "time" - _ "github.com/mattn/go-sqlite3" - db "github.com/go-rel/sqlite3" - "github.com/go-rel/rel" "github.com/go-rel/migration" + "github.com/go-rel/rel" + db "github.com/go-rel/sqlite3" + _ "github.com/mattn/go-sqlite3" "github.com/go-rel/rel/cmd/rel/internal/testdata/migrations" ) @@ -63,9 +62,7 @@ func main() { repo.Instrumentation(logger) m.Instrumentation(logger) - m.Register(1, migrations.MigrateCreateSamples, migrations.RollbackCreateSamples) - m.Migrate(ctx) } From f76baf3aca5725a64f0ffe2d28b28f39a6d5564d Mon Sep 17 00:00:00 2001 From: Fs02 Date: Wed, 23 Feb 2022 15:13:27 +0900 Subject: [PATCH 3/4] add deprecation notice --- migrator/migrator.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/migrator/migrator.go b/migrator/migrator.go index 85f33dd5..9638f111 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -42,6 +42,8 @@ func (v versions) Swap(i, j int) { } // Migrator is a migration manager that handles migration logic. +// +// Deprecated: use github.com/go-rel/migration instead type Migrator struct { repo rel.Repository instrumenter rel.Instrumenter From e125764627294cdf85ca66a609ffd9ba9cf2c84b Mon Sep 17 00:00:00 2001 From: Surya Asriadie Date: Wed, 23 Feb 2022 15:39:40 +0900 Subject: [PATCH 4/4] Delete rel-1545325035.go --- cmd/rel/internal/testdata/rel-1545325035.go | 68 --------------------- 1 file changed, 68 deletions(-) delete mode 100644 cmd/rel/internal/testdata/rel-1545325035.go diff --git a/cmd/rel/internal/testdata/rel-1545325035.go b/cmd/rel/internal/testdata/rel-1545325035.go deleted file mode 100644 index 160761f3..00000000 --- a/cmd/rel/internal/testdata/rel-1545325035.go +++ /dev/null @@ -1,68 +0,0 @@ -package main - -import ( - "context" - "log" - "strings" - "time" - - "github.com/go-rel/migration" - "github.com/go-rel/rel" - db "github.com/go-rel/sqlite3" - _ "github.com/mattn/go-sqlite3" - - "github.com/go-rel/rel/cmd/rel/internal/testdata/migrations" -) - -var ( - shutdowns []func() error -) - -func logger(ctx context.Context, op string, message string) func(err error) { - // no op for rel functions. - if strings.HasPrefix(op, "rel-") { - return func(error) {} - } - - if op == "migrate" || op == "rollback" { - log.Print("Running: ", op, " ", message) - } - - t := time.Now() - return func(err error) { - duration := time.Since(t) - if op == "migrate" || op == "rollback" { - log.Print("=> Done: ", op, " ", message, " in ", duration) - } else if false { - log.Print("\t[duration: ", duration, " op: ", op, "] ", message) - } - - if err != nil { - log.Println("\tError: ", op, " ", err) - } - } -} - -func main() { - var ( - ctx = context.Background() - ) - - adapter, err := db.Open(":memory:") - if err != nil { - log.Fatal(err) - } - - var ( - repo = rel.New(adapter) - m = migrator.New(repo) - ) - - log.SetFlags(0) - repo.Instrumentation(logger) - m.Instrumentation(logger) - - m.Register(1, migrations.MigrateCreateSamples, migrations.RollbackCreateSamples) - - m.Migrate(ctx) -}