mapserver/mapobjectdb/sqlite/migrate.go

47 lines
858 B
Go
Raw Permalink Normal View History

2019-02-05 15:25:01 +03:00
package sqlite
2019-01-18 17:51:10 +03:00
import (
"database/sql"
"embed"
2021-04-12 14:03:10 +03:00
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source/iofs"
2019-01-18 17:51:10 +03:00
"github.com/sirupsen/logrus"
_ "modernc.org/sqlite"
2019-01-18 17:51:10 +03:00
)
//go:embed migrations/*.sql
var migrations embed.FS
2019-01-18 17:51:10 +03:00
type Sqlite3Accessor struct {
db *sql.DB
2019-01-18 17:51:10 +03:00
}
func (a *Sqlite3Accessor) Migrate() error {
driver, err := sqlite3.WithInstance(a.db, &sqlite3.Config{})
2019-01-18 17:51:10 +03:00
if err != nil {
return err
}
2021-04-12 14:03:10 +03:00
d, err := iofs.New(migrations, "migrations")
2021-04-12 14:03:10 +03:00
if err != nil {
return err
}
m, err := migrate.NewWithInstance("iofs", d, "sqlite", driver)
if err != nil {
2019-01-29 20:00:00 +03:00
return err
}
2019-01-29 20:00:00 +03:00
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
2019-01-29 20:00:00 +03:00
return err
}
v, _, _ := m.Version()
logrus.WithFields(logrus.Fields{"version": v}).Info("DB Migrated")
return nil
2019-01-29 20:00:00 +03:00
}