Sha256: ebd96468ba11545059bed35405d9961b023c80db8d3096e574328c9f656cf49b

Contents?: true

Size: 1.84 KB

Versions: 16

Compression:

Stored size: 1.84 KB

Contents

class Ridgepole::ExecuteExpander
  class Stub
    def method_missing(method_name, *args, &block)
      # Nothing to do
    end
  end

  cattr_accessor :noop,     :instance_writer => false, :instance_reader => false
  cattr_accessor :callback, :instance_writer => false, :instance_reader => false

  class << self
    def without_operation(callback = nil)
      begin
        self.noop = true
        self.callback = callback
        yield
      ensure
        self.noop = false
        self.callback = nil
      end
    end

    def expand_execute(connection)
      return if connection.respond_to?(:execute_with_noop)

      class << connection
        def execute_with_noop(sql, name = nil)
          if Ridgepole::ExecuteExpander.noop
            if (callback = Ridgepole::ExecuteExpander.callback)
              callback.call(sql, name)
            end

            if sql =~ /\A(SELECT|SHOW)\b/i
              execute_without_noop(sql, name)
            else
              Stub.new
            end
          else
            execute_without_noop(sql, name)
          end
        end
        alias_method_chain :execute, :noop
      end
    end
  end # of class methods
end

require 'active_record/connection_adapters/abstract/schema_statements'

module ActiveRecord::ConnectionAdapters::SchemaStatements
  def index_name_exists_with_noop?(table_name, column_name, options = {})
    if Ridgepole::ExecuteExpander.noop
      caller_methods = caller.map {|i| i =~ /:\d+:in `(.+)'/ ? $1 : '' }

      if caller_methods.any? {|i| i =~ /\Aremove_index/ }
        true
      elsif caller_methods.any? {|i| i =~ /\Aadd_index/ }
        false
      else
        index_name_exists_without_noop?(table_name, column_name, options)
      end
    else
      index_name_exists_without_noop?(table_name, column_name, options)
    end
  end
  alias_method_chain :index_name_exists?, :noop
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ridgepole-0.4.9 lib/ridgepole/execute_expander.rb
ridgepole-0.4.8 lib/ridgepole/execute_expander.rb
ridgepole-0.4.8.rc2 lib/ridgepole/execute_expander.rb
ridgepole-0.4.8.rc1 lib/ridgepole/execute_expander.rb
ridgepole-0.4.7 lib/ridgepole/execute_expander.rb
ridgepole-0.4.6 lib/ridgepole/execute_expander.rb
ridgepole-0.4.5 lib/ridgepole/execute_expander.rb
ridgepole-0.4.4 lib/ridgepole/execute_expander.rb
ridgepole-0.4.3 lib/ridgepole/execute_expander.rb
ridgepole-0.4.2 lib/ridgepole/execute_expander.rb
ridgepole-0.4.1 lib/ridgepole/execute_expander.rb
ridgepole-0.4.0 lib/ridgepole/execute_expander.rb
ridgepole-0.3.9 lib/ridgepole/execute_expander.rb
ridgepole-0.3.8 lib/ridgepole/execute_expander.rb
ridgepole-0.3.7 lib/ridgepole/execute_expander.rb
ridgepole-0.3.6 lib/ridgepole/execute_expander.rb