forked from hidu/mysql-schema-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (63 loc) · 1.69 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
package main
import (
"flag"
"fmt"
"github.com/Guazi-inc/schema-sync/internal"
"log"
"os"
"strings"
)
var configPath = flag.String("conf", "./config.json", "json config file path")
var sync = flag.Bool("sync", false, "sync shcema change to dest db")
var drop = flag.Bool("drop", false, "drop fields,index,foreign key")
var source = flag.String("source", "", "mysql dsn source,eg: test@(10.10.0.1:3306)/test\n\twhen it is not empty ignore [-conf] param")
var dest = flag.String("dest", "", "mysql dsn dest,eg test@(127.0.0.1:3306)/imis")
var tables = flag.String("tables", "", "table names to check\n\teg : product_base,order_*")
var mailTo = flag.String("mail_to", "", "overwrite config's email.to")
func init() {
log.SetFlags(log.Lshortfile | log.Ldate)
df := flag.Usage
flag.Usage = func() {
df()
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "mysql schema sync tools "+internal.Version)
fmt.Fprintln(os.Stderr, internal.AppURL+"\n")
}
}
var cfg *internal.Config
func main() {
flag.Parse()
if *source == "" {
cfg = internal.LoadConfig(*configPath)
} else {
cfg = new(internal.Config)
cfg.SourceDSN = *source
cfg.DestDSN = *dest
}
cfg.Sync = *sync
cfg.Drop = *drop
if *mailTo != "" && cfg.Email != nil {
cfg.Email.To = *mailTo
}
if cfg.Tables == nil {
cfg.Tables = []string{}
}
if *tables != "" {
_ts := strings.Split(*tables, ",")
for _, _name := range _ts {
_name = strings.TrimSpace(_name)
if _name != "" {
cfg.Tables = append(cfg.Tables, _name)
}
}
}
defer (func() {
if err := recover(); err != nil {
log.Println(err)
cfg.SendMailFail(fmt.Sprintf("%s", err))
log.Fatalln("exit")
}
})()
cfg.Check()
internal.CheckSchemaDiff(cfg)
}