Skip to content

Commit 9470b25

Browse files
code cleanup
+ Cleanup all the dead new lines + Better stdout when verbose and debug mode is one + Reduce code duplication
1 parent d7ff280 commit 9470b25

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

constraintsRestore.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ func FixConstraints() {
2323
var constr = []string{"PRIMARY", "UNIQUE", "FOREIGN"}
2424
for _, v := range constr {
2525
totalViolations := len(savedConstraints[v])
26-
Infof("Found %v violation of %s KEYS, attempting to fix them", totalViolations, v)
27-
bar := StartProgressBar(fmt.Sprintf("Fixing %s KEYS violation", v), totalViolations)
26+
k := strings.ToLower(v)
27+
Infof("Found %v violation of %s keys, if found any attempting to fix them", totalViolations, k)
28+
bar := StartProgressBar(fmt.Sprintf("Fixing %s keys violation", k), totalViolations)
2829
for _, con := range savedConstraints[v] {
2930
switch {
3031
case v == "PRIMARY":
@@ -38,7 +39,6 @@ func FixConstraints() {
3839
}
3940
bar.Add(1)
4041
}
41-
fmt.Println()
4242
}
4343

4444
// Recreate constraints
@@ -210,7 +210,6 @@ func recreateAllConstraints() {
210210
}
211211
bar.Add(1)
212212
}
213-
fmt.Println()
214213
}
215214
}
216215

custom.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,5 @@ func (c *Skeleton) LoadDataByConfiguration() {
172172
CopyData(tab, col, data, db)
173173
bar.Add(1)
174174
}
175-
fmt.Println()
176175
}
177176
}

helpers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func StartProgressBar(text string, max int) *progressbar.ProgressBar {
7676
}
7777

7878
return progressbar.NewOptions(max,
79+
progressbar.OptionOnCompletion(func() {
80+
fmt.Println()
81+
}),
7982
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
8083
progressbar.OptionEnableColorCodes(true),
8184
progressbar.OptionSetBytes(10000),

sql.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ func getTotalPKViolator(tab, cols string) int {
412412

413413
_, err := db.Query(pg.Scan(&total), query)
414414
if err != nil {
415-
fmt.Println()
416415
Debugf("query: %s", query)
417416
Fatalf("Error when executing the query to extract pk violators: %v", err)
418417
}
@@ -437,7 +436,6 @@ func GetPKViolators(tab, cols string) []DBViolationRow {
437436
query := strings.Replace(getPKViolator(tab, cols), "SELECT "+cols, "SELECT "+cols+" AS row", -1)
438437
_, err := db.Query(&result, query)
439438
if err != nil {
440-
fmt.Println()
441439
Debugf("query: %s", query)
442440
Fatalf("Error when executing the query to extract pk violators for table %s: %v", tab, err)
443441
}
@@ -491,7 +489,6 @@ func GetTotalFKViolators(key ForeignKey) int {
491489

492490
_, err := db.Query(pg.Scan(&total), query)
493491
if err != nil {
494-
fmt.Println()
495492
Debugf("Query: %s", query)
496493
Fatalf("Error when executing the query to total rows of foreign keys for table %s: %v", key.Table, err)
497494
}
@@ -530,7 +527,6 @@ func GetFKViolators(key ForeignKey) []DBViolationRow {
530527
query := strings.Replace(getFKViolators(key), "SELECT "+key.Column, "SELECT "+key.Column+" AS row", -1)
531528
_, err := db.Query(&result, query)
532529
if err != nil {
533-
fmt.Println()
534530
Debugf("query: %s", query)
535531
Fatalf("Error when executing the query to extract fk violators for table %s: %v", key.Table, err)
536532
}
@@ -605,7 +601,6 @@ WHERE t.typname = '%s'
605601
// Execute and provide the result
606602
_, err := db.Query(&result, query)
607603
if err != nil {
608-
fmt.Println()
609604
Debugf("query: %s", query)
610605
Fatalf("Error when executing the query to check if the data type is enum:%v", err)
611606
}

tables.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ func CreateFakeTables() {
1616
createTable(i)
1717
bar.Add(1)
1818
}
19-
fmt.Println()
2019

2120
Infof("Completed creating %d fake tables on the database: %s", cmdOptions.Tab.TotalTables, cmdOptions.Database)
2221
}

worker.go

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type TableCollection struct {
1414
var (
1515
skippedTab []string
1616
delimiter = "$"
17+
oneColumnTable []string
18+
progressBarMsg = "Mocking Table %s"
1719
)
1820

1921
func MockTable(tables []DBTables) {
@@ -56,7 +58,7 @@ func tableMocker(tables []DBTables) {
5658

5759
// Extract the column and its datatypes of the table
5860
func columnExtractor(tables []DBTables) []TableCollection {
59-
Info("Extracting the columns and datatype information")
61+
Info("Extracting the columns and data type information")
6062
var columns []DBColumns
6163
var collection []TableCollection
6264

@@ -71,11 +73,11 @@ func columnExtractor(tables []DBTables) []TableCollection {
7173
columns = columnExtractorGPDB(fmt.Sprintf("\"%s\"", t.Schema), t.Table)
7274
}
7375

74-
// There are instance where the table can have one column and datatype serial
76+
// There are instance where the table can have one column and data type serial
77+
// then lets save them for later loading via a different method
7578
// take a look at the issue: https://github.com/pivotal-gss/mock-data/issues/29
76-
// lets fix this
7779
if len(columns) == 1 {
78-
checkAndAddDataIfItsASerialDatatype(t, columns)
80+
checkIfOneColumnIsASerialDatatype(t, columns)
7981
}
8082

8183
// Loops through the columns and make a collection of tables
@@ -92,7 +94,6 @@ func columnExtractor(tables []DBTables) []TableCollection {
9294
}
9395
bar.Add(1)
9496
}
95-
fmt.Println()
9697
return collection
9798
}
9899

@@ -106,13 +107,16 @@ func BackupConstraintsAndStartDataLoading(tables []TableCollection) {
106107
Infof("Total numbers of tables to mock: %d", totalTables)
107108
for _, t := range tables {
108109
// Remove Constraints
109-
table := fmt.Sprintf("\"%s\".\"%s\"", t.Schema, t.Table)
110+
table := GenerateTableName(t.Table, t.Schema)
110111
RemoveConstraints(table)
111112

112113
// Start the committing data to the table
113114
CommitData(t)
114115
}
115116

117+
// Now load the one column serial data type table
118+
addDataIfItsASerialDatatype()
119+
116120
// If the program skipped the tables lets the users know
117121
skipTablesWarning()
118122

@@ -123,8 +127,9 @@ func BackupConstraintsAndStartDataLoading(tables []TableCollection) {
123127
func CommitData(t TableCollection) {
124128
// Start committing data
125129
tab := GenerateTableName(t.Table, t.Schema)
126-
msg := fmt.Sprintf("Mocking Table %s", tab)
130+
msg := fmt.Sprintf(progressBarMsg, tab)
127131
bar := StartProgressBar(msg, cmdOptions.Rows)
132+
Debugf("Building and loading mock data to the table %s", tab)
128133

129134
// Open db connection
130135
db := ConnectDB()
@@ -159,7 +164,6 @@ DataTypePickerLoop:
159164
CopyData(tab, col, data, db)
160165
bar.Add(1)
161166
}
162-
fmt.Println()
163167
}
164168

165169
// Copy the data to the database table
@@ -171,34 +175,49 @@ func CopyData(tab string, col []string, data []string, db *pg.DB) {
171175

172176
// Handle Error
173177
if err != nil {
174-
fmt.Println()
175178
Debugf("Table: %s", tab)
176179
Debugf("Copy Statement: %s", copyStatment)
177180
Debugf("Data: %s", strings.Join(data, delimiter))
178181
Fatalf("Error during committing data: %v", err)
179182
}
180183
}
181184

182-
// Insert data to the table if its only a single column with serial data type
183-
func checkAndAddDataIfItsASerialDatatype(t DBTables, c []DBColumns) {
184-
Debugf("Check if the table %s.%s which has only a single column is of serial data type", t.Schema, t.Table)
185+
186+
// Check its a serial datatype
187+
func checkIfOneColumnIsASerialDatatype(t DBTables, c []DBColumns) {
188+
tab := GenerateTableName(t.Table, t.Schema)
185189
column := c[0] // we know its only one , because we did a check on the parent function
186-
total := 0
190+
Debugf("Check if the table %s which has only a single column is of serial data type", tab)
191+
192+
// If they are save them for later use
187193
if isItSerialDatatype(column) {
194+
oneColumnTable = append(oneColumnTable, tab)
195+
}
196+
}
197+
198+
// Insert data to the table if its only a single column with serial data type
199+
func addDataIfItsASerialDatatype() {
200+
for _, t := range oneColumnTable {
201+
var total = 0
202+
// Start the progress bar
203+
bar := StartProgressBar(fmt.Sprintf(progressBarMsg, t), cmdOptions.Rows)
204+
Debugf("Loading data for one column serial data type table %s", t)
205+
206+
// Start loading
188207
for total < cmdOptions.Rows {
189-
query := "INSERT INTO \"%s\".\"%s\" default values;"
190-
query = fmt.Sprintf(query, t.Schema, t.Table)
208+
query := "INSERT INTO %s default values;"
209+
query = fmt.Sprintf(query, t)
191210
_, err := ExecuteDB(query)
192211
if err != nil {
193-
Fatalf("Error when loading the serial datatype for table %s.%s, err: %v",
194-
t.Schema, t.Table, err)
212+
Fatalf("Error when loading the serial datatype for table %s, err: %v", t, err)
195213
}
196214
total++
215+
bar.Add(1)
197216
}
198217
}
199218
}
200219

201-
// Is it serial
220+
// Is it serial data type
202221
func isItSerialDatatype(c DBColumns) bool {
203222
if strings.HasPrefix(c.Sequence, "nextval") {
204223
return true

0 commit comments

Comments
 (0)