-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
332 lines (297 loc) · 10.5 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"text/template"
log "github.com/sirupsen/logrus"
)
var (
version = "dev"
date = "unknown"
whParamCsvFileName = "warehouse_parameters.csv"
wsCsvFileName = "warehouses.csv"
)
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
}
func printSeparator() {
log.Info("------------------------------------------------------------")
}
// downloadData downloads data from source Snowflake account
// Access to SNOWFLAKE.ACCOUNT_USAGE is required
// Permission to create a stage in the source account is required
func downloadData(sfClient *SnowflakeDBClient, args *Args) {
printSeparator()
log.Printf("Downloading data from source account %s", args.SrcAccount)
// template parsed sql script
downloadSqlScript := &bytes.Buffer{}
// default sql script
downloadSqlStr := downloadSqlScriptTemplate
// use custom sql file
if contains(args.CustomSql, "download_data.sql") {
log.Info("Using custom download sql script")
fb, err := os.ReadFile(path.Join(args.Out, "download_data.sql"))
if err != nil {
log.Errorf("Error opening custom download sql script falling back to default: %s", err)
} else {
downloadSqlStr = string(fb)
log.Debugf("Custom download sql file: %s", downloadSqlStr)
}
}
err := parseTemplate(downloadSqlStr, downloadSqlScript, args)
if err != nil {
log.Errorf("Error parsing download sql script: %s", err)
}
if args.SaveSql {
savePath := filepath.Join(args.Out, "download_data.sql")
log.Infof("Saving sql script to file %s", savePath)
err := os.WriteFile(savePath, downloadSqlScript.Bytes(), 0644)
if err != nil {
log.Infof("Error saving sql script to file %s: %s", savePath, err)
}
return
}
queries := strings.Split(downloadSqlScript.String(), "\n")
for _, query := range queries {
if query == "" {
continue
}
log.Infof("Running query: %s", query)
rows, err := sfClient.Query(query)
if err != nil {
log.Errorf(fmt.Sprintf("Error running query: %s; Error: %s", query, err))
} else {
log.Infof("%d rows affected", rows.RowAffected())
rows.Close()
}
}
}
// getWarehouseParameters gets warehouse parameters from source Snowflake account and save to csv file
// The format for the csv file is as follows:
// warehouse_name,parameter_name,parameter_values...
func getWarehouseParameters(sfClient *SnowflakeDBClient, args *Args) {
printSeparator()
log.Info("Getting warehouse parameters from source account")
if args.SaveSql {
sPath := filepath.Join(args.Out, whParamCsvFileName)
log.Infof(
"Please export the result of 'show warehouses' from the source account and save as a CSV file %s manually",
sPath,
)
return
}
log.Infof("Running query SHOW WAREHOUSES")
rows, err := sfClient.Query("SHOW WAREHOUSES")
if err != nil {
log.Infof("Error running SHOW WAREHOUSES: %s", err)
}
defer rows.Close()
wsMap := rows.ResultToMap(true)
saveToCsv(filepath.Join(args.Out, wsCsvFileName), wsMap)
warehouseParameters := sfClient.GetAllWarehouseParameters()
saveToCsv(filepath.Join(args.Out, whParamCsvFileName), warehouseParameters)
}
// uploadData uploads data to target Snowflake account
func uploadData(sfClient *SnowflakeDBClient, args *Args) {
printSeparator()
log.Infof("Uploading data to target account %s", args.TgtAccount)
// template parsed sql script
uploadSqlScript := &bytes.Buffer{}
// default sql script
uploadSqlStr := uploadSqlScriptTemplate
// use custom sql file
if contains(args.CustomSql, "upload_data.sql") {
log.Info("Using custom upload sql script")
fb, err := os.ReadFile(path.Join(args.Out, "upload_data.sql"))
if err != nil {
log.Errorf("Error opening custom upload sql script falling back to default: %s", err)
} else {
uploadSqlStr = string(fb)
log.Debugf("Custom upload sql: %s", uploadSqlStr)
}
}
err := parseTemplate(uploadSqlStr, uploadSqlScript, args)
if err != nil {
log.Error(err)
}
if args.SaveSql {
savePath := filepath.Join(args.Out, "upload_data.sql")
log.Infof("Saving sql script to file %s", savePath)
err = os.WriteFile(savePath, uploadSqlScript.Bytes(), 0644)
if err != nil {
log.Infof("Error saving sql script to file %s: %s", savePath, err)
}
return
}
_ = sfClient.UseSchema(args.TgtSchema)
queries := strings.Split(uploadSqlScript.String(), "\n")
for _, query := range queries {
if query == "" {
continue
}
log.Infof("Running query: %s", query)
rows, err := sfClient.Query(query)
if err != nil {
log.Infof("Error running query: %s; Error: %s", query, err)
} else {
log.Infof("%d rows affected", rows.RowAffected())
rows.Close()
}
}
}
func createResources(sfClient *SnowflakeDBClient, args *Args) {
printSeparator()
log.Infof("Creating resources in target account %s", args.TgtAccount)
if args.TgtNewRole == "" {
args.TgtNewRole = fmt.Sprintf("UNRAVEL_%s", generateStr(5, false, true, true, false))
}
if args.TgtNewUser == "" {
args.TgtNewUser = fmt.Sprintf("%s_USER_%s", args.TgtNewRole, generateStr(5, false, true, false, false))
}
if args.TgtNewUserPass == "" {
args.TgtNewUserPass = generateStr(8, true, true, true, true)
}
if args.SaveSql {
sPath := filepath.Join(args.Out, "create_resources.sql")
queries := []string{
fmt.Sprintf("CREATE ROLE IF NOT EXISTS %s", args.TgtNewRole),
fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s.%s", args.TgtDatabase, args.TgtSchema),
fmt.Sprintf("GRANT USAGE ON DATABASE %s TO ROLE %s", args.TgtDatabase, args.TgtNewRole),
fmt.Sprintf("GRANT USAGE, CREATE FILE FORMAT, CREATE STAGE ON SCHEMA %s TO ROLE %s", args.TgtSchema, args.TgtNewRole),
fmt.Sprintf("GRANT USAGE ON WAREHOUSE %s TO ROLE %s", args.TgtWarehouse, args.TgtNewRole),
fmt.Sprintf("GRANT INSERT, SELECT, UPDATE, TRUNCATE ON ALL TABLES IN SCHEMA %s TO ROLE %s", args.TgtSchema, args.TgtNewRole),
fmt.Sprintf(`CREATE USER IF NOT EXISTS %s password="%s"`, args.TgtNewUser, args.TgtNewUserPass),
fmt.Sprintf("GRANT ROLE %s TO USER %s;", args.TgtNewRole, args.TgtNewUser),
}
log.Infof("Saving sql script to file %s", sPath)
err := os.WriteFile(sPath, []byte(strings.Join(queries, ";\n")), 0644)
if err != nil {
log.Errorf("Error saving sql script to file %s: %s", sPath, err)
}
return
}
log.Infof("Creating role %s", args.TgtNewRole)
_ = sfClient.CreateRole(args.TgtNewRole)
log.Infof("Creating schema %s and table in database %s", args.TgtSchema, args.TgtDatabase)
_ = sfClient.CreateSchemaTable(args.TgtDatabase, args.TgtSchema)
log.Infof("Granting Database USAGE permission to role %s", args.TgtNewRole)
_ = sfClient.GrantDatabasePermissions(args.TgtDatabase, args.TgtNewRole, "USAGE")
log.Infof("Granting Schema USAGE, CREATE FILE FORMAT, CREATE STAGE permissions to role %s", args.TgtNewRole)
_ = sfClient.GrantSchemaPermissions(args.TgtSchema, args.TgtNewRole, "USAGE", "CREATE FILE FORMAT", "CREATE STAGE")
log.Infof("Granting Warehouse USAGE permission to role %s", args.TgtNewRole)
_ = sfClient.GrantWarehousePermissions(args.TgtWarehouse, args.TgtNewRole, "USAGE")
log.Infof("Granting all tables read write on schema %s to role %s", args.TgtSchema, args.TgtNewRole)
_ = sfClient.GrantReadWriteAllTables(args.TgtSchema, args.TgtNewRole, "INSERT", "SELECT", "UPDATE", "TRUNCATE")
log.Infof("Creating user %s", args.TgtNewUser)
_ = sfClient.CreateUser(args.TgtNewUser, args.TgtNewUserPass)
log.Infof("Granting role %s to user %s", args.TgtNewRole, args.TgtNewUser)
_ = sfClient.GrantUserRole(args.TgtNewUser, args.TgtNewRole)
}
func printSummary(args *Args) {
if !contains(args.Actions, "create") || args.SaveSql {
return
}
if args.TgtNewUser != "" {
log.Infof("New user: %s", args.TgtNewUser)
log.Infof("New user password: %s", args.TgtNewUserPass)
}
if args.TgtNewRole != "" {
log.Infof("New role: %s", args.TgtNewRole)
}
}
func parseTemplate(templateStr string, buffer *bytes.Buffer, args *Args) (err error) {
err = template.Must(template.New("").Parse(templateStr)).Execute(buffer, args)
return err
}
// cleanUp removes all temporary files download by this tool
func cleanUp(args *Args) {
cleanUPCandidates := []string{
wsCsvFileName,
whParamCsvFileName,
"query_history.csv*",
"warehouse_load_history.csv*",
"warehouse_events_history.csv*",
"warehouse_metering_history.csv*",
"access_history.csv*",
"metering_history.csv*",
"metering_daily_history.csv*",
"tables.csv*",
}
count := 0
for _, f := range cleanUPCandidates {
fs, err := filepath.Glob(filepath.Join(args.Out, f))
if err != nil {
log.Infof("Error getting file list: %s", err)
}
for _, f := range fs {
log.Debugf("Removing file %s", f)
err := os.Remove(f)
if err != nil {
log.Errorf("Error removing file %s: %s", f, err)
} else {
log.Debugf("Removed file %s", f)
count++
}
}
}
log.Infof("Removed %d files", count)
}
func main() {
log.Infof("Snowflake Warehouse Migration Tool %s; built at %s", version, date)
args := getArgs()
var srcPrivateKeyPath, tgtPrivateKeyPath string
// Create source account Snowflake client
switch args.SrcLoginMethod {
case "keypair":
srcPrivateKeyPath = args.PrivateKeyPath
}
var srcClient, tgtClient *SnowflakeDBClient
var err, err1 error
if contains(args.Actions, "download") {
srcClient, err = NewSnowflakeClient(
args.SrcLoginMethod, args.SrcUser, args.SrcPassword, args.SrcAccount, args.SrcWarehouse, args.SrcDatabase,
args.SrcSchema, args.SrcRole, args.SrcPasscode, srcPrivateKeyPath, args.SrcPrivateLink, args.SrcOktaURL,
args.Debug,
)
}
// Create target account Snowflake client
switch args.TgtLoginMethod {
case "keypair":
tgtPrivateKeyPath = args.PrivateKeyPath
}
if contains(args.Actions, "create") || contains(args.Actions, "upload") {
tgtClient, err1 = NewSnowflakeClient(
args.TgtLoginMethod, args.TgtUser, args.TgtPassword, args.TgtAccount, args.TgtWarehouse, args.TgtDatabase,
"", args.TgtRole, args.TgtPasscode, tgtPrivateKeyPath, args.TgtPrivateLink, args.TgtOktaURL,
args.Debug,
)
}
if err != nil || err1 != nil {
if !args.SaveSql {
log.Fatalf("failed to create snowflake client, src account err: %s, target account err: %s", err, err1)
}
}
defer printSummary(args)
// Clean up temporary files after complete
if !args.DisableCleanup {
defer cleanUp(args)
}
if contains(args.Actions, "download") {
downloadData(srcClient, args)
getWarehouseParameters(srcClient, args)
}
if contains(args.Actions, "create") {
createResources(tgtClient, args)
}
if contains(args.Actions, "upload") {
uploadData(tgtClient, args)
}
}