37 lines
783 B
Go
37 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
|
||
|
}
|