-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrations.go
42 lines (34 loc) · 900 Bytes
/
migrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"appdoki-be/config"
"database/sql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
log "github.com/sirupsen/logrus"
)
type migrationsLogger struct {
verbose bool
}
func (ml *migrationsLogger) Printf(format string, v ...interface{}) {
log.Debugf(format, v...)
}
func (ml *migrationsLogger) Verbose() bool {
return ml.verbose
}
func runMigrations(db *sql.DB, conf *config.DatabaseConfig) {
driver, err := postgres.WithInstance(db, &postgres.Config{})
if err != nil {
log.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(conf.MigrationsDir, "postgres", driver)
if err != nil {
log.Fatal(err)
}
m.Log = &migrationsLogger{
verbose: conf.MigrationsLogVerbose,
}
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
log.Fatal(err)
}
}