This commit is contained in:
parent
cd1959d824
commit
32fea20476
|
@ -28,6 +28,11 @@ var serveCmd = &cobra.Command{
|
|||
}
|
||||
defer tele.Close()
|
||||
|
||||
if err := db.RunMigrations(cfg); err != nil {
|
||||
log.New(cmd.Context()).Err(err).Error("failed to run migrations")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
database, err := db.NewComfy(cfg)
|
||||
if err != nil {
|
||||
log.New(cmd.Context()).Err(err).Error("failed to open database")
|
||||
|
|
23
db/db.go
23
db/db.go
|
@ -69,3 +69,26 @@ func ApplyLogger(cfg *config.Config, db *sql.DB) *sql.DB {
|
|||
sqldblogger.WithSQLQueryAsMessage(true),
|
||||
)
|
||||
}
|
||||
|
||||
func RunMigrations(cfg *config.Config) error {
|
||||
if cfg.Bool("db.automigrate") {
|
||||
driver := cfg.String("db.driver")
|
||||
dsn := cfg.String("db.string")
|
||||
db, err := sql.Open(driver, dsn)
|
||||
if err != nil {
|
||||
return errs.Wrapw(err, "migration: failed to open database", "driver", driver, "db.string", dsn)
|
||||
}
|
||||
defer db.Close()
|
||||
goose.SetLogger(goose.NopLogger())
|
||||
goose.SetBaseFS(Migrations)
|
||||
|
||||
if err := goose.SetDialect(driver); err != nil {
|
||||
return errs.Wrapw(err, "failed to set goose dialect", "dialect", driver, "dsn", dsn)
|
||||
}
|
||||
|
||||
if err := goose.Up(db, "db/migrations"); err != nil {
|
||||
return errs.Wrapw(err, "failed to migrate database", "dialect", driver, "dsn", dsn)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue