Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no merge] patch to stable-2-x #9

Draft
wants to merge 13 commits into
base: stable-2-x
Choose a base branch
from
Draft
8 changes: 6 additions & 2 deletions lib/active_record/turntable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ module ActiveRecord::Turntable
include Base
end

RackupFramework = Rails if defined?(Rails);
RackupFramework = Padrino if defined?(Padrino);

module ClassMethods
DEFAULT_PATH = File.dirname(File.dirname(__FILE__))

def turntable_config_file
@@turntable_config_file ||=
File.join(defined?(::Rails) ?
::Rails.root.to_s : DEFAULT_PATH, 'config/turntable.yml')
File.join(defined?(ActiveRecord::Turntable::RackupFramework) ?
ActiveRecord::Turntable::RackupFramework.root.to_s : DEFAULT_PATH, 'config/turntable.yml')
end

def turntable_config_file=(filename)
Expand All @@ -69,4 +72,5 @@ def turntable_config
end

require "active_record/turntable/railtie" if defined?(Rails)
require "active_record/turntable/padrinotie" if defined?(Padrino)
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def log(sql, name = "SQL", binds = [], statement_name = nil)
:turntable_shard_name => turntable_shard_name) { yield }
rescue Exception => e
message = "#{e.class.name}: #{e.message}: #{sql} : #{turntable_shard_name}"
@logger.error message if @logger
@logger.debug message if @logger
exception = translate_exception(e, message)
exception.set_backtrace e.backtrace
raise exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module ActiveRecord
module Tasks
module DatabaseTasks
def env
@env ||= ActiveRecord::Turntable::RackupFramework.env
end

def create_all_turntable_cluster
each_local_turntable_cluster_configuration { |name, configuration|
puts "[turntable] *** executing to database: #{configuration['database']}"
Expand Down
1 change: 0 additions & 1 deletion lib/active_record/turntable/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def turntable(cluster_name, shard_key_name, options = {})
turntable_replace_connection_pool
end


def turntable_replace_connection_pool
ch = connection_handler
cp = ConnectionProxy.new(self, turntable_cluster)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/turntable/cluster_helper_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def recursive_transaction(pools, options, &block)
end

def force_connect_all_shards!
conf = configurations[Rails.env]
conf = configurations[ActiveRecord::Turntable::RackupFramework.env]
shards = {}
shards = shards.merge(conf["shards"]) if conf["shards"]
shards = shards.merge(conf["seq"]) if conf["seq"]
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/turntable/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def [](key)
@config[key]
end

def self.load!(config_file = ActiveRecord::Base.turntable_config_file, env = (defined?(Rails) ? Rails.env : 'development'))
def self.load!(config_file = ActiveRecord::Base.turntable_config_file, env = (defined?(ActiveRecord::Turntable::RackupFramework) ? ActiveRecord::Turntable::RackupFramework.env : 'development')) # FIXME
instance.load!(config_file, env)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/turntable/connection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def supports_views?(*args)
end

def spec
@spec ||= master.connection_pool.spec
@spec ||= shards.values.first.connection_pool.spec
end

private
Expand Down
69 changes: 44 additions & 25 deletions lib/active_record/turntable/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module ActiveRecord::Turntable::Migration

included do
extend ShardDefinition
class_attribute :target_shards, :current_shard
class_attribute :target_shards, :current_shard, :target_seqs
alias_method_chain :announce, :turntable
alias_method_chain :migrate, :turntable
alias_method_chain :exec_migration, :turntable
::ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, SchemaStatementsExt)
::ActiveRecord::Migration::CommandRecorder.send(:include, CommandRecorder)
Expand All @@ -13,19 +14,17 @@ module ActiveRecord::Turntable::Migration

module ShardDefinition
def clusters(*cluster_names)
config = ActiveRecord::Base.turntable_config
config = ActiveRecord::Base.turntable_config["clusters"]
cluster_names = config.keys if cluster_names.first == :all
(self.target_shards ||= []).concat(
if cluster_names.first == :all
config['clusters'].map do |name, cluster_conf|
cluster_conf["shards"].map {|shard| shard["connection"]}
end
else
cluster_names.map do |cluster_name|
config['clusters'][cluster_name]["shards"].map do |shard|
shard["connection"]
end
config[cluster_name]["shards"].map { |shard| shard["connection"] }
end.flatten
)
(self.target_seqs ||= []).concat(
cluster_names.map do |cluster_name|
config[cluster_name]["seq"].values.map { |seq| seq["connection"] }
end.flatten
end
)
end

Expand All @@ -35,15 +34,35 @@ def shards(*connection_names)
end

def target_shard?(shard_name)
target_shards.blank? or target_shards.include?(shard_name)
return false if shard_name.present? && target_shards.blank?
shard_name.nil? or target_shards.blank? or target_shards.include?(shard_name)
end

def announce_with_turntable(message)
announce_without_turntable("#{message} - Shard: #{current_shard}")
end

def exec_migration_with_turntable(*args)
exec_migration_without_turntable(*args) if target_shard?(current_shard)
exec_migration_without_turntable(*args) # if target_shard?(current_shard)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target_shard? seems never used so changes to it is not important

end

def migrate_with_turntable(*args)
return migrate_without_turntable(*args) if target_shards.blank?

config = ActiveRecord::Base.configurations[ActiveRecord::Turntable::RackupFramework.env||"development"]
shard_conf = target_shards.map { |shard| [shard, config["shards"][shard]] }.to_h
seqs_conf = target_seqs.map { |seq| [seq, config["seq"][seq]] }.to_h

# SHOW FULL FIELDS FROM `users` を実行してテーブルの情報を取得するためにデフォルトのデータベースも追加する
{"master": config}.merge(shard_conf).merge(seqs_conf).each do |connection_name, database_config|
next if database_config["database"].blank?
ActiveRecord::Base.clear_active_connections!
ActiveRecord::Base.establish_connection(database_config)
ActiveRecord::Migration.current_shard = connection_name
migrate_without_turntable(*args)
end
ActiveRecord::Base.establish_connection config
ActiveRecord::Base.clear_active_connections!
end

module SchemaStatementsExt
Expand Down Expand Up @@ -114,28 +133,28 @@ module ClassMethods
def up_with_turntable(migrations_paths, target_version = nil)
up_without_turntable(migrations_paths, target_version)

ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected do |name, configuration|
puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
_original_up(migrations_paths, target_version)
end
# ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected do |name, configuration|
# puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
# _original_up(migrations_paths, target_version)
# end
end

def down_with_turntable(migrations_paths, target_version = nil, &block)
down_without_turntable(migrations_paths, target_version, &block)

ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected do |name, configuration|
puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
_original_down(migrations_paths, target_version, &block)
end
# ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected do |name, configuration|
# puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
# _original_down(migrations_paths, target_version, &block)
# end
end

def run_with_turntable(*args)
run_without_turntable(*args)

ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected do |name, configuration|
puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
_original_run(*args)
end
# ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected do |name, configuration|
# puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
# _original_run(*args)
# end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/turntable/mixer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def build_select_fader(tree, method, query, *args, &block)
build_shards_with_same_query(@proxy.shards.values, query),
method, query, *args, &block
)
elsif tree.group_by or tree.order_by or tree.limit.try(:value).to_i > 0
elsif @proxy.current_shard.blank? and (tree.group_by or tree.order_by or tree.limit.try(:value).to_i > 0)
raise CannotSpecifyShardError, "cannot specify shard for query: #{tree.to_sql}"
elsif shard_keys.present?
if SQLTree::Node::SelectDeclaration === tree.select.first and
Expand Down
17 changes: 17 additions & 0 deletions lib/active_record/turntable/padrinotie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
module ActiveRecord::Turntable
class Padrinotie
Padrino::Tasks.files << File.dirname(__FILE__) + "/padrinoties/databases.rb"

# padrino loading hook
Padrino.before_load do
ActiveRecord::Base.send(:include, ActiveRecord::Turntable)

require 'generators/padrino/turntable/install_generators'
end
# # Swap QueryCache Middleware
# initializer "turntable.swap_query_cache_middleware" do |app|
# app.middleware.swap ActiveRecord::QueryCache, ActiveRecord::Turntable::Rack::QueryCache
# end
end
end
Loading