-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (45 loc) · 2.14 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
package main
import (
"fmt"
"strings"
comparer "github.com/PZBird/go-mysql-compare/comparer"
"github.com/PZBird/go-mysql-compare/configuration"
db "github.com/PZBird/go-mysql-compare/database"
"github.com/PZBird/go-mysql-compare/model"
)
func main() {
config := configuration.LoadConfiguration("./config.json")
db1 := db.Connect(db.ConnectionString(&config.Db1))
db2 := db.Connect(db.ConnectionString(&config.Db2))
hostnameLeft := config.Db1.Hostname + ":" + config.Db1.Port
hostnameRight := config.Db2.Hostname + ":" + config.Db2.Port
databaseLeft := db.GetDatabaseTablesOrFail(db1, config.Db1.DatabasesSuffix, hostnameLeft)
databaseRight := db.GetDatabaseTablesOrFail(db2, config.Db2.DatabasesSuffix, hostnameRight)
compareResult := comparer.Compare(databaseLeft, databaseRight, config)
genCteateDatabaseScripts(compareResult.LeftDatabaseExtraSchemas, hostnameLeft)
genCteateDatabaseScripts(compareResult.RightDatabaseExtraSchemas, hostnameRight)
leftColumnsSql, rightColumsSql := genColumnDiffScripts(&compareResult)
fmt.Println(leftColumnsSql)
fmt.Println(rightColumsSql)
fmt.Println(compareResult)
}
func genCteateDatabaseScripts(databaseExtraSchemas []*model.DatabaseSchema, hostnameLeft string) {
if len(databaseExtraSchemas) > 0 {
fmt.Printf("New schemas for the %s:\n", hostnameLeft)
for _, schema := range databaseExtraSchemas {
query := db.CreateDatabase(schema)
fmt.Println(query)
}
}
}
func genColumnDiffScripts(result *comparer.ComparerResult) (leftColumSql, reightColumnSql string) {
var leftColumnSqlBuilder strings.Builder
var rightColumnSqlBuilder strings.Builder
db.GenNewColumnsScripts(&leftColumnSqlBuilder, result.ColumnToInsertDBLeft)
db.GenNewColumnsScripts(&rightColumnSqlBuilder, result.ColumnToInsertDBRight)
db.GenRemovedColumnsScripts(&leftColumnSqlBuilder, result.ColumnToRemoveDBLeft)
db.GenRemovedColumnsScripts(&rightColumnSqlBuilder, result.ColumnToRemoveDBRight)
db.GenModifiedColumnsScripts(&leftColumnSqlBuilder, result.ColumnToModifyDBLeft)
db.GenModifiedColumnsScripts(&rightColumnSqlBuilder, result.ColumnToModifyDBRight)
return leftColumnSqlBuilder.String(), rightColumnSqlBuilder.String()
}