Sha256: bd303e004e27237b374af5609decce61e4857c9ab52f3b57b1aca6b5437e84c9

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

# ActiveRecord's update_all invokes compile_update for the Arel::SelectManager returned
# by build_arel. It returns an Arel::UpdateManager. This makes sure that internal call
# assembles the update query correctly.

module Unreliable
  class SqlTestingData
    class_attribute :update_manager_sql
  end
end

RSpec.describe "update_manager" do
  it "in ActiveRecord >= 7, updates by subquery with select in random order",
    skip: ((ActiveRecord::VERSION::MAJOR < 7) ? "test is for ActiveRecord >= 7 only" : false) do
    module Arel
      class SelectManager
        def testing_compile_update(*args)
          um = old_compile_update(*args)
          Unreliable::SqlTestingData.update_manager_sql = um.to_sql
          um
        end
        alias_method :old_compile_update, :compile_update
        alias_method :compile_update, :testing_compile_update
      end
    end
    Cat.update_all(name: "bar")
    expect(Unreliable::SqlTestingData.update_manager_sql).to end_with("ORDER BY RANDOM())")
    Cat.where(name: "foo").update_all(name: "bar")
    expect(Unreliable::SqlTestingData.update_manager_sql).to end_with("ORDER BY RANDOM())")
    Cat.where(name: "bar").order(:id).update_all(name: "baz")
    expect(Unreliable::SqlTestingData.update_manager_sql).to end_with("ORDER BY \"cats\".\"id\" ASC)")
  ensure
    module Arel
      class SelectManager
        alias_method :compile_update, :old_compile_update
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unreliable-0.9.1 spec/model_update_arel_10_spec.rb
unreliable-0.9.0 spec/model_update_arel_10_spec.rb