mercury/internal/database/sql/migrate.go
2023-09-03 00:23:48 +02:00

36 lines
783 B
Go

package sql
import (
"database/sql"
"emperror.dev/errors"
"git.sleepycat.moe/sam/mercury/internal/database/migrations"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/rs/zerolog/log"
migrate "github.com/rubenv/sql-migrate"
)
func Migrate(dsn string) error {
db, err := sql.Open("pgx", dsn)
if err != nil {
return errors.Wrap(err, "opening database")
}
defer db.Close()
if err := db.Ping(); err != nil {
return errors.Wrap(err, "pinging database")
}
migrations := &migrate.EmbedFileSystemMigrationSource{
FileSystem: migrations.FS,
Root: ".",
}
n, err := migrate.Exec(db, "postgres", migrations, migrate.Up)
if err != nil {
return errors.Wrap(err, "executing migrations")
}
log.Info().Int("count", n).Msg("Performed migrations!")
return nil
}