This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
98 lines (85 loc) · 2.34 KB
/
root.go
File metadata and controls
98 lines (85 loc) · 2.34 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"fmt"
"log"
"os"
"strconv"
"github.com/jackc/pgx/v4"
"github.com/pollex/go-migrate/pkg/migration"
"github.com/urfave/cli/v2"
)
func main() {
a := &cli.App{
Name: "Go Migrate",
Usage: "Apply database migrations",
Description: `Using this command the user can apply or undo database migrations.
The second parameter can be used either give a target migration or migrate relative to the current migration.
To migrate to the latest version, omit the second parameter:
'go-migrate ./migrations'
To migrate to the second migration, supply an integer as second parameter:
'go-migrate ./migrations 2'
To undo the last 3 migrations, supply a relative integer (prefixed with + or -):
'go-migrate ./migrations -3'
`,
ArgsUsage: "<folder> [(relative) index]",
Action: migrate,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "database",
Aliases: []string{
"db",
"d",
},
Usage: "Set the connection string of the database to connect to",
},
},
}
a.Run(os.Args)
}
func migrate(c *cli.Context) error {
connString := c.String("database")
if connString == "" {
fmt.Println("Must provide a database connection string (--database, --db, -d)")
os.Exit(1)
}
path := c.Args().Get(0)
if path == "" {
fmt.Println("Command must contain a path to the directory containing the migrations")
os.Exit(1)
}
ixArg := c.Args().Get(1)
// Create database connection
pg, err := pgx.Connect(context.Background(), connString)
if err != nil {
log.Fatalf("Could not connect to database: %s", err)
}
defer pg.Close(context.Background())
// Create migrator instance
m, err := migration.NewMigrator(pg, path)
if err != nil {
log.Fatalf("Could not create migrator: %s", err)
}
// if arg is empty then apply all
if ixArg == "" {
err = m.MigrateAll()
} else {
ix, err := strconv.ParseInt(ixArg, 10, 32)
if err != nil {
log.Fatalf("Incorrect index parameter given.\n")
}
// If an argument is given, check whether it is relative
// if a plus or minus is given then it MUST be relative
if ixArg[0] == '+' || ixArg[0] == '-' {
err = m.MigrateRelative(int(ix))
} else {
// Otherwise it is an absolute index
err = m.MigrateTo(int(ix))
}
}
if err != nil {
log.Fatalf("\nCould not apply migrations: %s", err)
}
fmt.Print("Finished applying database migrations!\n")
return nil
}