-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (135 loc) · 4.19 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
package main
import (
"flag"
"fmt"
"os"
"path"
"runtime/debug"
"github.com/musinit/migradaptor/builder"
)
func main() {
var (
dstType string
srcMigrPath string
dstMigrPath string
flgVersion bool
helpPtr bool
)
flag.BoolVar(&flgVersion, "version", false, "if true, print version and exit")
flag.BoolVar(&helpPtr, "help", false, "print help information")
flag.StringVar(&dstType, "dst-lib", "golang-migrate", "destination library format")
flag.StringVar(&srcMigrPath, "src", "src", "source migrations folder")
flag.StringVar(&dstMigrPath, "dst", "dst", "destination migrations folder")
flag.Parse()
switch {
case flgVersion:
println(GetVersion())
os.Exit(0)
case helpPtr:
PrintHelp()
os.Exit(0)
}
if err := builder.ValidateInput(&dstType, &srcMigrPath, &dstMigrPath); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "validate error: %s\n Run migrator -help for information.\n", err.Error())
os.Exit(1)
}
destType, err := builder.GetDstType(dstType)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "get dstType type error: %s\n", err.Error())
os.Exit(1)
}
if _, err := os.Stat(srcMigrPath); os.IsNotExist(err) {
_, _ = fmt.Fprintf(os.Stderr, "source migration directory %s doesn't exists\n", srcMigrPath)
os.Exit(1)
}
pwd, err := os.Getwd()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "can't get current directory: %s\n", err.Error())
os.Exit(1)
}
dstMigrPath = path.Join(pwd, dstMigrPath)
srcMigrPath = path.Join(pwd, srcMigrPath)
if _, err := os.Stat(dstMigrPath); os.IsNotExist(err) {
if err := os.MkdirAll(dstMigrPath, os.ModePerm); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "create dest dir error: %s\n", err.Error())
os.Exit(1)
}
} else {
if err := builder.RemoveContents(dstMigrPath); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "clear dest migrations folder error: %s\n", err.Error())
os.Exit(1)
}
}
files, err := os.ReadDir(srcMigrPath)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "read src migrations folder error: %s\n", err.Error())
os.Exit(1)
}
maxTime := int64(0)
for _, file := range files {
if !builder.IsSqlMigrationFile(file.Name()) {
continue
}
lf, err := os.Open(path.Join(srcMigrPath, file.Name()))
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "open src migrations folder error: %s\n", err.Error())
os.Exit(1)
}
defer func() {
if err := lf.Close(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "closing src migrations folder error: %s\n", err.Error())
os.Exit(1)
}
}()
lines, err := builder.ReadFileLines(lf)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "reading src migrations file lines error: %s\n", err.Error())
os.Exit(1)
}
var upMigr, downMigr []string
switch destType {
default:
upMigr, downMigr = builder.BuildMigrationData(lines)
}
timestamp, name, err := builder.ParseFilename(file.Name())
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "filename parsing error: %s\n", err.Error())
os.Exit(1)
}
if timestamp <= maxTime {
timestamp = maxTime + 1
}
maxTime = timestamp
println(fmt.Sprintf("%d : %s", timestamp, name))
upMgrFn := fmt.Sprintf("%d_%s.up.sql", timestamp, name)
downMgrFn := fmt.Sprintf("%d_%s.down.sql", timestamp, name)
// create migration .up file
if err := builder.CreateAndWrite(dstMigrPath, upMgrFn, upMigr); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "writing destination migrations error: %s\n", err.Error())
os.Exit(1)
}
// migration .down file
if err := builder.CreateAndWrite(dstMigrPath, downMgrFn, downMigr); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "writing destination migrations error: %s\n", err.Error())
os.Exit(1)
}
}
println("finished")
}
func PrintHelp() {
helpText := `
Usage: migradaptor [options] ...
Migrate your sql migrations files between different lib formats.
Options:
-source-type=rubenv-sql-migration Source library of sql files, that need to transform.
-src="source migrations path" Source migrations folder.
-dst="destination migrations path" Destination migrations folder.
`
println(helpText)
}
func GetVersion() string {
if buildInfo, ok := debug.ReadBuildInfo(); ok && buildInfo.Main.Version != "(devel)" {
return buildInfo.Main.Version
}
return "dev"
}