-
Notifications
You must be signed in to change notification settings - Fork 1
/
priority_groups.go
93 lines (82 loc) · 2.03 KB
/
priority_groups.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
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
90
91
92
93
package backup
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/GrantStreetGroup/go-exasol-client"
)
// This backs up priority groups
type priorityGroup struct {
name string
weight int
comment string
}
func BackupPriorityGroups(src *exasol.Conn, dst string) error {
log.Info("Backing up priority groups")
priorityGroups, err := getPriorityGroupsToBackup(src)
if err != nil {
return err
}
if len(priorityGroups) == 0 {
log.Warning("No priority groups found")
return nil
}
var sql string
for _, priorityGroup := range priorityGroups {
sql += createPriorityGroup(priorityGroup)
}
os.MkdirAll(dst, os.ModePerm)
file := filepath.Join(dst, "priority_groups.sql")
err = ioutil.WriteFile(file, []byte(sql), 0644)
if err != nil {
return fmt.Errorf("Unable to backup priority groups: %s", err)
}
log.Info("Done backing up priority groups")
return nil
}
func getPriorityGroupsToBackup(conn *exasol.Conn) ([]*priorityGroup, error) {
sql := `
SELECT priority_group_name,
priority_group_weight,
priority_group_comment
FROM exa_priority_groups
ORDER BY priority_group_name
`
res, err := conn.FetchSlice(sql)
if err != nil {
return nil, fmt.Errorf("Unable to get priority groups to backup: %s", err)
}
priorityGroups := []*priorityGroup{}
for _, row := range res {
p := &priorityGroup{
name: row[0].(string),
weight: int(row[1].(float64)),
}
if row[2] != nil {
p.comment = row[2].(string)
}
priorityGroups = append(priorityGroups, p)
}
return priorityGroups, nil
}
func createPriorityGroup(p *priorityGroup) string {
log.Infof("Backing up priority group %s", p.name)
sql := ""
if p.name == "MEDIUM" {
sql = fmt.Sprintf("ALTER PRIORITY GROUP [%s] SET WEIGHT = %d;\n", p.name, p.weight)
} else {
sql = fmt.Sprintf(
"DROP PRIORITY GROUP [%s];\n"+
"CREATE PRIORITY GROUP [%s] WITH WEIGHT = %d;\n",
p.name, p.name, p.weight,
)
}
if p.comment != "" {
sql += fmt.Sprintf(
"COMMENT ON PRIORITY GROUP [%s] IS '%s';\n",
p.name, qStr(p.comment),
)
}
return sql
}