github.com/team-ide/go-dialect@v1.9.20/sync.go (about) 1 package main 2 3 import ( 4 "database/sql" 5 "encoding/json" 6 "github.com/team-ide/go-dialect/dialect" 7 "github.com/team-ide/go-dialect/worker" 8 "strings" 9 ) 10 11 func doSync() { 12 if *targetDialect == "" { 13 println("请输入 同步 目标 数据库类型") 14 return 15 } 16 if *syncOwner == "" { 17 println("请输入 同步 库或表所属者") 18 return 19 } 20 db, err := getDbInfo(*sourceDialect, *sourceUser, *sourcePassword, *sourceHost, *sourcePort, *sourceDatabase) 21 if err != nil { 22 panic(err) 23 } 24 dia, err := dialect.NewDialect(*sourceDialect) 25 if err != nil { 26 panic(err) 27 } 28 if db == nil || dia == nil { 29 panic("sourceDialect [" + *sourceDialect + "] not support") 30 } 31 32 targetDb, err := getDbInfo(*targetDialect, *targetUser, *targetPassword, *targetHost, *targetPort, *targetDatabase) 33 if err != nil { 34 panic(err) 35 } 36 targetDia, err := dialect.NewDialect(*targetDialect) 37 if err != nil { 38 panic(err) 39 } 40 if targetDb == nil || targetDia == nil { 41 panic("targetDialect [" + *targetDialect + "] not support") 42 } 43 44 password := *importOwnerCreatePassword 45 if password == "" { 46 password = *sourcePassword 47 } 48 var owners = getSyncOwners(*syncOwner) 49 for _, owner := range owners { 50 owner.Password = password 51 } 52 task := worker.NewTaskSync(db, dia, targetDb, targetDia, 53 func(owner *worker.TaskSyncOwner) (workDb *sql.DB, err error) { 54 ownerName := owner.TargetName 55 if ownerName == "" { 56 ownerName = owner.SourceName 57 } 58 ownerUsername := owner.Username 59 ownerPassword := owner.Password 60 61 if ownerPassword == "" { 62 ownerPassword = password 63 } 64 65 if *sourceDialect == "sqlite" || *sourceDialect == "sqlite3" { 66 workDb = db 67 return 68 } 69 if *sourceDialect == "mysql" { 70 if ownerUsername == "" { 71 ownerUsername = *sourceUser 72 } 73 workDb, err = getDbInfo(*sourceDialect, ownerUsername, ownerPassword, *sourceHost, *sourcePort, ownerName) 74 return 75 } 76 if ownerUsername == "" { 77 ownerUsername = ownerName 78 } 79 workDb, err = getDbInfo(*sourceDialect, ownerName, password, *sourceHost, *sourcePort, *sourceDatabase) 80 return 81 }, 82 &worker.TaskSyncParam{ 83 Owners: owners, 84 SyncStruct: *syncStruct == "" || *syncStruct == "1" || *syncStruct == "true", 85 SyncData: *syncData == "" || *syncData == "1" || *syncData == "true", 86 OwnerCreateIfNotExist: *syncOwnerCreateIfNotExist == "1" || *syncOwnerCreateIfNotExist == "true", 87 FormatIndexName: func(ownerName string, tableName string, index *dialect.IndexModel) string { 88 return tableName + "_" + index.IndexName 89 }, 90 ErrorContinue: true, 91 OnProgress: func(progress *worker.TaskProgress) { 92 progress.OnError = func(err error) { 93 dataBytes, _ := json.Marshal(progress) 94 println("progress:" + string(dataBytes)) 95 println("progress error:" + err.Error()) 96 } 97 //println(string(bs)) 98 }, 99 }) 100 err = task.Start() 101 if err != nil { 102 panic(err) 103 } 104 println("同步成功") 105 } 106 107 func getSyncOwners(ownerInfoStr string) (owners []*worker.TaskSyncOwner) { 108 ownerStrList := strings.Split(ownerInfoStr, ",") 109 for _, ownerStr := range ownerStrList { 110 ss := strings.Split(ownerStr, "=") 111 if len(ss) > 1 { 112 owners = append(owners, &worker.TaskSyncOwner{ 113 SourceName: strings.TrimSpace(ss[0]), 114 TargetName: strings.TrimSpace(ss[1]), 115 }) 116 } else if len(ss) > 0 { 117 owners = append(owners, &worker.TaskSyncOwner{ 118 SourceName: strings.TrimSpace(ss[0]), 119 }) 120 } 121 } 122 return 123 }