Skip to content

Commit

Permalink
一旦コミット
Browse files Browse the repository at this point in the history
  • Loading branch information
gussan committed May 16, 2014
1 parent 1d8feb6 commit 694cc8f
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 15 deletions.
5 changes: 2 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ namespace :turntable do
require 'active_record/turntable'
ActiveRecord::Base.send(:include, ActiveRecord::Turntable)
ActiveRecord::ConnectionAdapters::SchemaStatements.send(:include, ActiveRecord::Turntable::Migration::SchemaStatementsExt)
database_configs = [ActiveRecord::Base.configurations[RAILS_ENV]] + ActiveRecord::Base.configurations[RAILS_ENV]["shards"].values + ActiveRecord::Base.configurations[RAILS_ENV]["seq"].values

database_configs.each do |dbconf|
ActiveRecord::Base.establish_connection dbconf
ActiveRecord::Tasks::DatabaseTasks.each_current_cluster_connected(RAILS_ENV) do |configuration|
ActiveRecord::Base.establish_connection configuration

ActiveRecord::Base.connection.create_table :users do |t|
t.string :nickname
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Tasks
module DatabaseTasks
attr_accessor :exclude_shard_regexp

def create_current_cluster(environment = env)
def create_current_cluster(environment)
each_current_cluster_configuration(environment) { |configuration|
puts "*** executing to database: #{configuration['database']}"
create configuration
Expand All @@ -20,7 +20,7 @@ def drop_current_cluster(environment = env)
}
end

def each_current_cluster_connected(environment = env, exclude_pattern = exclude_shard_regexp)
def each_current_cluster_connected(environment, exclude_pattern = exclude_shard_regexp)
each_current_cluster_configuration(environment, exclude_pattern) do |configuration|
ActiveRecord::Base.clear_active_connections!
ActiveRecord::Base.establish_connection(configuration)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/turntable/sequencer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.build(klass, sequence_name = nil)
unless sequence_name
sequence_name = ActiveRecord::Base.turntable_config["clusters"][klass.turntable_cluster_name.to_s]["seq"]["connection"]
end
seq_config = ActiveRecord::Base.configurations[Rails.env]["seq"][sequence_name.to_s]
seq_config = ActiveRecord::Base.configurations[sequence_name.to_s]
seq_type = (seq_config["seq_type"] ? seq_config["seq_type"].to_sym : :mysql)
@@tables[klass.table_name] ||= (@@sequences[sequence_name(klass.table_name, klass.primary_key)] ||= @@sequence_types[seq_type].new(klass, seq_config))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/turntable/shard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def name
def retrieve_connection_pool
ActiveRecord::Base.turntable_connections[name] ||=
begin
config = ActiveRecord::Base.configurations[Rails.env][name]
config = ActiveRecord::Base.configurations[name]
raise ArgumentError, "Unknown database config: #{name}, have #{ActiveRecord::Base.configurations.inspect}" unless config
ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec_for(config))
end
Expand Down
10 changes: 5 additions & 5 deletions spec/config/turntable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ test:
seq:
connection: turntable_shard_seq_1_test
shards:
- connection: turntable_shard_0
- connection: turntable_shard_0_test
less_than: 20000
- connection: turntable_shard_1
- connection: turntable_shard_1_test
less_than: 40000
- connection: turntable_shard_0
- connection: turntable_shard_0_test
less_than: 60000
- connection: turntable_shard_1
- connection: turntable_shard_1_test
less_than: 80000
- connection: turntable_shard_2
- connection: turntable_shard_2_test
less_than: 10000000
6 changes: 3 additions & 3 deletions spec/test_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
class User < ActiveRecord::Base
# shard by surrogate_key
turntable :user_cluster, :id
sequencer :user_seq_1
sequencer :turntable_shard_seq_1_test
has_one :user_status
has_many :cards_user
end

class UserStatus < ActiveRecord::Base
# shard by other key
turntable :user_cluster, :user_id
sequencer :user_seq_1
sequencer :turntable_shard_seq_1_test
belongs_to :user
end

Expand All @@ -20,7 +20,7 @@ class Card < ActiveRecord::Base

class CardsUser < ActiveRecord::Base
turntable :user_cluster, :user_id
sequencer :user_seq_1
sequencer :turntable_shard_seq_1_test

belongs_to :user
belongs_to :card
Expand Down

0 comments on commit 694cc8f

Please sign in to comment.