go-waku/waku/persistence/postgres/migrations/migrate.go

41 lines
789 B
Go
Raw Normal View History

2022-05-30 19:13:27 +00:00
package migrations
import (
"database/sql"
"github.com/golang-migrate/migrate/v4"
2023-01-04 17:58:14 +00:00
"github.com/golang-migrate/migrate/v4/database"
2022-05-30 19:13:27 +00:00
bindata "github.com/golang-migrate/migrate/v4/source/go_bindata"
)
// Migrate applies migrations.
2023-01-04 17:58:14 +00:00
func Migrate(db *sql.DB, driver database.Driver) error {
2022-05-30 19:13:27 +00:00
return migrateDB(db, bindata.Resource(
AssetNames(),
2023-01-04 17:58:14 +00:00
Asset,
), driver)
2022-05-30 19:13:27 +00:00
}
// Migrate database using provided resources.
2023-01-04 17:58:14 +00:00
func migrateDB(db *sql.DB, resources *bindata.AssetSource, driver database.Driver) error {
2022-05-30 19:13:27 +00:00
source, err := bindata.WithInstance(resources)
if err != nil {
return err
}
m, err := migrate.NewWithInstance(
"go-bindata",
source,
2023-01-04 17:58:14 +00:00
"gowakudb",
2022-05-30 19:13:27 +00:00
driver)
if err != nil {
return err
}
if err = m.Up(); err != migrate.ErrNoChange {
return err
}
return nil
}