be38c83fd8
* db cleanup * dev * pg setup * postgres uuid migration * cleanup * fk * sqlite migration * cleanup * wal * max open conns = 1 * fix panic --------- Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
46 lines
843 B
Go
46 lines
843 B
Go
package postgres
|
|
|
|
import (
|
|
"database/sql"
|
|
"embed"
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
"github.com/golang-migrate/migrate/v4/database/postgres"
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var migrations embed.FS
|
|
|
|
type PostgresAccessor struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func (a *PostgresAccessor) Migrate() error {
|
|
driver, err := postgres.WithInstance(a.db, &postgres.Config{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d, err := iofs.New(migrations, "migrations")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m, err := migrate.NewWithInstance("iofs", d, "postgres", driver)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = m.Up()
|
|
if err != nil && err != migrate.ErrNoChange {
|
|
return err
|
|
}
|
|
|
|
v, _, _ := m.Version()
|
|
logrus.WithFields(logrus.Fields{"version": v}).Info("DB Migrated")
|
|
|
|
return nil
|
|
}
|