-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.go
176 lines (159 loc) · 4.26 KB
/
views.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package backup
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sync"
"github.com/GrantStreetGroup/go-exasol-client"
)
type view struct {
schema string
name string
scope string
text string
}
func (v *view) Schema() string { return v.schema }
func (v *view) Name() string { return v.name }
func BackupViews(src *exasol.Conn, dst string, crit Criteria, maxRows int, dropExtras bool) error {
log.Info("Backing up views")
views, dbObjs, err := getViewsToBackup(src, crit)
if err != nil {
return err
}
if dropExtras {
removeExtraObjects("views", dbObjs, dst, crit)
}
if len(views) == 0 {
log.Warning("Object criteria did not match any views")
return nil
}
for _, v := range views {
dir := filepath.Join(dst, "schemas", v.schema, "views")
os.MkdirAll(dir, os.ModePerm)
err = backupView(dir, v)
if err != nil {
return err
}
shouldBackup, err := shouldBackupViewData(src, v, maxRows)
if err != nil {
return err
}
if shouldBackup {
log.Infof("Backing up view data for %s.%s", v.schema, v.name)
wg := &sync.WaitGroup{}
wg.Add(2)
data := make(chan []byte)
errors := make(chan error, 2)
go readViewData(src, v, data, errors, wg)
go writeViewData(dir, v, data, errors, wg)
wg.Wait()
select {
case err = <-errors:
return err
default:
}
}
}
log.Info("Done backing up views")
return nil
}
func getViewsToBackup(conn *exasol.Conn, crit Criteria) ([]*view, []dbObj, error) {
// Not that view and view-column comments can only be added
// directly in the context of a CREATE VIEW so as long as we
// pull out view_text we got them all.
sql := fmt.Sprintf(`
SELECT view_schema AS s,
view_name AS o,
scope_schema,
view_text
FROM exa_all_views
WHERE %s
ORDER BY local.s, local.o
`, crit.getSQLCriteria(),
)
res, err := conn.FetchSlice(sql)
if err != nil {
return nil, nil, fmt.Errorf("Unable to get views: %s", err)
}
views := []*view{}
dbObjs := []dbObj{}
for _, row := range res {
v := &view{
schema: row[0].(string),
name: row[1].(string),
text: row[3].(string),
}
if row[2] == nil {
v.scope = row[0].(string)
} else {
v.scope = row[2].(string)
}
views = append(views, v)
dbObjs = append(dbObjs, v)
}
return views, dbObjs, nil
}
func backupView(dir string, v *view) error {
log.Infof("Backing up view %s.%s", v.schema, v.name)
// We have to swap out the name too because if the view got renamed
// the v.text still references the original name.
r := regexp.MustCompile(`^(?is).*?CREATE[^V]+?VIEW\s+(("[^"]+"|[\w_-]+)\.)?("[^"]+"|[\w_-]+)`)
replacement := fmt.Sprintf(`CREATE OR REPLACE FORCE VIEW "%s"."%s"`, v.schema, v.name)
createView := r.ReplaceAllString(v.text, replacement)
sql := fmt.Sprintf("OPEN SCHEMA [%s];\n%s;\n", v.scope, createView)
file := filepath.Join(dir, v.name+".sql")
err := ioutil.WriteFile(file, []byte(sql), 0644)
if err != nil {
return fmt.Errorf("Unable to backup view %s: %s", v.name, err)
}
return nil
}
func shouldBackupViewData(conn *exasol.Conn, v *view, maxRows int) (bool, error) {
if maxRows == 0 {
return false, nil
}
sql := fmt.Sprintf(`SELECT COUNT(*) FROM [%s].[%s]`, v.schema, v.name)
res, err := conn.FetchSlice(sql)
if err != nil {
return false, fmt.Errorf("Unable to number of view rows: %s", err)
}
numRows := int(res[0][0].(float64))
return numRows > 0 && numRows <= maxRows, nil
}
func readViewData(conn *exasol.Conn, v *view, data chan<- []byte, errors chan<- error, wg *sync.WaitGroup) {
defer func() {
close(data)
wg.Done()
}()
exportSQL := fmt.Sprintf(
"EXPORT (SELECT * FROM [%s].[%s]) INTO CSV AT '%%s' FILE 'data.csv'",
v.schema, v.name,
)
res := conn.StreamQuery(exportSQL)
if res.Error != nil {
errors <- fmt.Errorf("Unable to read view %s: %s", v.name, res.Error)
return
}
for d := range res.Data {
data <- d
}
}
func writeViewData(dst string, v *view, data <-chan []byte, errors chan<- error, wg *sync.WaitGroup) {
defer func() { wg.Done() }()
fp := filepath.Join(dst, v.name+".csv")
f, err := os.Create(fp)
if err != nil {
errors <- fmt.Errorf("Unable to create view file %s: %s", fp, err)
return
}
for d := range data {
_, err = f.Write(d)
if err != nil {
errors <- fmt.Errorf("Unable to write view file %s: %s", fp, err)
return
}
}
f.Close()
}