@@ -14,6 +14,8 @@ type TableCollection struct {
1414var (
1515 skippedTab []string
1616 delimiter = "$"
17+ oneColumnTable []string
18+ progressBarMsg = "Mocking Table %s"
1719)
1820
1921func MockTable (tables []DBTables ) {
@@ -56,7 +58,7 @@ func tableMocker(tables []DBTables) {
5658
5759// Extract the column and its datatypes of the table
5860func 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) {
123127func 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
202221func isItSerialDatatype (c DBColumns ) bool {
203222 if strings .HasPrefix (c .Sequence , "nextval" ) {
204223 return true
0 commit comments