-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (74 loc) · 2.88 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
package main
import (
"fmt"
"github.com/docopt/docopt.go"
"golang.org/x/exp/slices"
"log"
"os"
"sqlow/config"
"sqlow/io"
"sqlow/migrations"
"strings"
)
var VERSION = "development"
func main() {
usage := "sqlow (version: " + VERSION + `)
Usage:
sqlow [options] -p<password> run <path>
sqlow --version
sqlow --help
Options:
-h --help Show this Help.
-r --recursive Traverse the directory structure for .yml or .yaml files.
-d --dry-run Print the non-check SQL statements that will be run (this will run the migrations, but will roll them back).
-c --config=<configPath> Path to config [default: ./config.yml].
-e --engine=<engine> Database engine (overrides config).
-h --host=<host> Database host (overrides config).
-P --port=<port> Database port (overrides config).
-s --schema=<schema> Database schema (overrides config).
-u --username=<username> Database username (overrides config).
-p --password=<password> Database password (required).
-o --options=<opts> Comma separated list of key:value options (merges with config).
Examples:
sqlow -r -pSup4rS@f3P@ssw0rd run ./migrations
sqlow -pSup4rS@f3P@ssw0rd run ./migrations/a_migration.yml
sqlow -c ./my-config.yml -pSup4rS@f3P@ssw0rd run ./migrations/a_migration.yml
sqlow -r -c ./my-config.yml -udbuser -pSup4rS@f3P@ssw0rd run ./migrations/`
arguments, _ := docopt.ParseDoc(usage)
version, _ := arguments.Bool("--version")
if version {
fmt.Println("sqlow " + VERSION)
os.Exit(0)
}
path, _ := arguments.String("<path>")
isRecursive, _ := arguments.Bool("--recursive")
isDryRun, _ := arguments.Bool("--dry-run")
configPath, _ := arguments.String("--config")
engine := arguments["--engine"]
host := arguments["--host"]
port := arguments["--port"]
schema := arguments["--schema"]
username := arguments["--username"]
password := arguments["--password"]
options := arguments["--options"]
log.Println("Loading config...")
configuration := config.FromYAML(configPath).WithOverrides(engine, host, port, schema, username, password, options)
if slices.Contains([]string{"maria", "mariadb", "mysql"}, configuration.Engine) && isDryRun {
log.Println("MariaDB and MySQL don't support dry-runs as they can implicitly end transactions!")
log.Println("For more information, see:")
log.Println("* https://mariadb.com/kb/en/sql-statements-that-cause-an-implicit-commit/")
log.Fatalln("* https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html")
}
log.Println("Started.")
driver := configuration.MakeDriver(isDryRun)
driver.Connect()
defer driver.Close()
files := io.GetAllFiles(path, isRecursive)
for _, file := range files {
if strings.HasSuffix(file, ".yml") || strings.HasSuffix(file, ".yaml") {
migrationList := migrations.ResolveMigrations(file)
migrations.RunMigrations(migrationList, driver)
}
}
log.Println("Done!")
}