-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsql.go
executable file
·634 lines (556 loc) · 17.2 KB
/
sql.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package main
import (
"fmt"
"strings"
"github.com/go-pg/pg/v10"
)
// GreenplumOrPostgres ...
var GreenplumOrPostgres = "greenplum"
// DBTables store house of all tables
type DBTables struct {
Schema string
Table string
}
// DBColumns store house of all the columns
type DBColumns struct {
Column string
Datatype string
Sequence string
}
// DBConstraints store house of all constraints
type DBConstraints struct {
Tablename string
Constraintname string
Constraintkey string
}
// DBConstraintsByTable store house for constraints by table
type DBConstraintsByTable struct {
Tablename string
Constraintname string
Constraintcol string
Constrainttype string
}
// DBConstraintsByDataType store house for constraints by datatype
type DBConstraintsByDataType struct {
Colname string
Dtype string
}
// DBIndex store house for indexes
type DBIndex struct {
Tablename string
Indexdef string
}
// DBViolationRow capture violating row
type DBViolationRow struct {
Row string
}
// EnumDataType store house for emun datatype data
type EnumDataType struct {
EnumSchema string
EnumName string
EnumValue string
}
// Connection check and database version
func dbVersion() {
Debug("Checking the version of the database")
// db connection
db := ConnectDB()
defer db.Close()
// Fire the database version query and check if there is no
// database error
var version string
query := "SELECT version()"
_, err := db.QueryOne(pg.Scan(&version), query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when connecting to the database, err: %v", err)
}
Infof("Version of the database: %s", version)
postgresOrGreenplum()
}
// Is this postgres or greenplum database
func postgresOrGreenplum() {
Debug("Checking if this a greenplum or postgres DB")
// Only greenplum has this table
query := "select * from gp_segment_configuration"
_, err := ExecuteDB(query)
if err != nil {
GreenplumOrPostgres = "postgres"
}
Infof("The flavour of postgres is: %s", GreenplumOrPostgres)
}
// Postgres get all the tables
func allTablesPostgres(whereClause string) []DBTables {
Debug("Extracting the tables info from the postgres database")
var result []DBTables
// db connection
db := ConnectDB()
defer db.Close()
// The query
query := `
SELECT n.nspname AS SCHEMA,
c.relname AS table
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n
ON n.oid = c.relnamespace
WHERE c.relkind IN ( 'r', '' )
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND n.nspname !~ '^gp_toolkit'
AND c.relkind = 'r'
%s
UNION
SELECT foreign_table_schema as SCHEMA,
foreign_table_name as table
FROM information_schema.foreign_tables
ORDER BY 1
`
// add where clause
query = fmt.Sprintf(query, whereClause)
// execute the query
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when getting all the tables from postgres database, err: %v", err)
}
return result
}
// Greenplum / HDB get all the tables
func allTablesGPDB(whereClause string) []DBTables {
Debug("Extracting the tables info from greenplum database")
var result []DBTables
// db connection
db := ConnectDB()
defer db.Close()
// The query
query := `
SELECT n.nspname AS SCHEMA,
c.relname AS TABLE
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n
ON n.oid = c.relnamespace
WHERE c.relkind IN ( 'r', '' )
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND n.nspname <> 'gp_toolkit'
AND c.relkind = 'r'
AND c.relstorage IN ('a','h','c')
%s
ORDER BY 1
`
// add where clause
query = fmt.Sprintf(query, whereClause)
// execute the query
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when getting all the tables from GPDB, err: %v", err)
}
return result
}
// Extract Column & DataType Postgres
func columnExtractorPostgres(schema, table string) []DBColumns {
tableName := fmt.Sprintf("%s.\"%s\"", schema, table)
Debugf("Extracting the column information from postgres database for table: %s", tableName)
var result []DBColumns
// db connection
db := ConnectDB()
defer db.Close()
// The query
query := `
SELECT a.attname AS COLUMN,
pg_catalog.Format_type(a.atttypid, a.atttypmod) AS datatype,
COALESCE(
(
SELECT substring( pg_catalog.Pg_get_expr(d.adbin, d.adrelid) for 128 )
FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid
AND d.adnum = a.attnum
AND a.atthasdef ), '' ) AS sequence
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = '%s' :: regclass
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum
`
// add table information and execute the query
query = fmt.Sprintf(query, tableName)
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when getting all the columns from Postgres, err: %v", err)
}
return result
}
// Extract Column & DataType GPDB
func columnExtractorGPDB(schema, table string) []DBColumns {
tableName := fmt.Sprintf("%s.\"%s\"", schema, table)
Debugf("Extracting the column information from postgres database for table: %s", tableName)
var result []DBColumns
// db connection
db := ConnectDB()
defer db.Close()
// The query
query := `
SELECT a.attname AS COLUMN,
pg_catalog.Format_type(a.atttypid, a.atttypmod) AS datatype,
COALESCE(
(
SELECT substring( pg_catalog.Pg_get_expr(d.adbin, d.adrelid) for 128 )
FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid
AND d.adnum = a.attnum
AND a.atthasdef ), '' ) AS sequence
FROM pg_catalog.pg_attribute a
LEFT OUTER JOIN pg_catalog.pg_attribute_encoding e
ON e.attrelid = a.attrelid
AND e.attnum = a.attnum
WHERE a.attrelid = '%s' :: regclass
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum
`
// add table information and execute the query
query = fmt.Sprintf(query, tableName)
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when getting all the columns from GPDB, err: %v", err)
}
return result
}
// GetPGConstraintDDL saves all the DDL of the constraint ( like PK(p), FK(f), CK(c), UK(u) )
func GetPGConstraintDDL(conntype string) []DBConstraints {
Debugf("Extracting the DDL of the %s constraints", conntype)
var result []DBConstraints
query := `
SELECT '"'
|| n.nspname
|| '"."'
|| c.relname
|| '"' tablename,
con.conname constraintname,
pg_catalog.Pg_get_constraintdef(con.oid, true) constraintKey
FROM pg_catalog.pg_class c,
pg_catalog.pg_constraint con,
pg_catalog.pg_namespace n
WHERE conrelid = c.oid
AND n.oid = c.relnamespace
AND contype = '%s'
ORDER BY tablename
`
// db connection
db := ConnectDB()
defer db.Close()
// add table information and execute the query
query = fmt.Sprintf(query, conntype)
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when getting all the constraints from database, err: %s", err)
}
return result
}
// GetPGIndexDDL gets all the Unique index from the database
func GetPGIndexDDL() []DBIndex {
Debugf("Extracting the unique indexes")
var result []DBIndex
query := `
SELECT '"'
|| schemaname
|| '"."'
|| tablename
|| '"' tablename,
indexdef indexdef
FROM pg_indexes
WHERE schemaname IN (SELECT nspname
FROM pg_namespace
WHERE nspname NOT IN ( 'pg_catalog', 'information_schema'
,
'pg_aoseg',
'gp_toolkit',
'pg_toast', 'pg_bitmapindex' ))
AND indexdef LIKE 'CREATE UNIQUE%'
`
// db connection
db := ConnectDB()
defer db.Close()
// add table information and execute the query
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when getting all the constraints from database, err: %s", err)
}
return result
}
// GetConstraintsPertab provides drop statement for the table
func GetConstraintsPertab(tabname string) []DBConstraintsByTable {
Debugf("Extracting constraint info for table: %s", tabname)
var result []DBConstraintsByTable
query := `
SELECT *
FROM (SELECT n.nspname
|| '.'
|| c.relname tablename,
con.conname constraintname,
pg_catalog.Pg_get_constraintdef(con.oid, TRUE) constraintcol,
'constraint' constrainttype
FROM pg_catalog.pg_class c,
pg_catalog.pg_constraint con,
pg_namespace n
WHERE c.oid = '%[1]s' :: regclass
AND conrelid = c.oid
AND n.oid = c.relnamespace
AND contype IN ( 'u', 'f', 'c', 'p' )
UNION
SELECT schemaname
|| '.'
|| tablename tablename,
'"'
|| schemaname
|| '"."'
|| indexname
|| '"' conname,
indexdef concol,
'index' contype
FROM pg_indexes
WHERE schemaname IN (SELECT nspname
FROM pg_namespace
WHERE nspname NOT IN (
'pg_catalog', 'information_schema'
,
'pg_aoseg',
'gp_toolkit',
'pg_toast', 'pg_bitmapindex' ))
AND indexdef LIKE 'CREATE UNIQUE%[2]s'
AND '"'
|| schemaname
|| '"'
|| '."'
|| tablename
|| '"' = '%[1]s') a
ORDER BY constrainttype
`
// db connection
db := ConnectDB()
defer db.Close()
// add table information and execute the query
query = fmt.Sprintf(query, tabname, "%")
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when getting constraints for table %s from database, err: %s",
tabname, err)
}
return result
}
// Get the datatype of the column
func getDatatype(tab string, columns []string) []DBConstraintsByDataType {
Debugf("Extracting constraint column data type info for table: %s", tab)
var result []DBConstraintsByDataType
whereClause := strings.Join(columns, "' or attname = '")
whereClause = strings.Replace(whereClause, "attname = ' ", "attname = '", -1)
query := `
SELECT attname colname,
pg_catalog.Format_type(atttypid, atttypmod) dtype
FROM pg_attribute
WHERE attname = '%s'
AND attrelid = '%s' :: regclass
`
// db connection
db := ConnectDB()
defer db.Close()
// add table information and execute the query
query = fmt.Sprintf(query, whereClause, tab)
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Encountered error when getting data type for "+
"building constrints for table %s from database, err: %v", tab, err)
}
return result
}
// Primary key violation check
func getTotalPKViolator(tab, cols string) int {
var total int
query := fmt.Sprintf(`SELECT COUNT(*) FROM ( %s ) a`, getPKViolator(tab, cols))
// db connection
db := ConnectDB()
defer db.Close()
_, err := db.Query(pg.Scan(&total), query)
if err != nil {
addNewLine()
Debugf("query: %s", query)
Errorf("Error when executing the query to extract pk violators: %v", err)
}
return total
}
// Total Primary Key violator
func getPKViolator(tab, cols string) string {
return fmt.Sprintf(`SELECT %[1]s FROM %[2]s GROUP BY %[1]s HAVING COUNT(*) > 1`, cols, tab)
}
// GetPKViolators gets the list of the PK violators
func GetPKViolators(tab, cols string) []DBViolationRow {
Debugf("Extracting the unique violations for table %s and column %s", tab, cols)
var result []DBViolationRow
// db connection
db := ConnectDB()
defer db.Close()
query := strings.Replace(getPKViolator(tab, cols), "SELECT "+cols, "SELECT "+cols+" AS row", -1)
_, err := db.Query(&result, query)
if err != nil {
addNewLine()
Debugf("query: %s", query)
Errorf("Error when executing the query to extract pk violators for table %s: %v", tab, err)
}
return result
}
// UpdatePKKey fixes PK Violators
func UpdatePKKey(tab, col, whichrow, newdata string) string {
query := `
UPDATE %[1]s
SET %[2]s = '%[3]s'
WHERE ctid =
(
SELECT ctid
FROM %[1]s
WHERE %[2]s = '%[4]s' limit 1 )
`
query = fmt.Sprintf(query, tab, col, newdata, whichrow)
_, err := ExecuteDB(query)
if err != nil {
addNewLine()
Debugf("query: %s", query)
Errorf("Error when updating the primary key for table %s, err: %v", tab, err)
}
return ""
}
// Get the foreign violations keys
func getFKViolator(key ForeignKey) string {
query := `
SELECT %[1]s
FROM %[2]s
WHERE %[1]s NOT IN
(
SELECT %[3]s
FROM %[4]s )
`
return fmt.Sprintf(query, key.Column, key.Table, key.Refcolumn, key.Reftable)
}
// GetTotalFKViolators gets total FK violators
func GetTotalFKViolators(key ForeignKey) int {
var total int
query := `SELECT COUNT(*) FROM (%s) a`
query = fmt.Sprintf(query, getFKViolator(key))
// db connection
db := ConnectDB()
defer db.Close()
_, err := db.Query(pg.Scan(&total), query)
if err != nil {
addNewLine()
Debugf("Query: %s", query)
Errorf("Error when executing the query to total rows of foreign keys for table %s: %s", key.Table, err)
}
return total
}
// TotalRows gets total rows of the table
func TotalRows(tab string) int {
var total int
query := fmt.Sprintf(`SELECT COUNT(*) FROM %s`, tab)
// db connection
db := ConnectDB()
defer db.Close()
_, err := db.Query(pg.Scan(&total), query)
if err != nil {
addNewLine()
Debugf("query: %s", query)
Errorf("Error when executing the query to total rows: %v", err)
}
return total
}
// GetFKViolators gets the list of the FK violators
func GetFKViolators(key ForeignKey) []DBViolationRow {
Debugf("Extracting the foreign violations for table %s and column %s", key.Table, key.Reftable)
var result []DBViolationRow
// db connection
db := ConnectDB()
defer db.Close()
query := strings.Replace(getFKViolator(key), "SELECT "+key.Column, "SELECT "+key.Column+" AS row", -1)
_, err := db.Query(&result, query)
if err != nil {
addNewLine()
Debugf("query: %s", query)
Errorf("Error when executing the query to extract fk violators for table %s: %v", key.Table, err)
}
return result
}
// UpdateFKeys update FK violators with rows from the referenced table
func UpdateFKeys(key ForeignKey, totalRows int, whichRow string) {
query := `
UPDATE %[1]s
SET %[2]s =
(
SELECT %[3]s
FROM %[4]s offset floor(random()*%[5]d) limit 1)
WHERE %[2]s = '%[6]s'
`
query = fmt.Sprintf(query, key.Table, key.Column, key.Refcolumn, key.Reftable, totalRows, whichRow)
_, err := ExecuteDB(query)
if err != nil {
addNewLine()
Debugf("query: %s", query)
Errorf("Error when updating the foreign key for table %s, err: %v", key.Table, err)
}
}
// Delete the violating key
func deleteViolatingConstraintKeys(tab string, column string) error {
Debugf("Deleting the rows of the table that violate the constraints: %s(%s)", tab, column)
query := `
DELETE
FROM %[1]s
WHERE (
%[2]s) IN
(
SELECT %[2]s
FROM %[1]s
GROUP BY %[2]s
HAVING count(*) > 1);
`
query = fmt.Sprintf(query, tab, column)
_, err := ExecuteDB(query)
if err != nil {
Debugf("query: %s", query)
return err
}
return nil
}
// Check & provide values if the datatype is ENUM
func checkEnumDatatype(dt string) []EnumDataType {
Debugf("Checking if the datatype is enum")
var result []EnumDataType
// db connection
db := ConnectDB()
defer db.Close()
// query
query := `
SELECT n.nspname AS enum_schema,
t.typname AS enum_name,
e.enumlabel AS enum_value
FROM pg_type t
JOIN pg_enum e
ON t.oid = e.enumtypid
JOIN pg_catalog.pg_namespace n
ON n.oid = t.typnamespace
WHERE t.typname = '%s'
`
query = fmt.Sprintf(query, dt)
// Execute and provide the result
_, err := db.Query(&result, query)
if err != nil {
Debugf("query: %s", query)
Fatalf("Error when executing the query to check if the data type is enum:%v", err)
}
return result
}