From df104a6aaa2f72fd9066f4b5d979426640157bcb Mon Sep 17 00:00:00 2001 From: ridhoperdana Date: Wed, 29 May 2019 15:27:58 +0700 Subject: [PATCH 1/2] feat: add postgres support --- mysql/migration_test.go | 37 +++++++++++++++++++++++++ postgres/migration.go | 51 +++++++++++++++++++++++++++++++++++ postgres/migration_test.go | 37 +++++++++++++++++++++++++ postgres/suite.go | 55 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 mysql/migration_test.go create mode 100644 postgres/migration.go create mode 100644 postgres/migration_test.go create mode 100644 postgres/suite.go diff --git a/mysql/migration_test.go b/mysql/migration_test.go new file mode 100644 index 0000000..d755898 --- /dev/null +++ b/mysql/migration_test.go @@ -0,0 +1,37 @@ +package mysql + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/stretchr/testify/suite" +) + +type SuiteTest struct { + MysqlSuite +} + +func TestMysqlSuite(t *testing.T) { + s := SuiteTest{ + MysqlSuite{ + DSN: "root:root@tcp(mysql:3306)/mysql_test", + MigrationLocationFolder: "migrations", + DBName: "feed", + }, + } + + suite.Run(t, &s) +} + +func (s *SuiteTest) TestRunMigrationUp() { + errs, ok := s.Migration.Up() + assert.True(s.T(), ok) + assert.Len(s.T(), errs, 0) +} + +func (s *SuiteTest) TestRunMigrationDown() { + errs, ok := s.Migration.Up() + assert.True(s.T(), ok) + assert.Len(s.T(), errs, 0) +} diff --git a/postgres/migration.go b/postgres/migration.go new file mode 100644 index 0000000..5be3ca6 --- /dev/null +++ b/postgres/migration.go @@ -0,0 +1,51 @@ +package postgres + +import ( + "database/sql" + "strings" + + "github.com/golang-migrate/migrate" + _postgres "github.com/golang-migrate/migrate/database/postgres" +) + +type migration struct { + Migrate *migrate.Migrate +} + +func (this *migration) Up() (error, bool) { + err := this.Migrate.Up() + if err != nil { + if err == migrate.ErrNoChange { + return nil, true + } + return err, false + } + return nil, true +} + +func (this *migration) Down() (error, bool) { + err := this.Migrate.Down() + if err != nil { + return err, false + } + return nil, true +} + +func runMigration(dbConn *sql.DB, migrationsFolderLocation string) (*migration, error) { + dataPath := []string{} + dataPath = append(dataPath, "file://") + dataPath = append(dataPath, migrationsFolderLocation) + + pathToMigrate := strings.Join(dataPath, "") + + driver, err := _postgres.WithInstance(dbConn, &_postgres.Config{}) + if err != nil { + return nil, err + } + + m, err := migrate.NewWithDatabaseInstance(pathToMigrate, postgres, driver) + if err != nil { + return nil, err + } + return &migration{Migrate: m}, nil +} diff --git a/postgres/migration_test.go b/postgres/migration_test.go new file mode 100644 index 0000000..bf44185 --- /dev/null +++ b/postgres/migration_test.go @@ -0,0 +1,37 @@ +package postgres + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/stretchr/testify/suite" +) + +type SuiteTest struct { + PostgresSuite +} + +func TestPostgresSuite(t *testing.T) { + s := SuiteTest{ + PostgresSuite{ + DSN: "root:root@tcp(postgres:5432)/postgres_test", + MigrationLocationFolder: "migrations", + DBName: "feed", + }, + } + + suite.Run(t, &s) +} + +func (s *SuiteTest) TestRunMigrationUp() { + errs, ok := s.Migration.Up() + assert.True(s.T(), ok) + assert.Len(s.T(), errs, 0) +} + +func (s *SuiteTest) TestRunMigrationDown() { + errs, ok := s.Migration.Up() + assert.True(s.T(), ok) + assert.Len(s.T(), errs, 0) +} diff --git a/postgres/suite.go b/postgres/suite.go new file mode 100644 index 0000000..494ee16 --- /dev/null +++ b/postgres/suite.go @@ -0,0 +1,55 @@ +package postgres + +import ( + "database/sql" + + driverSql "github.com/go-sql-driver/mysql" + _ "github.com/golang-migrate/migrate/source/file" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +const postgres = "postgres" + +// PostgresSuite struct for MySQL Suite +type PostgresSuite struct { + suite.Suite + DSN string + DBConn *sql.DB + Migration *migration + MigrationLocationFolder string + DBName string +} + +// SetupSuite setup at the beginning of test +func (s *PostgresSuite) SetupSuite() { + DisableLogging() + + var err error + + s.DBConn, err = sql.Open(postgres, s.DSN) + err = s.DBConn.Ping() + require.NoError(s.T(), err) + + s.Migration, err = runMigration(s.DBConn, s.MigrationLocationFolder) + require.NoError(s.T(), err) +} + +// TearDownSuite teardown at the end of test +func (s *PostgresSuite) TearDownSuite() { + err := s.DBConn.Close() + require.NoError(s.T(), err) +} + +func DisableLogging() { + nopLogger := NopLogger{} + if err := driverSql.SetLogger(nopLogger); err != nil { + panic(err) + } + +} + +type NopLogger struct { +} + +func (l NopLogger) Print(v ...interface{}) {} From 272f93c1c67a3ec2870b72f368355d2703ab16c8 Mon Sep 17 00:00:00 2001 From: ridhoperdana Date: Wed, 29 May 2019 15:29:37 +0700 Subject: [PATCH 2/2] refactor: remove wrong migration test --- mysql/migration_test.go | 37 ------------------------------------- postgres/migration_test.go | 37 ------------------------------------- 2 files changed, 74 deletions(-) delete mode 100644 mysql/migration_test.go delete mode 100644 postgres/migration_test.go diff --git a/mysql/migration_test.go b/mysql/migration_test.go deleted file mode 100644 index d755898..0000000 --- a/mysql/migration_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package mysql - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/stretchr/testify/suite" -) - -type SuiteTest struct { - MysqlSuite -} - -func TestMysqlSuite(t *testing.T) { - s := SuiteTest{ - MysqlSuite{ - DSN: "root:root@tcp(mysql:3306)/mysql_test", - MigrationLocationFolder: "migrations", - DBName: "feed", - }, - } - - suite.Run(t, &s) -} - -func (s *SuiteTest) TestRunMigrationUp() { - errs, ok := s.Migration.Up() - assert.True(s.T(), ok) - assert.Len(s.T(), errs, 0) -} - -func (s *SuiteTest) TestRunMigrationDown() { - errs, ok := s.Migration.Up() - assert.True(s.T(), ok) - assert.Len(s.T(), errs, 0) -} diff --git a/postgres/migration_test.go b/postgres/migration_test.go deleted file mode 100644 index bf44185..0000000 --- a/postgres/migration_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package postgres - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/stretchr/testify/suite" -) - -type SuiteTest struct { - PostgresSuite -} - -func TestPostgresSuite(t *testing.T) { - s := SuiteTest{ - PostgresSuite{ - DSN: "root:root@tcp(postgres:5432)/postgres_test", - MigrationLocationFolder: "migrations", - DBName: "feed", - }, - } - - suite.Run(t, &s) -} - -func (s *SuiteTest) TestRunMigrationUp() { - errs, ok := s.Migration.Up() - assert.True(s.T(), ok) - assert.Len(s.T(), errs, 0) -} - -func (s *SuiteTest) TestRunMigrationDown() { - errs, ok := s.Migration.Up() - assert.True(s.T(), ok) - assert.Len(s.T(), errs, 0) -}