@@ -227,6 +227,98 @@ func TestBuildRangeInsertQuery(t *testing.T) {
227227 }
228228}
229229
230+
231+ func TestBuildRangeInsertQueryWhereClauseFiltering (t * testing.T ) {
232+ databaseName := "mydb"
233+ originalTableName := "tbl"
234+ ghostTableName := "ghost"
235+ sharedColumns := []string {"id" , "name" , "position" }
236+ {
237+ uniqueKey := "PRIMARY"
238+ uniqueKeyColumns := NewColumnList ([]string {"id" })
239+ rangeStartValues := []string {"@v1s" }
240+ rangeEndValues := []string {"@v1e" }
241+ rangeStartArgs := []interface {}{3 }
242+ rangeEndArgs := []interface {}{103 }
243+ whereClause := "id = 1"
244+
245+ query , explodedArgs , err := BuildRangeInsertQuery (databaseName , originalTableName , ghostTableName , sharedColumns , sharedColumns , uniqueKey , uniqueKeyColumns , rangeStartValues , rangeEndValues , rangeStartArgs , rangeEndArgs , true , false , whereClause )
246+ test .S (t ).ExpectNil (err )
247+ expected := `
248+ insert /* gh-ost mydb.tbl */ ignore
249+ into
250+ mydb.ghost
251+ (id, name, position)
252+ (
253+ select id, name, position
254+ from
255+ mydb.tbl
256+ force index (PRIMARY)
257+ where
258+ (((id > @v1s) or ((id = @v1s)))
259+ and ((id < @v1e) or ((id = @v1e))) and id = 1)
260+ )`
261+ test .S (t ).ExpectEquals (normalizeQuery (query ), normalizeQuery (expected ))
262+ test .S (t ).ExpectTrue (reflect .DeepEqual (explodedArgs , []interface {}{3 , 3 , 103 , 103 }))
263+ }
264+ {
265+ uniqueKey := "PRIMARY"
266+ uniqueKeyColumns := NewColumnList ([]string {"id" })
267+ rangeStartValues := []string {"@v1s" }
268+ rangeEndValues := []string {"@v1e" }
269+ rangeStartArgs := []interface {}{3 }
270+ rangeEndArgs := []interface {}{103 }
271+ whereClause := "id not in (1,2,3)"
272+
273+ query , explodedArgs , err := BuildRangeInsertQuery (databaseName , originalTableName , ghostTableName , sharedColumns , sharedColumns , uniqueKey , uniqueKeyColumns , rangeStartValues , rangeEndValues , rangeStartArgs , rangeEndArgs , true , false , whereClause )
274+ test .S (t ).ExpectNil (err )
275+ expected := `
276+ insert /* gh-ost mydb.tbl */ ignore
277+ into
278+ mydb.ghost
279+ (id, name, position)
280+ (
281+ select id, name, position
282+ from
283+ mydb.tbl
284+ force index (PRIMARY)
285+ where
286+ (((id > @v1s) or ((id = @v1s)))
287+ and ((id < @v1e) or ((id = @v1e))) and id not in (1,2,3))
288+ )`
289+ test .S (t ).ExpectEquals (normalizeQuery (query ), normalizeQuery (expected ))
290+ test .S (t ).ExpectTrue (reflect .DeepEqual (explodedArgs , []interface {}{3 , 3 , 103 , 103 }))
291+ }
292+ {
293+ uniqueKey := "PRIMARY"
294+ uniqueKeyColumns := NewColumnList ([]string {"id" })
295+ rangeStartValues := []string {"@v1s" }
296+ rangeEndValues := []string {"@v1e" }
297+ rangeStartArgs := []interface {}{3 }
298+ rangeEndArgs := []interface {}{103 }
299+ whereClause := "id in (select id from ids)"
300+
301+ query , explodedArgs , err := BuildRangeInsertQuery (databaseName , originalTableName , ghostTableName , sharedColumns , sharedColumns , uniqueKey , uniqueKeyColumns , rangeStartValues , rangeEndValues , rangeStartArgs , rangeEndArgs , true , false , whereClause )
302+ test .S (t ).ExpectNil (err )
303+ expected := `
304+ insert /* gh-ost mydb.tbl */ ignore
305+ into
306+ mydb.ghost
307+ (id, name, position)
308+ (
309+ select id, name, position
310+ from
311+ mydb.tbl
312+ force index (PRIMARY)
313+ where
314+ (((id > @v1s) or ((id = @v1s)))
315+ and ((id < @v1e) or ((id = @v1e))) and id in (select id from ids))
316+ )`
317+ test .S (t ).ExpectEquals (normalizeQuery (query ), normalizeQuery (expected ))
318+ test .S (t ).ExpectTrue (reflect .DeepEqual (explodedArgs , []interface {}{3 , 3 , 103 , 103 }))
319+ }
320+ }
321+
230322func TestBuildRangeInsertQueryRenameMap (t * testing.T ) {
231323 databaseName := "mydb"
232324 originalTableName := "tbl"
0 commit comments