Skip to content

Commit

Permalink
migrate at db init
Browse files Browse the repository at this point in the history
  • Loading branch information
wen-templari committed May 21, 2024
1 parent 908a2f6 commit 5f9e5cc
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions repo/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (

"github.com/jackc/pgx/v5/stdlib"

_ "github.com/lib/pq"

"github.com/nbtca/saturday/util"
"github.com/qustavo/sqlhooks/v2"
"github.com/sirupsen/logrus"

"github.com/jmoiron/sqlx"

"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)

Expand Down Expand Up @@ -43,12 +45,22 @@ func (h *Hooks) After(ctx context.Context, query string, args ...interface{}) (c

func InitDB() {
var err error
sql.Register("mysqlWithHooks", sqlhooks.Wrap(&stdlib.Driver{},&Hooks{}))
sql.Register("mysqlWithHooks", sqlhooks.Wrap(&stdlib.Driver{}, &Hooks{}))
db, err = sqlx.Connect("mysqlWithHooks", os.Getenv("DB_URL"))
if err != nil {
util.Logger.Fatal(err)
}

driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
if err != nil {
util.Logger.Fatal(err)
}

m, err := migrate.NewWithDatabaseInstance(
"file:///migrations",
"file://migrations",
"postgres", driver)
m.Up() // or m.Step(2) if you want to explicitly set the number of migrations to run
m.Close()

if err != nil {
util.Logger.Fatal(err)
Expand Down

0 comments on commit 5f9e5cc

Please sign in to comment.