Skip to content

Commit

Permalink
feat: separate db migrate to mysql and postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoet committed Jun 23, 2021
1 parent c792f66 commit 72a667e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 47 deletions.
55 changes: 8 additions & 47 deletions pkg/db/migration/migration.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package migration

import (
"database/sql"
"fmt"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/gopaytech/go-commons/pkg/db"
"github.com/rs/zerolog/log"
)

Expand All @@ -17,58 +11,25 @@ type Migration interface {
Close() error
}

type migrator struct {
migrate *migrate.Migrate
type Migrator struct {
Migrate *migrate.Migrate
}

func (m *migrator) Up() (err error) {
err = m.migrate.Up()
func (m *Migrator) Up() (err error) {
err = m.Migrate.Up()
if err == migrate.ErrNoChange {
log.Info().Msg("No new migration found, Ignoring!")
return nil
}
return
}

func (m *migrator) DownLast() (err error) {
err = m.migrate.Steps(-1)
func (m *Migrator) DownLast() (err error) {
err = m.Migrate.Steps(-1)
return
}

func (m *migrator) Close() (err error) {
_, err = m.migrate.Close()
return
}

func New(config db.Config, path string) (mgrn Migration, err error) {
dsn := fmt.Sprintf("mysql://%s:%s@tcp(%s:%v)/%s", config.Username, config.Password, config.Host, config.Port, config.DatabaseName)
m, err := migrate.New(path, dsn)
if err != nil {
return
}

mgrn = &migrator{
migrate: m,
}

return
}

func WithInstance(db *sql.DB, path string) (mgrn Migration, err error) {
driver, err := mysql.WithInstance(db, &mysql.Config{})
m, _ := migrate.NewWithDatabaseInstance(
path,
"mysql",
driver,
)

if err != nil {
return
}

mgrn = &migrator{
migrate: m,
}

func (m *Migrator) Close() (err error) {
_, err = m.Migrate.Close()
return
}
45 changes: 45 additions & 0 deletions pkg/db/migration/mysql/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mysql

import (
"database/sql"
"fmt"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/gopaytech/go-commons/pkg/db"
"github.com/gopaytech/go-commons/pkg/db/migration"
)

func New(config db.Config, path string) (mgrn migration.Migration, err error) {
dsn := fmt.Sprintf("mysql://%s:%s@tcp(%s:%v)/%s", config.Username, config.Password, config.Host, config.Port, config.DatabaseName)
m, err := migrate.New(path, dsn)
if err != nil {
return
}

mgrn = &migration.Migrator{
Migrate: m,
}

return
}

func WithInstance(db *sql.DB, path string) (mgrn migration.Migration, err error) {
driver, err := mysql.WithInstance(db, &mysql.Config{})
m, _ := migrate.NewWithDatabaseInstance(
path,
"mysql",
driver,
)

if err != nil {
return
}

mgrn = &migration.Migrator{
Migrate: m,
}

return
}
44 changes: 44 additions & 0 deletions pkg/db/migration/postgresql/postgresql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package postgresql

import (
"database/sql"
"fmt"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/gopaytech/go-commons/pkg/db"
"github.com/gopaytech/go-commons/pkg/db/migration"
)

func New(config db.Config, path string) (mgrn migration.Migration, err error) {
dsn := fmt.Sprintf("postgres://%s:%s@%s:%v/%s", config.Username, config.Password, config.Host, config.Port, config.DatabaseName)
m, err := migrate.New(path, dsn)
if err != nil {
return
}

mgrn = &migration.Migrator{
Migrate: m,
}

return
}

func WithInstance(db *sql.DB, path string) (mgrn migration.Migration, err error) {
driver, err := postgres.WithInstance(db, &postgres.Config{})
m, _ := migrate.NewWithDatabaseInstance(
path,
"mysql",
driver,
)

if err != nil {
return
}

mgrn = &migration.Migrator{
Migrate: m,
}

return
}

0 comments on commit 72a667e

Please sign in to comment.