Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for postgres #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions postgres/migration.go
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 55 additions & 0 deletions postgres/suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package postgres

import (
"database/sql"

driverSql "github.com/go-sql-driver/mysql"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it the right choice? I'm afraid this is not the right one.

How about this: https://github.com/lib/pq??

_ "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{}) {}