-
Notifications
You must be signed in to change notification settings - Fork 4
/
sqlloop.go
435 lines (337 loc) · 11.9 KB
/
sqlloop.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package main
import (
"context"
"fmt"
"flag"
"os"
"time"
"github.com/eiannone/keyboard"
"encoding/json"
)
var (
success_count int64 = 0
errors_count int64 = 0
err_start_time time.Time
total_start_time time.Time
total_downtime time.Duration = 0
)
type Queries struct {
Queries []Query
}
type Query struct {
DDL_SQL string
Comment string
}
type SessionParameters struct {
SessionParameters []SessionParameter
}
type SessionParameter struct {
Parameter string
Value string
}
func ExecCreate(pgManager *PGManager) {
flag.Parse()
// Open our create.json script with DDLs to run prior
// to loop on excuting script.sql queries
create_ddl_file, err := os.Open(createfilename.value)
// if we os.Open returns an error then handle it
if err != nil {
exit1("Could not open DDL script:\n" , err)
}
fmt.Println("Executing DDL Script :")
// defer the closing of our jsonFile so that we can parse it later on
defer create_ddl_file.Close()
// get the file size
create_ddl_file_stat, err := create_ddl_file.Stat()
if err != nil {
fmt.Println("Error getting ddl script size information:", err)
exit1("Error getting file size information:\n" , err)
}
// read the file as a byte slice (array)
byteValue := make([]byte, create_ddl_file_stat.Size())
_, err = create_ddl_file.Read(byteValue)
if err != nil {
exit1("Error reading ddl script file:\n", err)
}
// we initialize our Queries array
var q Queries
// we unmarshal our byteArray which contains our
// jsonFile's content into 'Queries' which we defined above
json.Unmarshal(byteValue, &q)
// we iterate through every query within our Query array and
// print out the query DDL and Comment
for i := 0; i < len(q.Queries); i++ {
//DEBUG
//fmt.Println("DEBUG: SQL Query : " + q.Queries[i].DDL_SQL)
//fmt.Println("DEBUG: Comment : " + q.Queries[i].Comment)
//_, err := connectionInstance.Exec(context.Background(),q.Queries[i].DDL_SQL)
_, err := pgManager.conn.Exec(context.Background(),q.Queries[i].DDL_SQL)
if err != nil {
//connectionInstance.Close(context.Background())
message := "Something went wrong trying to execute the SQL script\n"
message += createfilename.value + " on the database\n"
exit1(message,err)
}
fmt.Printf(" %q\n",q.Queries[i].Comment)
}
fmt.Print(string(colorGreen))
fmt.Printf(" Script %q successfully executed !\n",createfilename.value)
fmt.Print(string(colorReset))
fmt.Println()
}
func SetSessionParameters(pgManager *PGManager) {
//user requested to throw special SET TRANSACTIONs parameters
//before looping on the queries of the SQL Script
//we create a SQL text file to be sent prior executing the main loop
flag.Parse()
// Open the session_parameters JSON file
gucs_parameters_file, err := os.Open(sessiongucsfilename.value)
// if we os.Open returns an error then handle it
if err != nil {
exit1("Could not open session parmeters GUCs file:\n", err)
}
// defer the closing of our jsonFile so that we can parse it later on
defer gucs_parameters_file.Close()
// get the file size
gucs_parameters_file_stat, err := gucs_parameters_file.Stat()
if err != nil {
exit1("Error getting GUCS parameters file size information:\n" , err)
}
// read the file as a byte slice (array)
byteValue := make([]byte, gucs_parameters_file_stat.Size())
_, err = gucs_parameters_file.Read(byteValue)
if err != nil {
exit1("Error reading GUCS parameters file:\n", err)
}
fmt.Println("The following Session Parameters are set:")
// we initialize our SessionParameters GUCS parameters q SessionParameters
var q SessionParameters
// we unmarshal our byteArray which contains our
// jsonFile's content into 'sessionparameters' which we defined above
json.Unmarshal(byteValue, &q)
// we iterate through every SessionParameter within our
// SessionParametersarray and print out the SessionParameter Type
// the name and the value
for i := 0; i < len(q.SessionParameters); i++ {
sessiongucs = sessiongucs + "SET " + q.SessionParameters[i].Parameter + " TO '" + q.SessionParameters[i].Value + "';\n"
fmt.Println(" ","SET " + q.SessionParameters[i].Parameter + " TO '" + q.SessionParameters[i].Value + "';")
}
//DEBUG
//fmt.Println("Session GUCS",sessiongucs)
_, err = pgManager.conn.Exec(context.Background(), sessiongucs)
if err != nil {
exit1("Error while trying to set session parameters as described in -session_parameters " + sessiongucsfilename.value+"\n",err)
} else {
fmt.Print(string(colorGreen))
fmt.Printf(" Session parameters applied to the PG session !\n")
fmt.Print(string(colorReset))
fmt.Println()
}
}
func do_sqlloop(pgManager *PGManager) {
// read script.sql
script_file, err := os.ReadFile(scriptfilename.value)
if err != nil {
exit1("Could not read script file:\n" , err)
}
statements := string(script_file)
//test script once to ensure there's no errors in it
bad_script := 0
// Exec executes SQL via the PostgreSQL simple query protocol. SQL may contain multiple queries. Execution is
// implicitly wrapped in a transaction unless a transaction is already in progress or SQL contains transaction control
// statements.
// see https://github.com/jackc/pgx/blob/master/pgconn/pgconn.go#L1047C1-L1049C15
_, err = pgManager.conn.Exec(context.Background(), statements)
if err != nil {
bad_script += 1
}
if bad_script > 0 {
message := "Execution of SQL script in "+scriptfilename.value+" returns errors"
message += "\nPlease correct the errors prior running pgSimload."
exit1(message,nil)
} else {
if sleep_time > 0 {
fmt.Printf("Now entering the main loop, executing script %q each %q\n",scriptfilename.value, sleep_time)
} else {
fmt.Printf("Now entering the main loop, executing script %q as fast as possible\n",scriptfilename.value)
}
if exec_loops !=0 || exec_time !=0 {
fmt.Printf("\nNumber of loops will be limited:\n")
if exec_loops != 0 {
fmt.Printf(" %d executions", exec_loops)
}
if exec_time != 0 {
if exec_loops !=0 {
fmt.Printf(" or\n %q maximum duration\n", exec_time)
fmt.Printf("Whichever happens first\n")
} else {
fmt.Printf(" %q maximum duration\n", exec_time)
}
} else {
fmt.Println()
}
}
}
//store Time when we started the loop
total_start_time = time.Now()
// MAIN loop on SQL content of script.sql
// This is to be able to stop the loop on <Esc> key
stopCh := make(chan bool)
go func() {
for {
_, key, err := keyboard.GetKey()
if err != nil {
//exit1("Error:\n",err)
}
if key == keyboard.KeyEsc {
stopCh <- true
}
}
}()
//if user has set a --time "duration" parameter we start a "timer"
//of that amount of time. This one will send "true" to stopCh once
//dead so it will break the loop
//basically, it's just to Sleep for exec_time duration..
go func() {
if exec_time != 0 {
time.Sleep(exec_time)
stopCh <- true
}
}()
loop:
for {
select {
case stop := <-stopCh:
if stop {
break loop
}
default:
_, err := pgManager.conn.Exec(context.Background(), statements)
if err != nil {
//the last execution of the script is in error
//if we were not in error until now,
//we set the current time as the start
//time of errors
if err_start_time.IsZero() {
err_start_time = time.Now()
}
errors_count += 1
//we may have been connected ? Let's ping the server
//if we don't have an answer, try to reconnect instead
//test connection
//err := pgManager.conn.Ping(context.Background())
err = pgManager.conn.Ping(context.Background())
if err != nil {
err := pgManager.PGReconnectWithTimeout(pgReconnectTimeout,err)
if err != nil {
exit1("\nUnable to reconnect to PostgreSQL:\n", err)
}
}
} else {
//the last execution of the script is OK
if ! err_start_time.IsZero() {
total_downtime += time.Since(err_start_time)
err_start_time = time.Time{}
} else {
success_count += 1
}
fmt.Print(string(colorGreen))
fmt.Printf(ClearLine);
fmt.Printf("\rScript executions succeeded : %10d ", success_count)
fmt.Print(string(colorReset))
if success_count == exec_loops {
break loop;
}
if sleep_time > 0 {
time.Sleep(sleep_time)
}
}
}
}
// end of the main SQL Loop, time to compute times, etc..
// and print the summary before exiting
fmt.Println(string(colorReset))
total_exec_time := time.Since(total_start_time)
var statements_per_sec float64
if total_exec_time < 1 {
statements_per_sec = float64(success_count)
} else {
statements_per_sec = float64(success_count) / total_exec_time.Seconds()
}
//clear current line : shows the previous "running counter"
//fmt.Printf(MoveCursorCol1);
//fmt.Printf(ClearLine);
// print a Summary
fmt.Println("=========================================================================")
fmt.Println("Summary")
fmt.Println("=========================================================================")
fmt.Print(string(colorGreen))
fmt.Printf("\rScript executions succeeded : %10d", success_count)
fmt.Printf(" (%.3f scripts/second)\n", statements_per_sec)
if errors_count > 0 {
fmt.Print(string(colorRed))
if total_downtime == 0 {
statements_per_sec = float64(errors_count)
} else {
statements_per_sec = float64(errors_count) / total_downtime.Seconds()
}
fmt.Printf("\rScript executions rollbacks : %10d", errors_count)
fmt.Printf(" (%.3f scripts/second)\n", statements_per_sec)
} else {
fmt.Print(string(colorGreen))
fmt.Printf("\rScript scripts rollbacked: none")
}
//print out total exec time in Milliseconds if < 10 min total time
//print out total exec time in Seconds otherwise
fmt.Print(string(colorGreen))
if total_exec_time.Truncate(time.Minute) < 10 {
fmt.Printf("\rTotal exec time : %10s\n", total_exec_time.Truncate(time.Millisecond).String())
} else {
fmt.Printf("\rTotal exec time : %10s\n", total_exec_time.Truncate(time.Second).String())
}
fmt.Print(string(colorReset))
if !(total_downtime==0) {
fmt.Print(string(colorRed))
fmt.Printf("\rTotal real downtime : %10s\n", total_downtime.Truncate(time.Millisecond).String())
fmt.Print(string(colorReset))
}
fmt.Println("=========================================================================")
}
func SQLLoop () {
// PG connex needed
// Create PG Manager instance
//DEBUG
//fmt.Println("DEBUG: New PG Manager instance")
pgManager, err := NewPGManager(configfilename.value)
if err != nil {
exit1("Failed to create PGManager:\n", err)
}
//DEBUG
//fmt.Println("DEBUG: Created a new PGManager")
// Initial connection
conn, err := pgManager.PGConnect()
if err != nil {
// we won't try to reconnect here since the loop
// did not started yet
exit1("Failed to connect to PostgreSQL:\n", err)
}
defer conn.Close(context.Background())
//fmt.Println("DEBUG: Connected to PG!")
// time to clear the screen to remove the licence
// etc...
// Clears the screen
fmt.Printf("\x1bc")
currentTime := time.Now()
fmt.Println("+ SQL-Loop at", currentTime.Format("2006.01.02 15:04:05"))
fmt.Println()
if sessiongucsfilename.set {
SetSessionParameters(pgManager)
}
if createfilename.set {
//user requested to execure a SQL DML/DDL script
//before looping on the queries of the SQL script
//so we do exec it
ExecCreate(pgManager)
}
do_sqlloop(pgManager)
}