2023-07-07 01:38:23 +00:00
|
|
|
package migrate
|
2022-05-30 19:13:27 +00:00
|
|
|
|
|
|
|
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-07-07 01:38:23 +00:00
|
|
|
func Migrate(db *sql.DB, driver database.Driver, assetNames []string, assetFunc bindata.AssetFunc) error {
|
2022-05-30 19:13:27 +00:00
|
|
|
return migrateDB(db, bindata.Resource(
|
2023-07-07 01:38:23 +00:00
|
|
|
assetNames,
|
|
|
|
assetFunc,
|
2024-01-03 16:49:54 +00:00
|
|
|
), driver, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate applies migrations.
|
|
|
|
func MigrateDown(db *sql.DB, driver database.Driver, assetNames []string, assetFunc bindata.AssetFunc) error {
|
|
|
|
return migrateDB(db, bindata.Resource(
|
|
|
|
assetNames,
|
|
|
|
assetFunc,
|
|
|
|
), driver, false)
|
2022-05-30 19:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate database using provided resources.
|
2024-01-03 16:49:54 +00:00
|
|
|
func migrateDB(db *sql.DB, resources *bindata.AssetSource, driver database.Driver, up bool) 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
|
|
|
|
}
|
|
|
|
|
2024-01-03 16:49:54 +00:00
|
|
|
fn := m.Up
|
|
|
|
if !up {
|
|
|
|
fn = m.Down
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fn(); err != migrate.ErrNoChange {
|
2022-05-30 19:13:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|