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



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 108

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 136

def on_db(db_name, &block)
  if @connection.is_a?(::ActiveRecord::Migration::CommandRecorder)
    record_on_db(db_name, block)
    return
  end

  s = "Switching connection to "
  if db_name.is_a?(Hash)
    s << db_name[:connection_name]
    s << ", schema #{db_name[:schema_name]}" if db_name[:schema_name]
  else
    s << db_name.inspect
  end
  announce s
  # 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



120
121
122
123
124
125
126
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 120

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



128
129
130
131
132
133
134
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 128

def replay_commands_on_db(name, commands)
  on_db(name) do
    commands.each do |cmd, args|
      send(cmd, *args)
    end
  end
end