-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvirtual_schemas.go
171 lines (150 loc) · 3.89 KB
/
virtual_schemas.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
package backup
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/GrantStreetGroup/go-exasol-client"
)
// This backs up schemas and virtual schemas.
type virtual_schema struct {
name string
comment string
isVirtual bool
adapter string
vSchemaProps []*vSchemaProp
}
type vSchemaProp struct {
name string
value string
}
func (s *virtual_schema) Schema() string { return s.name }
func (s *virtual_schema) Name() string { return "" }
func BackupVirtualSchemas(src *exasol.Conn, dst string, crit Criteria, dropExtras bool) error {
log.Infof("Backing up schemas")
schemas, dbObjs, err := getVirtualSchemasToBackup(src, crit)
if err != nil {
return err
}
if dropExtras {
removeExtraObjects("virtual_schemas", dbObjs, dst, crit)
}
if len(schemas) == 0 {
log.Warning("Object criteria did not match any virtual schemas")
return nil
}
err = addVirtualSchemaProps(src, schemas, crit)
if err != nil {
return err
}
dir := filepath.Join(dst, "schemas")
os.MkdirAll(dir, os.ModePerm)
for _, schema := range schemas {
err = createVirtualSchema(dir, schema)
if err != nil {
return err
}
}
log.Info("Done backing up virtual schemas")
return nil
}
func getVirtualSchemasToBackup(conn *exasol.Conn, crit Criteria) ([]*virtual_schema, []dbObj, error) {
adapterColumn := "adapter_script"
if capability.version >= 8 {
adapterColumn = `CONCAT(adapter_script_schema,'.',adapter_script_name)`
}
sql := fmt.Sprintf(`
SELECT s.schema_name AS s,
s.schema_name AS o,
s.schema_comment,
%s
FROM exa_schemas AS s
JOIN exa_all_virtual_schemas AS vs
ON s.schema_name = vs.schema_name
WHERE %s
ORDER BY local.s
`, adapterColumn, crit.getSQLCriteria(),
)
res, err := conn.FetchSlice(sql)
if err != nil {
return nil, nil, fmt.Errorf("Unable to get virtual schemas: %s", err)
}
schemas := []*virtual_schema{}
dbObjs := []dbObj{}
for _, row := range res {
s := &virtual_schema{
name: row[0].(string),
}
if row[2] != nil {
s.comment = row[2].(string)
}
if row[3] != nil {
s.adapter = row[3].(string)
}
schemas = append(schemas, s)
dbObjs = append(dbObjs, s)
}
return schemas, dbObjs, nil
}
func addVirtualSchemaProps(conn *exasol.Conn, schemas []*virtual_schema, crit Criteria) error {
sql := fmt.Sprintf(`
SELECT schema_name AS s,
schema_name AS o,
property_name,
property_value
FROM exa_dba_virtual_schema_properties
WHERE %s
ORDER BY schema_name, property_name
`, crit.getSQLCriteria(),
)
res, err := conn.FetchSlice(sql)
if err != nil {
return fmt.Errorf("Unable to get virtual schema properties: %s", err)
}
for _, row := range res {
schemaName := row[0].(string)
prop := &vSchemaProp{name: row[2].(string)}
if row[3] != nil {
prop.value = row[3].(string)
}
var schema *virtual_schema
for _, s := range schemas {
if s.name == schemaName {
schema = s
break
}
}
if schema == nil {
return fmt.Errorf("Cannot find schema %s for virtual property", schemaName)
}
schema.vSchemaProps = append(schema.vSchemaProps, prop)
}
return nil
}
func createVirtualSchema(dst string, s *virtual_schema) error {
log.Infof("Backing up virtual schema %s", s.name)
props := ""
if len(s.vSchemaProps) > 0 {
props = "\nWITH"
for _, p := range s.vSchemaProps {
props += fmt.Sprintf("\n %s = '%s'", p.name, qStr(p.value))
}
}
adapter := strings.Split(s.adapter, ".")
sql := fmt.Sprintf(
"CREATE VIRTUAL SCHEMA IF NOT EXISTS [%s]\nUSING [%s].[%s]%s;\n",
s.name, adapter[0], adapter[1], props,
)
if s.comment != "" {
sql += fmt.Sprintf("COMMENT ON SCHEMA [%s] IS '%s';\n", s.name, qStr(s.comment))
}
dir := filepath.Join(dst, s.name)
os.MkdirAll(dir, os.ModePerm)
file := filepath.Join(dir, "schema.sql")
err := ioutil.WriteFile(file, []byte(sql), 0644)
if err != nil {
return fmt.Errorf("Unable to backup virtual schema: %s", err)
}
return nil
}