This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (69 loc) · 2.06 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
package main
import (
"database/sql"
"os"
accountMigrations "lockbox.dev/accounts/storers/postgres/migrations"
clientMigrations "lockbox.dev/clients/storers/postgres/migrations"
grantsPostgres "lockbox.dev/grants/storers/postgres"
scopeMigrations "lockbox.dev/scopes/storers/postgres/migrations"
tokenMigrations "lockbox.dev/tokens/storers/postgres/migrations"
_ "github.com/lib/pq"
migrate "github.com/rubenv/sql-migrate"
yall "yall.in"
"yall.in/colour"
)
func main() {
log := yall.New(colour.New(os.Stdout, yall.Debug))
connString := os.Getenv("PG_DB")
if connString == "" {
log.Error("PG_DB must be set")
os.Exit(1)
}
pgConn, err := sql.Open("postgres", connString)
if err != nil {
log.WithError(err).Error("error connecting to postgres")
os.Exit(1)
}
packageMigrations := map[string]migrate.MigrationSource{
"accounts": &migrate.AssetMigrationSource{
Asset: accountMigrations.Asset,
AssetDir: accountMigrations.AssetDir,
Dir: "sql",
},
"clients": &migrate.AssetMigrationSource{
Asset: clientMigrations.Asset,
AssetDir: clientMigrations.AssetDir,
Dir: "sql",
},
"grants": grantsPostgres.MigrationsSource(),
"scopes": &migrate.AssetMigrationSource{
Asset: scopeMigrations.Asset,
AssetDir: scopeMigrations.AssetDir,
Dir: "sql",
},
"tokens": &migrate.AssetMigrationSource{
Asset: tokenMigrations.Asset,
AssetDir: tokenMigrations.AssetDir,
Dir: "sql",
},
}
for pkg, source := range packageMigrations {
log.WithField("pkg", pkg).Debug("running migrations")
migrate.SetTable("migrations_" + pkg)
_, err = migrate.Exec(pgConn, "postgres", source, migrate.Up)
if err != nil {
log.WithError(err).WithField("pkg", pkg).Error("error running migrations")
err = pgConn.Close()
if err != nil {
log.WithError(err).WithField("pkg", pkg).Error("error closing postgres control connection")
}
os.Exit(1)
}
}
err = pgConn.Close()
if err != nil {
log.WithError(err).Error("error closing postgres control connection")
os.Exit(1)
}
log.Info("migrations complete")
}