|
@@ -4,7 +4,6 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"log"
|
|
"log"
|
|
. "online_syncto_offline/config"
|
|
. "online_syncto_offline/config"
|
|
- . "online_syncto_offline/db"
|
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
. "app.yhyue.com/moapp/jybase/common"
|
|
. "app.yhyue.com/moapp/jybase/common"
|
|
@@ -18,7 +17,7 @@ type Entity interface {
|
|
}
|
|
}
|
|
|
|
|
|
//
|
|
//
|
|
-func sync_add(m *mysql.Mysql, tableName string, saveField []string, id int64, f func(fv map[string]interface{})) (lastId int64) {
|
|
|
|
|
|
+func sync_add(main, to *mysql.Mysql, tableName string, saveField []string, id int64, f func(fv map[string]interface{})) (lastId int64) {
|
|
log.Println("开始同步", tableName, "表。。。")
|
|
log.Println("开始同步", tableName, "表。。。")
|
|
index := 0
|
|
index := 0
|
|
array := []interface{}{}
|
|
array := []interface{}{}
|
|
@@ -29,7 +28,7 @@ func sync_add(m *mysql.Mysql, tableName string, saveField []string, id int64, f
|
|
} else {
|
|
} else {
|
|
q = fmt.Sprintf(`select * from %s where id>%d order by id`, tableName, id)
|
|
q = fmt.Sprintf(`select * from %s where id>%d order by id`, tableName, id)
|
|
}
|
|
}
|
|
- m.SelectByBath(Config.SelectBathSize, func(l *[]map[string]interface{}) {
|
|
|
|
|
|
+ main.SelectByBath(Config.SelectBathSize, func(l *[]map[string]interface{}) {
|
|
for _, v := range *l {
|
|
for _, v := range *l {
|
|
index++
|
|
index++
|
|
lastId = Int64All(v["id"])
|
|
lastId = Int64All(v["id"])
|
|
@@ -41,13 +40,13 @@ func sync_add(m *mysql.Mysql, tableName string, saveField []string, id int64, f
|
|
}
|
|
}
|
|
if index%Config.InsertBathSize == 0 {
|
|
if index%Config.InsertBathSize == 0 {
|
|
log.Println("同步", tableName, "表", index)
|
|
log.Println("同步", tableName, "表", index)
|
|
- Mysql_To.InsertIgnoreBatch(tableName, saveField, array)
|
|
|
|
|
|
+ to.InsertIgnoreBatch(tableName, saveField, array)
|
|
array = []interface{}{}
|
|
array = []interface{}{}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}, q)
|
|
}, q)
|
|
if len(array) > 0 {
|
|
if len(array) > 0 {
|
|
- Mysql_To.InsertIgnoreBatch(tableName, saveField, array)
|
|
|
|
|
|
+ to.InsertIgnoreBatch(tableName, saveField, array)
|
|
array = []interface{}{}
|
|
array = []interface{}{}
|
|
}
|
|
}
|
|
log.Println("同步", tableName, "表结束。。。", index)
|
|
log.Println("同步", tableName, "表结束。。。", index)
|
|
@@ -55,11 +54,11 @@ func sync_add(m *mysql.Mysql, tableName string, saveField []string, id int64, f
|
|
}
|
|
}
|
|
|
|
|
|
//
|
|
//
|
|
-func sync_update(tableName, start_layout, end_layout string, saveFields []string, id int64) {
|
|
|
|
|
|
+func sync_update(main, to *mysql.Mysql, tableName, start_layout, end_layout string, saveFields []string, id int64) {
|
|
log.Println("开始同步更新", tableName, "表 。。。")
|
|
log.Println("开始同步更新", tableName, "表 。。。")
|
|
index := 0
|
|
index := 0
|
|
array := [][]interface{}{}
|
|
array := [][]interface{}{}
|
|
- Mysql_Main.SelectByBath(Config.SelectBathSize, func(l *[]map[string]interface{}) {
|
|
|
|
|
|
+ main.SelectByBath(Config.SelectBathSize, func(l *[]map[string]interface{}) {
|
|
for _, v := range *l {
|
|
for _, v := range *l {
|
|
index++
|
|
index++
|
|
datas := []interface{}{}
|
|
datas := []interface{}{}
|
|
@@ -69,35 +68,35 @@ func sync_update(tableName, start_layout, end_layout string, saveFields []string
|
|
array = append(array, datas)
|
|
array = append(array, datas)
|
|
if index%Config.UpdateBathSize == 0 {
|
|
if index%Config.UpdateBathSize == 0 {
|
|
log.Println("同步更新", tableName, "表", index)
|
|
log.Println("同步更新", tableName, "表", index)
|
|
- Mysql_To.UpdateBath(tableName, saveFields, array)
|
|
|
|
|
|
+ to.UpdateBath(tableName, saveFields, array)
|
|
array = [][]interface{}{}
|
|
array = [][]interface{}{}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}, fmt.Sprintf(`select * from %s where id<? and timestamp>=? and timestamp<?`, tableName), id, start_layout, end_layout)
|
|
}, fmt.Sprintf(`select * from %s where id<? and timestamp>=? and timestamp<?`, tableName), id, start_layout, end_layout)
|
|
if len(array) > 0 {
|
|
if len(array) > 0 {
|
|
- Mysql_To.UpdateBath(tableName, saveFields, array)
|
|
|
|
|
|
+ to.UpdateBath(tableName, saveFields, array)
|
|
array = [][]interface{}{}
|
|
array = [][]interface{}{}
|
|
}
|
|
}
|
|
log.Println("同步更新", tableName, "表结束。。。", index)
|
|
log.Println("同步更新", tableName, "表结束。。。", index)
|
|
}
|
|
}
|
|
|
|
|
|
//全量同步
|
|
//全量同步
|
|
-func sync_full(e Entity) {
|
|
|
|
- if Mysql_To.Address != "172.17.4.195:3366" || Mysql_To.DBName != "buryingpoint" {
|
|
|
|
|
|
+func sync_full(e Entity, from, to *mysql.Mysql) {
|
|
|
|
+ if to.Address != "172.17.4.195:3366" || (to.DBName != "buryingpoint" && to.DBName != "bxt") {
|
|
log.Println("数据库配置错误")
|
|
log.Println("数据库配置错误")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- _, err := Mysql_To.ExecBySql(`drop table ` + e.TableName())
|
|
|
|
|
|
+ _, err := to.ExecBySql(`drop table ` + e.TableName())
|
|
if err == nil {
|
|
if err == nil {
|
|
log.Println(e.TableName(), "清空表完成")
|
|
log.Println(e.TableName(), "清空表完成")
|
|
} else {
|
|
} else {
|
|
log.Println(e.TableName(), "清空表出错", err)
|
|
log.Println(e.TableName(), "清空表出错", err)
|
|
}
|
|
}
|
|
- list := Mysql_From_Jianyu.SelectBySql(`SHOW CREATE TABLE ` + e.TableName())
|
|
|
|
|
|
+ list := from.SelectBySql(`SHOW CREATE TABLE ` + e.TableName())
|
|
if list != nil && len(*list) == 1 {
|
|
if list != nil && len(*list) == 1 {
|
|
createTable := (*list)[0]["Create Table"].(string)
|
|
createTable := (*list)[0]["Create Table"].(string)
|
|
log.Println(e.TableName(), "建表语句", createTable)
|
|
log.Println(e.TableName(), "建表语句", createTable)
|
|
- _, err := Mysql_To.ExecBySql(createTable)
|
|
|
|
|
|
+ _, err := to.ExecBySql(createTable)
|
|
if err == nil {
|
|
if err == nil {
|
|
log.Println(e.TableName(), "表创建成功")
|
|
log.Println(e.TableName(), "表创建成功")
|
|
} else {
|
|
} else {
|
|
@@ -108,7 +107,7 @@ func sync_full(e Entity) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
var lastId int64
|
|
var lastId int64
|
|
- array := Mysql_From_Jianyu.SelectBySql(`select id from ` + e.TableName() + ` order by id desc limit 1`)
|
|
|
|
|
|
+ array := from.SelectBySql(`select id from ` + e.TableName() + ` order by id desc limit 1`)
|
|
if len(*array) == 1 {
|
|
if len(*array) == 1 {
|
|
lastId = (*array)[0]["id"].(int64)
|
|
lastId = (*array)[0]["id"].(int64)
|
|
} else {
|
|
} else {
|
|
@@ -116,12 +115,12 @@ func sync_full(e Entity) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
fields := []string{}
|
|
fields := []string{}
|
|
- fields_list := Mysql_From_Jianyu.SelectBySql(`describe ` + e.TableName())
|
|
|
|
|
|
+ fields_list := from.SelectBySql(`describe ` + e.TableName())
|
|
for _, v := range *fields_list {
|
|
for _, v := range *fields_list {
|
|
fields = append(fields, v["Field"].(string))
|
|
fields = append(fields, v["Field"].(string))
|
|
}
|
|
}
|
|
for {
|
|
for {
|
|
- id := sync_add(Mysql_From_Jianyu, e.TableName(), fields, -1, nil)
|
|
|
|
|
|
+ id := sync_add(from, to, e.TableName(), fields, -1, nil)
|
|
if id >= lastId {
|
|
if id >= lastId {
|
|
break
|
|
break
|
|
}
|
|
}
|