2019-02-05 15:25:01 +03:00
|
|
|
package sqlite
|
2019-01-18 17:51:10 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-12-27 12:35:24 +03:00
|
|
|
"embed"
|
2021-04-12 14:03:10 +03:00
|
|
|
|
2023-12-27 12:35:24 +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"
|
2022-02-01 16:59:29 +03:00
|
|
|
_ "modernc.org/sqlite"
|
2019-01-18 17:51:10 +03:00
|
|
|
)
|
|
|
|
|
2023-12-27 12:35:24 +03:00
|
|
|
//go:embed migrations/*.sql
|
|
|
|
var migrations embed.FS
|
|
|
|
|
2019-01-18 17:51:10 +03:00
|
|
|
type Sqlite3Accessor struct {
|
2023-12-27 12:35:24 +03:00
|
|
|
db *sql.DB
|
2019-01-18 17:51:10 +03:00
|
|
|
}
|
|
|
|
|
2023-12-27 12:35:24 +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
|
|
|
|
2023-12-27 12:35:24 +03:00
|
|
|
d, err := iofs.New(migrations, "migrations")
|
2021-04-12 14:03:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-27 12:35:24 +03:00
|
|
|
m, err := migrate.NewWithInstance("iofs", d, "sqlite", driver)
|
|
|
|
if err != nil {
|
2019-01-29 20:00:00 +03:00
|
|
|
return err
|
2023-12-27 12:35:24 +03:00
|
|
|
}
|
2019-01-29 20:00:00 +03:00
|
|
|
|
2023-12-27 12:35:24 +03:00
|
|
|
err = m.Up()
|
|
|
|
if err != nil && err != migrate.ErrNoChange {
|
2019-01-29 20:00:00 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-12-27 12:35:24 +03:00
|
|
|
|
|
|
|
v, _, _ := m.Version()
|
|
|
|
logrus.WithFields(logrus.Fields{"version": v}).Info("DB Migrated")
|
|
|
|
|
|
|
|
return nil
|
2019-01-29 20:00:00 +03:00
|
|
|
}
|