-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.go
85 lines (68 loc) · 1.48 KB
/
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
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
85
package main
import (
"context"
"database/sql"
"embed"
"fmt"
"os"
"strings"
"app/adapter/logger"
"app/adapter/postgres"
"app/adapter/validation"
"app/config"
"github.com/pressly/goose/v3"
"go.uber.org/fx"
)
//go:embed migrations/*.sql
var migrations embed.FS
func main() {
if len(os.Args) < 2 {
fmt.Println(`usage:
go run cmd/migrate/migrate.go up
go run cmd/migrate/migrate.go down
go run cmd/migrate/migrate.go down-to 20170506082527
go run cmd/migrate/migrate.go status
go run cmd/migrate/migrate.go redo
go run cmd/migrate/migrate.go create`)
os.Exit(1)
}
ctx := context.Background()
db, dbErr := dbInstance(ctx)
if dbErr != nil {
fmt.Println(dbErr)
os.Exit(1)
}
goose.SetBaseFS(migrations)
action, args := os.Args[1], os.Args[2:]
gooseErr := goose.RunContext(ctx, action, db, dir(action), args...)
if gooseErr != nil {
fmt.Println(strings.ReplaceAll(gooseErr.Error(), `\n`, "\n"))
os.Exit(1)
}
}
func dbInstance(ctx context.Context) (*sql.DB, error) {
var db *sql.DB
app := fx.New(
logger.NopLoggerProvider,
validation.Module,
config.Module,
postgres.Module,
fx.Populate(&db),
)
if err := app.Err(); err != nil {
return nil, fmt.Errorf("app.Err: %w", err)
}
if err := app.Stop(ctx); err != nil {
return nil, fmt.Errorf("app.Stop: %w", err)
}
if db == nil {
return nil, fmt.Errorf("db is nil")
}
return db, nil
}
func dir(action string) string {
if action == "create" {
return "cmd/migrate/migrations"
}
return "migrations"
}