-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
49 lines (40 loc) · 974 Bytes
/
migrate.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
43
44
45
46
47
48
49
package main
import (
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
"github.com/spf13/viper"
"log"
"tabeldatadotcom/archetype/backend-api/config"
)
func main() {
var (
dbEnv = viper.GetString("DATABASE_NAME")
)
config.SetupEnvironment()
db, dbErr := config.SetupDatabase()
defer db.Close()
if dbErr != nil {
log.Fatal(dbErr)
}
driver, driverErr := postgres.WithInstance(db.DB, &postgres.Config{})
if driverErr != nil {
}
m, errMigrate := migrate.NewWithDatabaseInstance(
"file://db/migration",
dbEnv,
driver,
)
if errMigrate != nil {
log.Fatal(errMigrate)
}
_ = m.Up()
log.Println("---do-migrate---")
version, dirty, verErr := m.Version()
if verErr != nil {
log.Fatal(verErr)
}
log.Printf("version: %d, dirty: %t", version, dirty)
}