@@ -14,6 +14,8 @@ type TableCollection struct {
14
14
var (
15
15
skippedTab []string
16
16
delimiter = "$"
17
+ oneColumnTable []string
18
+ progressBarMsg = "Mocking Table %s"
17
19
)
18
20
19
21
func MockTable (tables []DBTables ) {
@@ -56,7 +58,7 @@ func tableMocker(tables []DBTables) {
56
58
57
59
// Extract the column and its datatypes of the table
58
60
func columnExtractor (tables []DBTables ) []TableCollection {
59
- Info ("Extracting the columns and datatype information" )
61
+ Info ("Extracting the columns and data type information" )
60
62
var columns []DBColumns
61
63
var collection []TableCollection
62
64
@@ -71,11 +73,11 @@ func columnExtractor(tables []DBTables) []TableCollection {
71
73
columns = columnExtractorGPDB (fmt .Sprintf ("\" %s\" " , t .Schema ), t .Table )
72
74
}
73
75
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
75
78
// take a look at the issue: https://github.com/pivotal-gss/mock-data/issues/29
76
- // lets fix this
77
79
if len (columns ) == 1 {
78
- checkAndAddDataIfItsASerialDatatype (t , columns )
80
+ checkIfOneColumnIsASerialDatatype (t , columns )
79
81
}
80
82
81
83
// Loops through the columns and make a collection of tables
@@ -92,7 +94,6 @@ func columnExtractor(tables []DBTables) []TableCollection {
92
94
}
93
95
bar .Add (1 )
94
96
}
95
- fmt .Println ()
96
97
return collection
97
98
}
98
99
@@ -106,13 +107,16 @@ func BackupConstraintsAndStartDataLoading(tables []TableCollection) {
106
107
Infof ("Total numbers of tables to mock: %d" , totalTables )
107
108
for _ , t := range tables {
108
109
// Remove Constraints
109
- table := fmt . Sprintf ( " \" %s \" . \" %s \" " , t . Schema , t .Table )
110
+ table := GenerateTableName ( t . Table , t .Schema )
110
111
RemoveConstraints (table )
111
112
112
113
// Start the committing data to the table
113
114
CommitData (t )
114
115
}
115
116
117
+ // Now load the one column serial data type table
118
+ addDataIfItsASerialDatatype ()
119
+
116
120
// If the program skipped the tables lets the users know
117
121
skipTablesWarning ()
118
122
@@ -123,8 +127,9 @@ func BackupConstraintsAndStartDataLoading(tables []TableCollection) {
123
127
func CommitData (t TableCollection ) {
124
128
// Start committing data
125
129
tab := GenerateTableName (t .Table , t .Schema )
126
- msg := fmt .Sprintf ("Mocking Table %s" , tab )
130
+ msg := fmt .Sprintf (progressBarMsg , tab )
127
131
bar := StartProgressBar (msg , cmdOptions .Rows )
132
+ Debugf ("Building and loading mock data to the table %s" , tab )
128
133
129
134
// Open db connection
130
135
db := ConnectDB ()
@@ -159,7 +164,6 @@ DataTypePickerLoop:
159
164
CopyData (tab , col , data , db )
160
165
bar .Add (1 )
161
166
}
162
- fmt .Println ()
163
167
}
164
168
165
169
// Copy the data to the database table
@@ -171,34 +175,49 @@ func CopyData(tab string, col []string, data []string, db *pg.DB) {
171
175
172
176
// Handle Error
173
177
if err != nil {
174
- fmt .Println ()
175
178
Debugf ("Table: %s" , tab )
176
179
Debugf ("Copy Statement: %s" , copyStatment )
177
180
Debugf ("Data: %s" , strings .Join (data , delimiter ))
178
181
Fatalf ("Error during committing data: %v" , err )
179
182
}
180
183
}
181
184
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 )
185
189
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
187
193
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
188
207
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 )
191
210
_ , err := ExecuteDB (query )
192
211
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 )
195
213
}
196
214
total ++
215
+ bar .Add (1 )
197
216
}
198
217
}
199
218
}
200
219
201
- // Is it serial
220
+ // Is it serial data type
202
221
func isItSerialDatatype (c DBColumns ) bool {
203
222
if strings .HasPrefix (c .Sequence , "nextval" ) {
204
223
return true
0 commit comments