diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1f54782 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +check: + go fmt ./... + +test: + go test -race -count 1 ./... diff --git a/foundation.go b/foundation.go new file mode 100644 index 0000000..ced5b81 --- /dev/null +++ b/foundation.go @@ -0,0 +1,177 @@ +package main + +import ( + "database/sql" + "io/ioutil" + "testing" + + "github.com/jmoiron/sqlx" +) + +type Foundation struct { + t *testing.T + currentStep int + stepByStep bool + migrator Migrator + db *sqlx.DB + interceptors map[int]Interceptor +} + +type Migrator interface { + DB() *sql.DB + DriverName() string + Setup() error + MigrateToStep(step int) error + TearDown() error +} + +type Interceptor func() error + +// ToDo: change T to TB? +func New(t *testing.T, migrator Migrator) *Foundation { + if err := migrator.Setup(); err != nil { + t.Fatalf("error setting up the migrator: %s", err) + } + + db := sqlx.NewDb(migrator.DB(), migrator.DriverName()) + + return &Foundation{ + t: t, + currentStep: 0, + // if true, will run the migrator Step function once per step + // instead of just once with the final step + stepByStep: false, + migrator: migrator, + db: db, + } +} + +func (f *Foundation) RegisterInterceptors(interceptors map[int]Interceptor) *Foundation { + f.interceptors = interceptors + return f +} + +func (f *Foundation) SetStepByStep(stepByStep bool) *Foundation { + f.stepByStep = stepByStep + return f +} + +// calculateNextStep returns the next step in the chain that has an +// interceptor or the final step to migrate to +func (f *Foundation) calculateNextStep(step int) int { + // should never happen + if f.currentStep >= step { + // nothing to do + return step // ToDo: or 0? merge the two conditions + } + + // if there are no interceptors, next step is directly the final + // one + if f.interceptors == nil { + return step + } + + i := f.currentStep + for { + i++ + + if _, ok := f.interceptors[i]; ok { + break + } + + if step == i { + break + } + } + + return i +} + +func (f *Foundation) MigrateToStep(step int) *Foundation { + if step == f.currentStep { + // log nothing to do + return f + } + + if step < f.currentStep { + f.t.Fatal("Down migrations not supported yet") + } + + // if there are no interceptors, just migrate to the last step + if f.interceptors == nil { + if err := f.migrateToStep(step); err != nil { + f.t.Fatalf("migration to step %d failed: %s", step, err) + } + + return f + } + + for f.currentStep < step { + nextStep := f.calculateNextStep(step) + + if err := f.migrateToStep(nextStep); err != nil { + f.t.Fatalf("migration to step %d failed: %s", nextStep, err) + } + + interceptorFn, ok := f.interceptors[nextStep] + if ok { + if err := interceptorFn(); err != nil { + f.t.Fatalf("interceptor function for step %d failed", nextStep) + } + } + } + + return f +} + +// migrateToStep executes the migrator function to migrate to a +// specific step and updates the foundation currentStep to reflect the +// result. This function doesn't take into account interceptors, that +// happens on MigrateToStep +func (f *Foundation) migrateToStep(step int) error { + if f.stepByStep { + for f.currentStep < step { + if err := f.migrator.MigrateToStep(f.currentStep + 1); err != nil { + return err + } + + f.currentStep++ + } + + return nil + } + + if err := f.migrator.MigrateToStep(step); err != nil { + return err + } + + f.currentStep = step + return nil +} + +func (f *Foundation) TearDown() { + if err := f.migrator.TearDown(); err != nil { + f.t.Fatalf("error tearing down migrator: %s", err) + } +} + +func (f *Foundation) DB() *sqlx.DB { + return f.db +} + +func (f *Foundation) Exec(s string) *Foundation { + if _, err := f.DB().Exec(s); err != nil { + f.t.Fatalf("failed to run %s: %s", s, err) + } + + return f +} + +func (f *Foundation) ExecFile(filePath string) *Foundation { + b, err := ioutil.ReadFile(filePath) + if err != nil { + f.t.Fatalf("failed to read file %s: %s", filePath, err) + } + + return f.Exec(string(b)) +} diff --git a/foundation_test.go b/foundation_test.go new file mode 100644 index 0000000..6e825b7 --- /dev/null +++ b/foundation_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCalculateNextStep(t *testing.T) { + t.Run("should return the last step if no interceptors are set", func(t *testing.T) { + f := New(t, &SampleMigrator{}) + + require.Equal(t, 7, f.calculateNextStep(7)) + f.currentStep = 5 + require.Equal(t, 7, f.calculateNextStep(7)) + }) + + t.Run("should return the next step with an interceptor", func(t *testing.T) { + interceptors := map[int]Interceptor{ + 3: func() error { return nil }, + 12: func() error { return nil }, + } + + f := New(t, &SampleMigrator{}). + RegisterInterceptors(interceptors) + + require.Equal(t, 3, f.calculateNextStep(7)) + f.currentStep = 5 + require.Equal(t, 7, f.calculateNextStep(7)) + require.Equal(t, 12, f.calculateNextStep(15)) + f.currentStep = 12 + require.Equal(t, 15, f.calculateNextStep(15)) + }) +} diff --git a/go.mod b/go.mod index d47b2dc..878bd52 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,14 @@ module git.ctrlz.es/mgdelacroix/foundation go 1.18 + +require ( + github.com/jmoiron/sqlx v1.3.5 + github.com/stretchr/testify v1.8.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f464131 --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +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/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= +github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= +github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helpers_test.go b/helpers_test.go new file mode 100644 index 0000000..d1a9d47 --- /dev/null +++ b/helpers_test.go @@ -0,0 +1,27 @@ +package main + +import "database/sql" + +type SampleMigrator struct{} + +func (sm *SampleMigrator) DB() *sql.DB { + return nil +} + +func (sm *SampleMigrator) DriverName() string { + return "" +} + +func (sm *SampleMigrator) Setup() error { + return nil +} + +func (sm *SampleMigrator) MigrateToStep(step int) error { + return nil +} + +func (sm *SampleMigrator) TearDown() error { + return nil +} + +var _ Migrator = &SampleMigrator{}