58 Star 193 Fork 74

hidu/mysql-schema-sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
config.go 3.03 KB
一键复制 编辑 原始数据 按行查看 历史
hidu 提交于 2015-11-08 15:56 +08:00 . v0.3 add foreign support
package internal
import (
"encoding/json"
"log"
"os"
)
// Config config struct
type Config struct {
SourceDSN string `json:"source"`
DestDSN string `json:"dest"`
AlterIgnore map[string]*AlterIgnoreTable `json:"alter_ignore"`
Tables []string `json:"tables"`
Email *EmailStruct `json:"email"`
ConfigPath string
Sync bool
Drop bool
}
func (cfg *Config) String() string {
ds, _ := json.MarshalIndent(cfg, " ", " ")
return string(ds)
}
// AlterIgnoreTable table's ignore info
type AlterIgnoreTable struct {
Column []string `json:"column"`
Index []string `json:"index"`
ForeignKey []string `json:"foreign"` //外键
}
// IsIgnoreField isIgnore
func (cfg *Config) IsIgnoreField(table string, name string) bool {
for tname, dit := range cfg.AlterIgnore {
if simpleMatch(tname, table, "IsIgnoreField_table") {
for _, col := range dit.Column {
if simpleMatch(col, name, "IsIgnoreField_colum") {
return true
}
}
}
}
return false
}
// ChechMatchTables check table is match
func (cfg *Config) ChechMatchTables(name string) bool {
if len(cfg.Tables) == 0 {
return true
}
for _, tableName := range cfg.Tables {
if simpleMatch(tableName, name, "ChechMatchTables") {
return true
}
}
return false
}
// Check check config
func (cfg *Config) Check() {
if cfg.SourceDSN == "" {
log.Fatal("source dns is empty")
}
if cfg.DestDSN == "" {
log.Fatal("dest dns is empty")
}
// log.Println("config:\n", cfg)
}
// IsIgnoreIndex is index ignore
func (cfg *Config) IsIgnoreIndex(table string, name string) bool {
for tname, dit := range cfg.AlterIgnore {
if simpleMatch(tname, table, "IsIgnoreIndex_table") {
for _, index := range dit.Index {
if simpleMatch(index, name) {
return true
}
}
}
}
return false
}
// IsIgnoreForeignKey 检查外键是否忽略掉
func (cfg *Config) IsIgnoreForeignKey(table string, name string) bool {
for tname, dit := range cfg.AlterIgnore {
if simpleMatch(tname, table, "IsIgnoreForeignKey_table") {
for _, foreignName := range dit.ForeignKey {
if simpleMatch(foreignName, name) {
return true
}
}
}
}
return false
}
// SendMailFail send fail mail
func (cfg *Config) SendMailFail(errStr string) {
if cfg.Email == nil {
log.Println("email conf is empty,skip send mail")
return
}
_host, _ := os.Hostname()
title := "[mysql-schema-sync][" + _host + "]failed"
body := "error:<font color=red>" + errStr + "</font><br/>"
body += "host:" + _host + "<br/>"
body += "config-file:" + cfg.ConfigPath + "<br/>"
body += "dest_dsn:" + cfg.DestDSN + "<br/>"
pwd, _ := os.Getwd()
body += "pwd:" + pwd + "<br/>"
cfg.Email.SendMail(title, body)
}
// LoadConfig load config file
func LoadConfig(confPath string) *Config {
var cfg *Config
err := loadJSONFile(confPath, &cfg)
if err != nil {
log.Fatalln("load json conf:", confPath, "failed:", err)
}
cfg.ConfigPath = confPath
// if *mailTo != "" {
// cfg.Email.To = *mailTo
// }
return cfg
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hidu/mysql-schema-sync.git
git@gitee.com:hidu/mysql-schema-sync.git
hidu
mysql-schema-sync
mysql-schema-sync
21680d8e8e24

搜索帮助