forked from rnubel/pgmgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (215 loc) · 6.03 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"fmt"
"os"
"github.com/rnubel/pgmgr/pgmgr"
"github.com/urfave/cli"
)
func displayErrorOrMessage(err error, args ...interface{}) error {
if err != nil {
return cli.NewExitError(fmt.Sprintln("Error: ", err), 1)
}
fmt.Println(args...)
return nil
}
func displayVersion(config *pgmgr.Config) error {
v, err := pgmgr.Version(config)
if v < 0 {
return displayErrorOrMessage(err, "Database has no schema_migrations table; run `pgmgr db migrate` to create it.")
}
return displayErrorOrMessage(err, "Latest migration version:", v)
}
func main() {
config := &pgmgr.Config{}
app := cli.NewApp()
app.Name = "pgmgr"
app.Usage = "manage your app's Postgres database"
app.Version = "0.0.2"
var s []string
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config-file, c",
Value: ".pgmgr.json",
Usage: "set the path to the JSON configuration file specifying your DB parameters",
EnvVar: "PGMGR_CONFIG_FILE",
},
cli.StringFlag{
Name: "database, d",
Value: "",
Usage: "the database name which pgmgr will connect to or try to create",
EnvVar: "PGMGR_DATABASE",
},
cli.StringFlag{
Name: "username, u",
Value: "",
Usage: "the username which pgmgr will connect with",
EnvVar: "PGMGR_USERNAME",
},
cli.StringFlag{
Name: "password, P",
Value: "",
Usage: "the password which pgmgr will connect with",
EnvVar: "PGMGR_PASSWORD",
},
cli.StringFlag{
Name: "host, H",
Value: "",
Usage: "the host which pgmgr will connect to",
EnvVar: "PGMGR_HOST",
},
cli.IntFlag{
Name: "port, p",
Value: 0,
Usage: "the port which pgmgr will connect to",
EnvVar: "PGMGR_PORT",
},
cli.StringFlag{
Name: "sslmode",
Value: "",
Usage: "whether to verify SSL connection or not. See https://www.postgresql.org/docs/9.1/static/libpq-ssl.html",
EnvVar: "PGMGR_SSLMODE",
},
cli.StringFlag{
Name: "url",
Value: "",
Usage: "connection URL or DSN containing connection info; will override the other params if given",
EnvVar: "PGMGR_URL",
},
cli.StringFlag{
Name: "dump-file",
Value: "",
Usage: "where to dump or load the database structure and contents to or from",
EnvVar: "PGMGR_DUMP_FILE",
},
cli.StringFlag{
Name: "column-type",
Value: "integer",
Usage: "column type to use in schema_migrations table; 'integer' or 'string'",
EnvVar: "PGMGR_COLUMN_TYPE",
},
cli.StringFlag{
Name: "format",
Value: "unix",
Usage: "timestamp format for migrations; 'unix' or 'datetime'",
EnvVar: "PGMGR_FORMAT",
},
cli.StringFlag{
Name: "migration-table",
Value: "schema_migrations",
Usage: "table to use for storing migration status; eg 'myschema.applied_migrations'",
EnvVar: "PGMGR_MIGRATION_TABLE",
},
cli.StringFlag{
Name: "migration-folder",
Value: "",
Usage: "folder containing the migrations to apply",
EnvVar: "PGMGR_MIGRATION_FOLDER",
},
cli.StringSliceFlag{
Name: "seed-tables",
Value: (*cli.StringSlice)(&s),
Usage: "list of tables (or globs matching table names) to dump the data of",
EnvVar: "PGMGR_SEED_TABLES",
},
}
app.Before = func(c *cli.Context) error {
return pgmgr.LoadConfig(config, c)
}
app.Commands = []cli.Command{
{
Name: "migration",
Usage: "generates a new migration with the given name",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "no-txn",
Usage: "generate a migration that will not be wrapped in a transaction when run",
},
},
Action: func(c *cli.Context) error {
if len(c.Args()) == 0 {
return cli.NewExitError("migration name not given! try `pgmgr migration NameGoesHere`", 1)
}
return displayErrorOrMessage(pgmgr.CreateMigration(config, c.Args()[0], c.Bool("no-txn")))
},
},
{
Name: "config",
Usage: "displays the current configuration as seen by pgmgr",
Action: func(c *cli.Context) error {
fmt.Printf("%+v\n", config)
return nil
},
},
{
Name: "db",
Usage: "manage your database. use 'pgmgr db help' for more info",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "creates the database if it doesn't exist",
Action: func(c *cli.Context) error {
return displayErrorOrMessage(pgmgr.Create(config), "Database", config.Database, "created successfully.")
},
},
{
Name: "drop",
Usage: "drops the database (all sessions must be disconnected first. this command does not force it)",
Action: func(c *cli.Context) error {
return displayErrorOrMessage(pgmgr.Drop(config), "Database", config.Database, "dropped successfully.")
},
},
{
Name: "dump",
Usage: "dumps the database schema and contents to the dump file (see --dump-file)",
Action: func(c *cli.Context) error {
err := pgmgr.Dump(config)
return displayErrorOrMessage(err, "Database dumped to", config.DumpFile, "successfully")
},
},
{
Name: "load",
Usage: "loads the database schema and contents from the dump file (see --dump-file)",
Action: func(c *cli.Context) error {
err := pgmgr.Load(config)
err = displayErrorOrMessage(err, "Database loaded successfully.")
if err != nil {
return err
}
return displayVersion(config)
},
},
{
Name: "version",
Usage: "returns the current schema version",
Action: func(c *cli.Context) error {
return displayVersion(config)
},
},
{
Name: "migrate",
Usage: "applies any un-applied migrations in the migration folder (see --migration-folder)",
Action: func(c *cli.Context) error {
err := pgmgr.Migrate(config)
if err != nil {
return cli.NewExitError(fmt.Sprintln("Error during migration:", err), 1)
}
return nil
},
},
{
Name: "rollback",
Usage: "rolls back the latest migration",
Action: func(c *cli.Context) error {
pgmgr.Rollback(config)
return nil
},
},
},
},
}
app.Action = func(c *cli.Context) error {
app.Command("help").Run(c)
return nil
}
app.Run(os.Args)
}