-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (80 loc) · 1.66 KB
/
Copy pathmain.go
File metadata and controls
89 lines (80 loc) · 1.66 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package indexdiff
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strings"
)
type index struct {
scheme string
table string
index string
enabled bool
unique bool
columns []string
included []string
}
func (idx *index) String() string {
result := fmt.Sprintf("%s.%s (%s)", idx.scheme, idx.table, strings.Join(idx.columns, ", "))
if len(idx.included) > 0 {
result += fmt.Sprintf(" INCLUDED(%s)", strings.Join(idx.included, ", "))
}
if !idx.enabled {
result += " DISABLED"
}
if idx.unique {
result += " UNIQUE"
}
result += " --NAME=" + idx.index
return result
}
func SaveSortedIndexes() {
cfgs, err := loadConfiguration()
if err != nil {
log.Fatal(err)
}
done := make(chan bool, len(cfgs))
for _, cfg := range cfgs {
go getAnSaveSortedIndexes(cfg, done)
}
for i := 0; i < len(cfgs); i ++ {
<-done
}
}
func getAnSaveSortedIndexes(cfg *Config, done chan<- bool) {
var engine Engine
if cfg.Port == 0 {
engine = NewMsSqlEngine(cfg)
} else {
engine = NewPostgresEngine(cfg)
}
indexes := engine.GetIndexes()
fileName := cfg.Database+"__"+strings.Replace(cfg.Server, `\`, "_", -1)
if cfg.Port != 0 {
fileName += fmt.Sprintf("_%d", cfg.Port)
}
saveSortedIndexes(fileName, indexes)
done <- true
}
func saveSortedIndexes(fileName string, indexes []*index) {
strIndexes := make([]string, len(indexes))
for i, idx := range indexes {
strIndexes[i] = idx.String()
}
sort.Strings(strIndexes)
file, err := os.Create(fileName + ".sql")
if err != nil {
log.Fatal(err)
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range strIndexes {
fmt.Fprintln(w, line)
}
err = w.Flush()
if err != nil {
log.Fatal(err)
}
}