/
migrate.go
74 lines (66 loc) · 1.56 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
package cmd
import (
"fmt"
"net/url"
"github.com/prest/prest/adapters/postgres"
"github.com/prest/prest/config"
"github.com/spf13/cobra"
// pq driver
_ "github.com/lib/pq"
)
var (
urlConn string
path string
)
// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Execute migration operations",
Long: `Execute migration operations`,
}
func checkTable(cmd *cobra.Command, args []string) error {
if config.PrestConf.Adapter == nil {
postgres.Load()
}
sc := config.PrestConf.Adapter.ShowTable("public", "schema_migrations")
if err := sc.Err(); err != nil {
return err
}
ts := []struct {
ColName string `json:"column_name,omitempty"`
}{}
_, err := sc.Scan(&ts)
if err != nil {
return err
}
var index *int
for i := range ts {
if ts[i].ColName == "dirty" {
index = &i
break
}
}
if index != nil {
db, err := postgres.Get()
if err != nil {
return err
}
_, err = db.Exec("ALTER TABLE public.schema_migrations DROP COLUMN dirty")
if err != nil {
return err
}
}
return nil
}
func driverURL() string {
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&sslcert=%s&sslkey=%s&sslrootcert=%s",
url.PathEscape(config.PrestConf.PGUser),
url.PathEscape(config.PrestConf.PGPass),
url.PathEscape(config.PrestConf.PGHost),
config.PrestConf.PGPort,
url.PathEscape(config.PrestConf.PGDatabase),
url.QueryEscape(config.PrestConf.SSLMode),
url.QueryEscape(config.PrestConf.SSLCert),
url.QueryEscape(config.PrestConf.SSLKey),
url.QueryEscape(config.PrestConf.SSLRootCert))
}