Module: ActiveRecord::Turntable::Migration

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_record/turntable/migration.rb

Defined Under Namespace

Modules: CommandRecorder, SchemaStatementsExt, ShardDefinition

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

AR < 3.1



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_record/turntable/migration.rb', line 5

def self.extended(base)
  class << base
    def announce_with_turntable(message)
      announce_without_turntable("#{message} - #{get_current_shard}")
    end

    alias_method_chain :migrate, :turntable
    alias_method_chain :announce, :turntable
    include ShardDefinition
  end
  base.class_eval do
    class_inheritable_accessor :target_shards
  end
  ::ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, SchemaStatementsExt)
end

Instance Method Details

#get_current_shardObject



58
59
60
# File 'lib/active_record/turntable/migration.rb', line 58

def get_current_shard
  "Shard: #{@@current_shard}" if @@current_shard
end

#migrate_with_turntable(direction) ⇒ Object



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

def migrate_with_turntable(direction)
  config = ActiveRecord::Base.configurations
  @@current_shard = nil
  shards = (self.class.target_shards||=[]).flatten.uniq.compact
  if self.class.target_shards.blank?
    return migrate_without_turntable(direction)
  end

  shards_conf = shards.map do |shard|
    config[Rails.env||"development"]["shards"][shard]
  end
  seqs = config[Rails.env||"development"]["seq"]
  shards_conf += seqs.values
  shards_conf << config[Rails.env||"development"]
  shards_conf.each_with_index do |conf, idx|
    @@current_shard = (shards[idx] || seqs.keys[idx - shards.size] || "master")
    ActiveRecord::Base.establish_connection(conf)
    if !ActiveRecord::Base.connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name())
      ActiveRecord::Base.connection.initialize_schema_migrations_table
    end
    migrate_without_turntable(direction)
  end
end