forked from sqldef/sqldef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqldef.go
147 lines (129 loc) · 3.04 KB
/
sqldef.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
package sqldef
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/k0kubun/sqldef/database"
"github.com/k0kubun/sqldef/schema"
)
type Options struct {
DesiredDDLs string
CurrentFile string
DryRun bool
Export bool
SkipDrop bool
BeforeApply string
Config database.GeneratorConfig
}
// Main function shared by all commands
func Run(generatorMode schema.GeneratorMode, db database.Database, sqlParser database.Parser, options *Options) {
currentDDLs, err := db.DumpDDLs()
if err != nil {
log.Fatalf("Error on DumpDDLs: %s", err)
}
if options.Export {
if currentDDLs == "" {
fmt.Printf("-- No table exists --\n")
} else {
ddls, err := schema.ParseDDLs(generatorMode, sqlParser, currentDDLs)
if err != nil {
log.Fatal(err)
}
ddls = schema.FilterTables(ddls, options.Config)
for i, ddl := range ddls {
if i > 0 {
fmt.Println()
}
fmt.Printf("%s;\n", ddl.Statement())
}
}
return
}
ddls, err := schema.GenerateIdempotentDDLs(generatorMode, sqlParser, options.DesiredDDLs, currentDDLs, options.Config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if len(ddls) == 0 {
fmt.Println("-- Nothing is modified --")
return
}
if options.DryRun || len(options.CurrentFile) > 0 {
showDDLs(ddls, options.SkipDrop, options.BeforeApply)
return
}
err = database.RunDDLs(db, ddls, options.SkipDrop, options.BeforeApply)
if err != nil {
log.Fatal(err)
}
}
func ParseFiles(files []string) []string {
if len(files) == 0 {
panic("ParseFiles got empty files") // assume default:"-"
}
result := make([]string, 0, len(files))
for _, f := range files {
result = append(result, strings.Split(f, ",")...)
}
for i, r := range result {
result[i] = strings.TrimSpace(r)
}
return result
}
func ReadFiles(filepaths []string) (string, error) {
var result strings.Builder
for _, filepath := range filepaths {
f, err := ReadFile(filepath)
if err != nil {
return "", err
}
_, err = result.WriteString(f)
if err != nil {
return "", err
}
}
return result.String(), nil
}
func ReadFile(filepath string) (string, error) {
var err error
var buf []byte
if filepath == "-" {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
return "", fmt.Errorf("stdin is not piped")
}
buf, err = ioutil.ReadAll(os.Stdin)
} else {
buf, err = ioutil.ReadFile(filepath)
}
if err != nil {
return "", err
}
return string(buf), nil
}
func showDDLs(ddls []string, skipDrop bool, beforeApply string) {
fmt.Println("-- dry run --")
if len(beforeApply) > 0 {
fmt.Println(beforeApply)
}
for _, ddl := range ddls {
if skipDrop && strings.Contains(ddl, "DROP") {
fmt.Printf("-- Skipped: %s;\n", ddl)
continue
}
fmt.Printf("%s;\n", ddl)
}
}
func ParseSkipTables(skipFile string) []string {
skipTables := []string{}
if raw, err := ReadFile(skipFile); err == nil {
trimmedRaw := strings.TrimSpace(raw)
if trimmedRaw == "" {
return []string{}
}
skipTables = strings.Split(strings.Trim(trimmedRaw, "\n"), "\n")
}
return skipTables
}