Sha256: 9549f0510fde6a1687187b3b237af1269dd5e5253d2d93dac765140c030f6e3a

Contents?: true

Size: 827 Bytes

Versions: 4

Compression:

Stored size: 827 Bytes

Contents

# frozen_string_literal: true

# Override ActiveRecord::QueryMethods.build_order to always append a final ORDER(RAND())

require "active_record/connection_adapters/abstract_adapter"

module Unreliable
  module BuildOrder
    def build_order(arel)
      super(arel)

      return unless Unreliable::Config.enabled?

      case Arel::Table.engine.connection.adapter_name
      when "Mysql2"
        # https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_rand
        arel.order("RAND()")

      when "PostgreSQL", "SQLite"
        # https://www.postgresql.org/docs/13/functions-math.html#FUNCTIONS-MATH-RANDOM-TABLE
        # https://www.sqlite.org/lang_corefunc.html#random
        arel.order("RANDOM()")

      else
        raise ArgumentError, "unknown Arel::Table.engine"

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unreliable-0.1.3 lib/unreliable/build_order.rb
unreliable-0.1.2 lib/unreliable/build_order.rb
unreliable-0.1.1 lib/unreliable/build_order.rb
unreliable-0.1.0 lib/unreliable/build_order.rb