forked from hidu/mysql-schema-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
67 lines (61 loc) · 1.56 KB
/
sync.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
package sync
import (
"strings"
"github.com/admpub/mysql-schema-sync/internal"
)
func Sync(c *Config, mc *EmailConfig, dbOperators ...internal.DBOperator) (sta *internal.Statics, err error) {
cfg := new(internal.Config)
cfg.SourceDSN = c.SourceDSN
cfg.DestDSN = c.DestDSN
cfg.Sync = c.Sync
cfg.Drop = c.Drop
c.AlterIgnore = strings.TrimSpace(c.AlterIgnore)
c.AlterIgnore = strings.TrimLeft(c.AlterIgnore, `{`)
c.AlterIgnore = strings.TrimRight(c.AlterIgnore, `}`)
c.AlterIgnore = strings.TrimRight(c.AlterIgnore, `,`)
if len(c.AlterIgnore) > 0 {
to := &map[string]*internal.AlterIgnoreTable{}
err = internal.ParseJSON(`{`+c.AlterIgnore+`}`, &to)
if err != nil {
return
}
cfg.AlterIgnore = *to
}
if mc != nil {
cfg.Email = &internal.EmailStruct{
SendMailAble: mc.On,
SMTPHost: mc.SMTPHost,
From: mc.From,
Password: mc.Password,
To: mc.To,
}
}
if len(c.MailTo) > 0 && cfg.Email != nil {
cfg.Email.To = c.MailTo
}
if cfg.Tables == nil {
cfg.Tables = []string{}
}
if len(c.Tables) > 0 {
_ts := strings.Split(c.Tables, ",")
for _, _name := range _ts {
_name = strings.TrimSpace(_name)
if len(_name) > 0 {
cfg.Tables = append(cfg.Tables, _name)
}
}
}
if cfg.SkipTables == nil {
cfg.SkipTables = []string{}
}
if len(c.SkipTables) > 0 {
_ts := strings.Split(c.SkipTables, ",")
for _, _name := range _ts {
_name = strings.TrimSpace(_name)
if len(_name) > 0 {
cfg.SkipTables = append(cfg.SkipTables, _name)
}
}
}
return internal.CheckSchemaDiff(cfg, dbOperators...), nil
}