@@ -79,7 +79,7 @@ func (cfg *Config) UpdateSchemaWithOps(ops UpdateSchemaOptions) (err error) {
79
79
defer tx .Rollback ()
80
80
81
81
//Run each update query.
82
- cfg .debugPrintln ("sqldb.UpdateSchema" , "Running UpdateQueries..." )
82
+ cfg .infoPrintln ("sqldb.UpdateSchema" , "Running UpdateQueries..." )
83
83
for _ , q := range cfg .UpdateQueries {
84
84
//Translate the query if needed. This will only translate queries with
85
85
//CREATE TABLE in the text.
@@ -93,10 +93,10 @@ func (cfg *Config) UpdateSchemaWithOps(ops UpdateSchemaOptions) (err error) {
93
93
if strings .Contains (q , "CREATE TABLE" ) {
94
94
idx := strings .Index (q , "(" )
95
95
if idx > 0 {
96
- cfg .debugPrintln (strings .TrimSpace (q [:idx ]) + "..." )
96
+ cfg .infoPrintln (strings .TrimSpace (q [:idx ]) + "..." )
97
97
}
98
98
} else {
99
- cfg .debugPrintln (q )
99
+ cfg .infoPrintln (q )
100
100
}
101
101
102
102
//Execute the query. Always log on error so users can identify query that has
@@ -109,29 +109,29 @@ func (cfg *Config) UpdateSchemaWithOps(ops UpdateSchemaOptions) (err error) {
109
109
return
110
110
}
111
111
}
112
- cfg .debugPrintln ("sqldb.UpdateSchema" , "Running UpdateQueries...done" )
112
+ cfg .infoPrintln ("sqldb.UpdateSchema" , "Running UpdateQueries...done" )
113
113
114
114
//Run each update func.
115
- cfg .debugPrintln ("sqldb.UpdateSchema" , "Running UpdateFuncs..." )
115
+ cfg .infoPrintln ("sqldb.UpdateSchema" , "Running UpdateFuncs..." )
116
116
for _ , f := range cfg .UpdateFuncs {
117
117
//Get function name for diagnostics.
118
118
rawNameWithPath := runtime .FuncForPC (reflect .ValueOf (f ).Pointer ()).Name ()
119
119
funcName := path .Base (rawNameWithPath )
120
120
121
121
//Log out some info about the func being run for diagnostics.
122
- cfg .debugPrintln (funcName )
122
+ cfg .infoPrintln (funcName )
123
123
124
124
//Execute the func. Always log on error so users can identify func that has
125
125
//an error. Connection always gets closed since an error occured.
126
126
innerErr := f (tx )
127
127
if innerErr != nil {
128
128
err = innerErr
129
- log . Println ("sqldb.UpdateSchema() " , "Error with func ." , funcName )
129
+ cfg . errorPrintln ("sqldb.UpdateSchema" , "Error with UpdateFunc ." , funcName )
130
130
cfg .Close ()
131
131
return innerErr
132
132
}
133
133
}
134
- cfg .debugPrintln ("sqldb.UpdateSchema" , "Running UpdateFuncs...done" )
134
+ cfg .infoPrintln ("sqldb.UpdateSchema" , "Running UpdateFuncs...done" )
135
135
136
136
//Commit transaction now that all UpdateQueries have been run successfully.
137
137
err = tx .Commit ()
@@ -140,8 +140,11 @@ func (cfg *Config) UpdateSchemaWithOps(ops UpdateSchemaOptions) (err error) {
140
140
return
141
141
}
142
142
143
+ //Close the connection to the database, if needed. We want to keep the connection
144
+ //open mostly for tests since tests might use an in-memory database (SQLite) and
145
+ //if the connection is closed the database gets wiped away.
143
146
if ops .CloseConnection {
144
- // Close() is handled by defer above.
147
+ cfg . Close ()
145
148
cfg .debugPrintln ("sqldb.UpdateSchama" , "Connection closed after success." )
146
149
} else {
147
150
cfg .debugPrintln ("sqldb.UpdateSchama" , "Connection left open after success." )
@@ -172,13 +175,13 @@ func UpdateSchema() (err error) {
172
175
}
173
176
174
177
// ignoreUpdateSchemaErrors handles when an error is returned from an UpdateQuery when
175
- // run from UpdateSchema(). This is used to handle queries that can fail and aren't really
176
- // an error (i.e.: adding a column that already exists). Excusable errors can happen
177
- // because UpdateQueries should be able to run more than once (i.e.: if you run UpdateSchema()
178
- // each time your app starts).
178
+ // run from UpdateSchema(). This is used to handle queries that can fail and aren't
179
+ // really an error (i.e.: adding a column that already exists). Excusable errors can
180
+ // happen because UpdateQueries should be able to run more than once (i.e.: if you run
181
+ // UpdateSchema() each time your app starts).
179
182
//
180
- // The query to update the schema is passed in so that we can check what an error is in
181
- // relation to. Sometimes the error returned doesn't provide enough context.
183
+ // The query to update the schema is passed in so that we can check what an error is
184
+ // in relation to. Sometimes the error returned doesn't provide enough context.
182
185
func (cfg * Config ) ignoreUpdateSchemaErrors (query string , err error ) bool {
183
186
//make sure an error was provided
184
187
if err == nil {
@@ -199,17 +202,17 @@ func (cfg *Config) ignoreUpdateSchemaErrors(query string, err error) bool {
199
202
}
200
203
201
204
// UpdateIgnoreErrorFunc is function for handling errors returned when trying to update
202
- // the schema of your database using UpdateSchema(). The query being run, as well as the
203
- // error from running the query, are passed in so that the function can determine if this
204
- // error can be ignored for this query. Each function of this type, and used for this
205
- // purpose should be very narrowly focused so as not to ignore errors by mistake (false
206
- // positives).
205
+ // the schema of your database using UpdateSchema(). The query being run, as well as
206
+ // the error from running the query, are passed in so that the function can determine
207
+ // if this error can be ignored for this query. Each function of this type, and used
208
+ // for this purpose should be very narrowly focused so as not to ignore errors by
209
+ // mistake (false positives).
207
210
type UpdateIgnoreErrorFunc func (Config , string , error ) bool
208
211
209
- // UFAddDuplicateColumn checks if an error was generated because a column already exists.
210
- // This typically happens because you are rerunning UpdateSchema() and the column has
211
- // already been added. This error can be safely ignored since a duplicate column won't
212
- // be create.
212
+ // UFAddDuplicateColumn checks if an error was generated because a column already
213
+ // exists. This typically happens because you are rerunning UpdateSchema() and the
214
+ // column has already been added. This error can be safely ignored since a duplicate
215
+ // column won't be create.
213
216
func UFAddDuplicateColumn (c Config , query string , err error ) bool {
214
217
addCol := strings .Contains (strings .ToUpper (query ), "ADD COLUMN" )
215
218
dup := strings .Contains (strings .ToLower (err .Error ()), "duplicate column" )
@@ -222,9 +225,9 @@ func UFAddDuplicateColumn(c Config, query string, err error) bool {
222
225
return false
223
226
}
224
227
225
- // UFDropUnknownColumn checks if an error from was generated because a column does not exist.
226
- // This typically happens because you are rerunning UpdateSchema() and the column has
227
- // already been dropped. This error can be safely ignored in most cases.
228
+ // UFDropUnknownColumn checks if an error from was generated because a column does not
229
+ // exist. This typically happens because you are rerunning UpdateSchema() and the
230
+ // column has already been dropped. This error can be safely ignored in most cases.
228
231
func UFDropUnknownColumn (c Config , query string , err error ) bool {
229
232
dropCol := strings .Contains (strings .ToUpper (query ), "DROP COLUMN" )
230
233
@@ -244,12 +247,12 @@ func UFDropUnknownColumn(c Config, query string, err error) bool {
244
247
245
248
// UFModifySQLiteColumn checks if an error occured because you are trying to modify a
246
249
// column for a SQLite database. SQLite does not allow modifying columns. In this case,
247
- // we just ignore the error. This is ok since SQLite allows you to store any type of value
248
- // in any column.
250
+ // we just ignore the error. This is ok since SQLite allows you to store any type of
251
+ // value in any column.
249
252
//
250
- // To get around this error, you should create a new table with the new schema, copy the
251
- // old data to the new table, delete the old table, and rename the new table to the old
252
- // table.
253
+ // To get around this error, you should create a new table with the new schema, copy
254
+ // the old data to the new table, delete the old table, and rename the new table to
255
+ // the old table.
253
256
func UFModifySQLiteColumn (c Config , query string , err error ) bool {
254
257
//ignore queries that modify a column for sqlite dbs
255
258
if strings .Contains (strings .ToUpper (query ), "MODIFY COLUMN" ) && c .Type == DBTypeSQLite {
0 commit comments