module DbCharmer::ActiveRecord::Migration::MultiDbMigrations

Public Class Methods

append_features(base) click to toggle source
Calls superclass method
# File lib/db_charmer/active_record/migration/multi_db_migrations.rb, line 6
def self.append_features(base)
  return false if base < self
  super
  base.extend const_get("ClassMethods") if const_defined?("ClassMethods")

  base.class_eval do
    if DbCharmer.rails31?
      alias_method_chain :migrate, :db_wrapper
    else
      class << self
        alias_method_chain :migrate, :db_wrapper
      end
    end
  end
end

Public Instance Methods

migrate_with_db_wrapper(direction) click to toggle source
# File lib/db_charmer/active_record/migration/multi_db_migrations.rb, line 82
def migrate_with_db_wrapper(direction)
  if names = self.class.multi_db_names
    names.each do |multi_db_name|
      on_db(multi_db_name) do
        migrate_without_db_wrapper(direction)
      end
    end
  else
    migrate_without_db_wrapper(direction)
  end
end
on_db(db_name) { || ... } click to toggle source
# File lib/db_charmer/active_record/migration/multi_db_migrations.rb, line 110
def on_db(db_name, &block)
  if @connection.is_a?(::ActiveRecord::Migration::CommandRecorder)
    record_on_db(db_name, block)
    return
  end

  name = db_name.is_a?(Hash) ? db_name[:connection_name] : db_name.inspect
  announce "Switching connection to #{name}"
  # Switch connection
  old_connection, old_proxy = @connection, ::ActiveRecord::Base.db_charmer_connection_proxy
  db_name = nil if db_name == :default
  ::ActiveRecord::Base.switch_connection_to(db_name, DbCharmer.connections_should_exist?)
  # Yield the block
  ::ActiveRecord::Base.connection_pool.with_connection do |conn|
    @connection = conn
    yield
  end
ensure
  @connection = old_connection
  # Switch it back
  ::ActiveRecord::Base.verify_active_connections!
  announce "Switching connection back"
  ::ActiveRecord::Base.switch_connection_to(old_proxy)
end
record_on_db(db_name, block) click to toggle source
# File lib/db_charmer/active_record/migration/multi_db_migrations.rb, line 94
def record_on_db(db_name, block)
  recorder = ::ActiveRecord::Migration::CommandRecorder.new(DbCharmer::ConnectionFactory.connect(db_name))
  old_recorder, @connection = @connection, recorder
  block.call
  old_recorder.record :on_db, [db_name, @connection]
  @connection = old_recorder
end
replay_commands_on_db(name, commands) click to toggle source
# File lib/db_charmer/active_record/migration/multi_db_migrations.rb, line 102
def replay_commands_on_db(name, commands)
  on_db(name) do
    commands.each do |cmd, args|
      send(cmd, *args)
    end
  end
end