lib/tasks/switchman.rake in switchman-1.3.18 vs lib/tasks/switchman.rake in switchman-1.4.0

- old
+ new

@@ -66,18 +66,10 @@ %w{db:migrate db:migrate:up db:migrate:down db:rollback}.each { |task_name| shardify_task(task_name) } private - def self.none(scope) - if ::Rails.version < '4' - scope.where("?", false) - else - scope.none - end - end - def self.shard_scope(scope, raw_shard_ids) raw_shard_ids = raw_shard_ids.split(',') shard_ids = [] negative_shard_ids = [] @@ -113,19 +105,19 @@ raise "Invalid fractional chunk: #{id}" end # one chunk means everything if denominator == 1 next if numerator == 1 - return none(scope) + return scope.none end total_shard_count ||= scope.count per_chunk = (total_shard_count / denominator.to_f).ceil index = numerator.abs # more chunks than shards; the trailing chunks are all empty - return none(scope) if index > total_shard_count + return scope.none if index > total_shard_count subscope = Shard.select(:id).order(:id) select = [] if index != 1 subscope = subscope.offset(per_chunk * (index - 1)) @@ -134,16 +126,11 @@ if index != denominator subscope = subscope.limit(per_chunk) select << "MAX(id) AS max_id" end - outerscope = if ::Rails.version < '4' - Shard.from("(#{subscope.to_sql}) subquery") - else - Shard.from(subscope) - end - result = outerscope.select(select.join(", ")).to_a.first + result = Shard.from(subscope).select(select.join(", ")).to_a.first if index == 1 range = "id<=#{result['max_id']}" elsif index == denominator range = "id>=#{result['min_id']}" else @@ -159,11 +146,11 @@ shard_ids.uniq! negative_shard_ids.uniq! unless shard_ids.empty? shard_ids -= negative_shard_ids if shard_ids.empty? && ranges.empty? - return none(scope) + return scope.none end # we already trimmed them all out; no need to make the server do it as well negative_shard_ids = [] if ranges.empty? end @@ -188,79 +175,53 @@ @filter_database_servers_chain ||= ->(servers) { servers } end end end -if ::Rails.version < '4' - old_task = Rake::Task['db:structure:dump'] - old_actions = old_task.actions.dup - old_task.actions.clear +module Switchman + module ActiveRecord + module PostgreSQLDatabaseTasks + if ::Rails.version < '4.2' + def structure_dump(filename) + set_psql_env + search_path = configuration['schema_search_path'] + unless search_path.blank? + search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ") + serialized_search_path = ::ActiveRecord::Base.connection.schema_search_path + end + if configuration['use_qualified_names'] + shard = Shard.current.name + serialized_search_path = shard + search_path = "--schema=#{Shellwords.escape(shard)}" + end - old_task.enhance do - config = current_config - if config['use_qualified_names'] && config['adapter'] =~ /postgresql/ - filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql") - set_psql_env(config) - shard = Shard.current.name - search_path = "--schema=#{Shellwords.escape(shard)}" - `pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(config['database'])}` - raise 'Error dumping database' if $?.exitstatus == 1 - File.open(filename, "a") { |f| f << "SET search_path TO #{shard};\n\n" } + command = "pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}" + raise 'Error dumping database' unless Kernel.system(command) - if ActiveRecord::Base.connection.supports_migrations? - File.open(filename, "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information } - end - old_task.reenable - else - old_actions.each(&:call) - end - end -else - module Switchman - module ActiveRecord - module PostgreSQLDatabaseTasks - if ::Rails.version < '4.2' - def structure_dump(filename) - set_psql_env - search_path = configuration['schema_search_path'] - unless search_path.blank? - search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ") - serialized_search_path = ::ActiveRecord::Base.connection.schema_search_path - end - if configuration['use_qualified_names'] - shard = Shard.current.name - serialized_search_path = shard - search_path = "--schema=#{Shellwords.escape(shard)}" - end - - command = "pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}" - raise 'Error dumping database' unless Kernel.system(command) - - File.open(filename, "a") { |f| f << "SET search_path TO #{serialized_search_path};\n\n" } + File.open(filename, "a") { |f| f << "SET search_path TO #{serialized_search_path};\n\n" } + end + else + def structure_dump(filename) + set_psql_env + args = ['-i', '-s', '-x', '-O', '-f', filename] + search_path = configuration['schema_search_path'] + if configuration['use_qualified_names'] + shard = Shard.current.name + serialized_search_path = shard + args << "--schema=#{Shellwords.escape(shard)}" + elsif !search_path.blank? + args << search_path.split(',').map do |part| + "--schema=#{part.strip}" + end.join(' ') + serialized_search_path = connection.schema_search_path end - else - def structure_dump(filename) - set_psql_env - args = ['-i', '-s', '-x', '-O', '-f', filename] - search_path = configuration['schema_search_path'] - if configuration['use_qualified_names'] - shard = Shard.current.name - serialized_search_path = shard - args << "--schema=#{Shellwords.escape(shard)}" - elsif !search_path.blank? - args << search_path.split(',').map do |part| - "--schema=#{part.strip}" - end.join(' ') - serialized_search_path = connection.schema_search_path - end - args << configuration['database'] - run_cmd('pg_dump', args, 'dumping') - File.open(filename, "a") { |f| f << "SET search_path TO #{serialized_search_path};\n\n" } - end + args << configuration['database'] + run_cmd('pg_dump', args, 'dumping') + File.open(filename, "a") { |f| f << "SET search_path TO #{serialized_search_path};\n\n" } end end end end - - ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend(Switchman::ActiveRecord::PostgreSQLDatabaseTasks) end + +ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend(Switchman::ActiveRecord::PostgreSQLDatabaseTasks)