~egtann/migrate

ref: f8bba57067c303761315e2916cc06bc61880b2cd migrate/cmd/migrate/main.go -rw-r--r-- 4.4 KiB
f8bba570 — Evan Tann update module path 1 year, 1 month ago
                                                                                
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
package main

import (
	"flag"
	"fmt"
	"os"
	"syscall"

	"egt.run/pkg/migrate"
	"egt.run/pkg/migrate/mysql"
	"egt.run/pkg/migrate/postgres"
	"egt.run/pkg/migrate/sqlite"
	"github.com/pkg/errors"
	"golang.org/x/crypto/ssh/terminal"
)

func main() {
	if err := run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func run() error {
	migrationDir := flag.String("dir", ".", "migrations directory")
	dbName := flag.String("db", "", "database name")
	dbUser := flag.String("u", "", "database user")
	dbHost := flag.String("h", "127.0.0.1", "database host")
	dbPort := flag.Int("p", 0, "database port")
	dbType := flag.String("t", "mysql", "type of database (mysql, postgres, sqlite)")
	dry := flag.Bool("d", false, "dry run")
	sslKey := flag.String("ssl-key", "", "path to client key pem")
	sslCert := flag.String("ssl-cert", "", "path to client cert pem")
	sslCA := flag.String("ssl-ca", "", "path to server ca pem")
	sslServerName := flag.String("ssl-server", "", "server name for ssl")
	skip := flag.String("skip", "", "skip up to this filename (inclusive)")
	pass := flag.String("pass", "", "password (optional flag, if not provided it will be requested)")
	version := flag.Bool("v", false, "print the version and exit")
	flag.Parse()

	if *version {
		fmt.Println("v1.0.0rc1")
		return nil
	}

	// Restrict this program to specific files (read-only) and greatly
	// restrict its possible syscalls
	paths := []string{*migrationDir}
	if *sslKey != "" {
		paths = append(paths, *sslKey, *sslCert, *sslCA)
		fmt.Println(paths)
	}
	if err := migrate.Unveil(paths); err != nil {
		return errors.Wrap(err, "unveil")
	}
	if err := migrate.Pledge(); err != nil {
		return errors.Wrap(err, "pledge")
	}

	if len(*dbName) == 0 {
		return errors.New("database name cannot be empty. specify using the -db flag. run `migrate -h` for help")
	}
	if *dry && *skip != "" {
		return errors.New("cannot skip ahead with dry mode")
	}

	// Validate flags for each type of database and set appropriate
	// defaults
	switch *dbType {
	case "sqlite":
		if *dbUser != "" {
			return errors.New("sqlite does not support the -u flag")
		}
		if *dbHost != "127.0.0.1" {
			return errors.New("sqlite does not support the -h flag")
		}
		if *dbPort != 0 {
			return errors.New("sqlite does not support the -p flag")
		}
		if *pass != "" {
			return errors.New("sqlite does not support the -pass flag")
		}
		if *sslKey != "" || *sslCert != "" || *sslCA != "" || *sslServerName != "" {
			return errors.New("sqlite does not support ssl")
		}
	case "postgres":
		if *sslServerName != "" {
			return errors.New("postgres does not support the -ssl-server flag")
		}
		if *dbUser == "" {
			*dbUser = "postgres"
		}
		if *dbPort == 0 {
			*dbPort = 5432
		}
	case "mysql":
		if *dbUser == "" {
			*dbUser = "root"
		}
		if *dbPort == 0 {
			*dbPort = 3306
		}
	default:
		return fmt.Errorf("unknown db type %q (mysql, postgres, sqlite allowed)", *dbType)
	}

	// Request database password if not provided as a flag argument
	var password []byte
	if *dbType != "sqlite" {
		if len(*pass) == 0 {
			fmt.Printf("%s database password: ", *dbName)
			var err error
			password, err = terminal.ReadPassword(int(syscall.Stdin))
			if err != nil {
				return errors.Wrap(err, "read pass")
			}
			fmt.Printf("\n")
		} else {
			password = []byte(*pass)
		}
	}

	// Prepare our database-specific configs
	var db migrate.Store
	switch *dbType {
	case "mysql":
		var err error
		db, err = mysql.New(*dbUser, string(password), *dbHost,
			*dbName, *dbPort, *sslKey, *sslCert, *sslCA,
			*sslServerName)
		if err != nil {
			return errors.Wrap(err, "mysql new")
		}
	case "sqlite":
		db = sqlite.New(*dbName)
	case "postgres":
		db = postgres.New(*dbUser, string(password), *dbHost, *dbName,
			*dbPort, *sslKey, *sslCert, *sslCA)
	default:
		return fmt.Errorf("unknown db type: %s", *dbType)
	}
	if *sslKey != "" {
		fmt.Println("using tls")
	}
	if err := db.Open(); err != nil {
		return errors.Wrap(err, "open")
	}

	// Prepare our database for migrations and collect the relevant files.
	m, err := migrate.New(db, migrate.StdLogger{}, *migrationDir, *skip)
	if err != nil {
		return err
	}
	if *dry {
		if len(m.Migrations) == len(m.Files) {
			fmt.Println("up to date")
			return nil
		}
		for i := len(m.Migrations); i < len(m.Files); i++ {
			fmt.Println("would migrate", m.Files[i].Name())
		}
		return nil
	}
	migrated, err := m.Migrate()
	if err != nil {
		return err
	}
	if migrated {
		fmt.Println("success")
	} else {
		fmt.Println("up to date")
	}
	return nil
}