class Object

Constants

RAILS_ENV

Public Instance Methods

invoke_task_if_exists(task_name) click to toggle source
# File lib/bard/rake.rb, line 6
def invoke_task_if_exists task_name
  Rake::Task[task_name].invoke if Rake::Task.task_defined? task_name
end
invoke_with_parallel(task) { || ... } click to toggle source
# File lib/bard/rake/db_multiple_environment_sanity.rb, line 29
def invoke_with_parallel task
  if Rails.version >= "7.1"
    Rake::Task["db:#{task}"].invoke
    if %w[development test].include?(Rails.env) && Rake::Task.task_defined?("parallel:#{task}")
      Rake::Task["parallel:#{task}"].invoke
    end

  else # Rails 6.1, 7.0
    run_in_all_environments do |config, env|
      if env == "test" && Rake::Task.task_defined?("parallel:#{task}")
        Rake::Task["parallel:#{task}"].invoke
      else
        if block_given?
          yield
        else
          ActiveRecord::Tasks::DatabaseTasks.send task.to_sym, config
        end
      end
    end
  end
end
run_in_all_environments(&block) click to toggle source
# File lib/bard/rake/db_multiple_environment_sanity.rb, line 52
def run_in_all_environments &block
  invoke_task_if_exists :rails_env

  whitelist = %w(development test staging production)
  modified_databases = Set.new

  ActiveRecord::Base.configurations.configs_for.each do |db_config|
    env = db_config.env_name
    if Rails.version < "7"
      configuration = db_config.config
      database = configuration["database"]
    else
      configuration = db_config.configuration_hash
      database = configuration[:database]
    end

    next unless database
    next unless whitelist.include?(env)
    next if modified_databases.include?(database)

    ActiveRecord::Base.establish_connection configuration
    block.call configuration, env

    modified_databases << database
  end

  Rake::Task["db:_dump"].invoke
end