@@ -230,14 +230,16 @@ def unblock_strands(target_shard, batch_size: 10_000)
230230 blocked_shard_ids = blocked_shards . pluck ( :id )
231231 query = lambda { |column , scope |
232232 ::Delayed ::Job
233- . where ( id : ::Delayed ::Job . select ( "DISTINCT ON (#{ column } ) id" )
234- . where ( scope )
233+ . where ( id : ::Delayed ::Job
234+ . with ( all_col : ::Delayed ::Job . and ( scope ) . distinct . select ( column ) )
235+ . with ( blocked_col : ::Delayed ::Job . from ( "all_col" ) . select ( column ) . where . not (
236+ ::Delayed ::Job . select ( 1 )
237+ . where ( next_in_strand : true ) . or ( ::Delayed ::Job . where ( source : "JobsMigrator::StrandBlocker" ) )
238+ . where ( "#{ column } = all_col.#{ column } " ) . arel . exists
239+ ) )
240+ . select ( "DISTINCT ON (#{ column } ) id" )
235241 . where . not ( shard_id : blocked_shard_ids )
236- . where (
237- ::Delayed ::Job . select ( 1 ) . from ( "#{ ::Delayed ::Job . quoted_table_name } dj2" )
238- . where ( "dj2.next_in_strand = true OR dj2.source = 'JobsMigrator::StrandBlocker'" )
239- . where ( "dj2.#{ column } = delayed_jobs.#{ column } " ) . arel . exists . not
240- )
242+ . where ( { column => ::Delayed ::Job . from ( "blocked_col" ) . select ( column ) } )
241243 . order ( column , :strand_order_override , :id ) ) . limit ( batch_size )
242244 }
243245
@@ -249,12 +251,13 @@ def unblock_strands(target_shard, batch_size: 10_000)
249251 # batches
250252
251253 loop do
252- break if query . call ( :strand , " strand IS NOT NULL" ) . update_all ( next_in_strand : true ) . zero?
254+ break if query . call ( :strand , :: Delayed :: Job . where . not ( strand : nil ) ) . update_all ( next_in_strand : true ) . zero?
253255 end
254256
255257 loop do
256258 break if query . call ( :singleton ,
257- "strand IS NULL AND singleton IS NOT NULL" ) . update_all ( next_in_strand : true ) . zero?
259+ ::Delayed ::Job . where ( strand : nil )
260+ . where . not ( singleton : nil ) ) . update_all ( next_in_strand : true ) . zero?
258261 end
259262 end
260263 end
0 commit comments