Module: DbCharmer::ActiveRecord::Migration::MultiDbMigrations

Defined in:
lib/db_charmer/active_record/migration/multi_db_migrations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#migrate_with_db_wrapper(direction) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# 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, &block) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Object



94
95
96
97
98
99
100
# 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) ⇒ Object



102
103
104
105
106
107
108
# 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