Permalink
Browse files

Default migration connection specification is possible now

  • Loading branch information...
1 parent b124b20 commit 9b93ff416a72c3d63653221a8d1fe9da7b9a3d2e @kovyrin kovyrin committed Apr 9, 2010
Showing with 25 additions and 10 deletions.
  1. +25 −10 lib/db_charmer/multi_db_migrations.rb
@@ -1,12 +1,31 @@
module DbCharmer
module MultiDbMigrations
- @multi_db_names = nil
+ def self.extended(base)
+ class << base
+ alias_method_chain :migrate, :db_wrapper
+ end
+ end
+
+ @@multi_db_names = {}
+ def multi_db_names
+ puts "Retrieving connections for #{self.name}"
+ @@multi_db_names[self.name] || @@multi_db_names['ActiveRecord::Migration']
+ end
+
+ def multi_db_names=(names)
+ puts "Setting connections for #{self.name}"
+ @@multi_db_names[self.name] = names
+ end
def migrate_with_db_wrapper(direction)
- @multi_db_names.each do |multi_db_name|
- on_db(multi_db_name) do
- migrate_without_db_wrapper(direction)
+ if names = 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
@@ -21,9 +40,8 @@ def on_db(db_name)
yield
ensure
# Switch it back
- announce "Checking all database connections"
ActiveRecord::Base.verify_active_connections!
- announce "Switching connection back to default"
+ announce "Switching connection back"
ActiveRecord::Base.switch_connection_to(old_proxy)
end
@@ -37,10 +55,7 @@ def db_magic(opts = {})
raise ArgumentError, "No connection name - no magic!" unless conns.any?
# Save connections
- @multi_db_names = conns
- class << self
- alias_method_chain :migrate, :db_wrapper
- end
+ self.multi_db_names = conns
end
# Return a list of connections to shards in a sharded connection

0 comments on commit 9b93ff4

Please sign in to comment.