-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (64 loc) · 1.76 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
// Retry is an exponential backoff retry helper. It is used to wait for postgres to boot up
func Retry(op func() error) error {
bo := backoff.NewExponentialBackOff()
bo.MaxInterval = time.Second * 10
bo.MaxElapsedTime = time.Minute * 5
if err := backoff.Retry(op, bo); err != nil {
if bo.NextBackOff() == backoff.Stop {
return fmt.Errorf("reached retry deadline")
}
return fmt.Errorf("retry failed: %w", err)
}
return nil
}
func main() {
host := os.Getenv("DATABASE_HOST")
port := os.Getenv("DATABASE_PORT")
username := os.Getenv("DATABASE_USERNAME")
password := os.Getenv("DATABASE_PASSWORD")
name := os.Getenv("DATABASE_NAME")
if host == "" {
log.Fatal("DATABASE_HOST not specified")
}
if port == "" {
log.Fatal("DATABASE_PORT not specified")
}
if username == "" {
log.Fatal("DATABASE_USERNAME not specified")
}
if password == "" {
log.Fatal("DATABASE_PASSWORD not specified")
}
if name == "" {
log.Fatal("DATABASE_NAME not specified")
}
uri := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", username, password, host, port, name)
var m *migrate.Migrate
if err := Retry(func() error {
var err error
fmt.Println("trying migration...")
m, err = migrate.New("file://migrations", uri)
if err != nil {
fmt.Printf("migration failed: (%+v) \n", err)
return err
}
fmt.Println("migration completed successfully")
return nil
}); err != nil {
log.Fatal("error creating new migration: ", err)
}
if err := m.Up(); err != nil {
log.Fatal("error running migrations: ", err)
}
}