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

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

Constant Summary collapse

@@multi_db_names =
{}

Instance Method Summary collapse

Instance Method Details

#db_magic(opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 88

def db_magic(opts = {})
  # Collect connections from all possible options
  conns = [ opts[:connection], opts[:connections] ]
  conns << shard_connections(opts[:sharded_connection]) if opts[:sharded_connection]

  # Get a unique set of connections
  conns = conns.flatten.compact.uniq
  raise ArgumentError, "No connection name - no magic!" unless conns.any?

  # Save connections
  self.multi_db_names = conns
end

#migrate_with_db_wrapper(direction) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 33

def migrate_with_db_wrapper(direction)
  # define a method that can be used to check if
  # a model is extended by db_charmer
  ::ActiveRecord::Base.create_true_method(:db_charmer_extended)

  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

#multi_db_namesObject



24
25
26
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 24

def multi_db_names
  @@multi_db_names[self.name] || @@multi_db_names['ActiveRecord::Migration']
end

#multi_db_names=(names) ⇒ Object



28
29
30
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 28

def multi_db_names=(names)
  @@multi_db_names[self.name] = names
end

#on_db(db_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 65

def on_db(db_name)
  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_proxy = ::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
  yield
ensure
  # Switch it back
  ::ActiveRecord::Base.verify_active_connections!
  announce "Switching connection back"
  ::ActiveRecord::Base.switch_connection_to(old_proxy)
end

#on_each_shard(sharded_connection) ⇒ Object

Run some migration method on all shards



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 50

def on_each_shard(sharded_connection)
  db_magic :sharded_connection => sharded_connection
  if names = multi_db_names
    names.each do |multi_db_name|
      on_db(multi_db_name) do
        yield
      end
    end
  else
    raise ArgumentError, 'no shard names defined'
  end
ensure
  self.multi_db_names = nil
end

#shard_connections(conn_name) ⇒ Object

Return a list of connections to shards in a sharded connection



102
103
104
105
# File 'lib/db_charmer/active_record/migration/multi_db_migrations.rb', line 102

def shard_connections(conn_name)
  conn = DbCharmer::Sharding.sharded_connection(conn_name)
  conn.shard_connections
end