database_tasks.rb 2.5 KB
Newer Older
P
Pat Allan 已提交
1
class ActiveRecord::Tasks::DatabaseTasks
2 3
  TASKS_PATTERNS = {
    /mysql/      => ActiveRecord::Tasks::MySQLDatabaseTasks,
4
    /postgresql/ => ActiveRecord::Tasks::PostgreSQLDatabaseTasks,
5 6
    /sqlite/     => ActiveRecord::Tasks::SQLiteDatabaseTasks
  }
P
Pat Allan 已提交
7
  LOCAL_HOSTS    = ['127.0.0.1', 'localhost']
P
Pat Allan 已提交
8

9 10 11
  def self.create(*arguments)
    configuration = arguments.first
    class_for_adapter(configuration['adapter']).new(*arguments).create
P
Pat Allan 已提交
12 13
  rescue Exception => error
    $stderr.puts error, *(error.backtrace)
P
Pat Allan 已提交
14 15
    $stderr.puts "Couldn't create database for #{configuration.inspect}"
  end
16

P
Pat Allan 已提交
17 18 19 20
  def self.create_all
    each_local_configuration { |configuration| create configuration }
  end

21 22 23 24 25
  def self.create_current(environment = Rails.env)
    each_current_configuration(environment) { |configuration|
      create configuration
    }
    ActiveRecord::Base.establish_connection environment
P
Pat Allan 已提交
26 27
  end

28 29 30
  def self.drop(*arguments)
    configuration = arguments.first
    class_for_adapter(configuration['adapter']).new(*arguments).drop
P
Pat Allan 已提交
31 32 33 34 35
  rescue Exception => error
    $stderr.puts error, *(error.backtrace)
    $stderr.puts "Couldn't drop #{configuration['database']}"
  end

P
Pat Allan 已提交
36 37 38 39
  def self.drop_all
    each_local_configuration { |configuration| drop configuration }
  end

40 41 42 43
  def self.drop_current(environment = Rails.env)
    each_current_configuration(environment) { |configuration|
      drop configuration
    }
P
Pat Allan 已提交
44 45
  end

P
Pat Allan 已提交
46 47 48 49
  def self.purge(configuration)
    class_for_adapter(configuration['adapter']).new(configuration).purge
  end

P
Pat Allan 已提交
50 51
  private

52
  def self.class_for_adapter(adapter)
53
    key = TASKS_PATTERNS.keys.detect { |pattern| adapter[pattern] }
54 55
    TASKS_PATTERNS[key]
  end
P
Pat Allan 已提交
56

57 58 59
  def self.each_current_configuration(environment)
    environments = [environment]
    environments << 'test' if environment.development?
P
Pat Allan 已提交
60

61
    configurations = ActiveRecord::Base.configurations.values_at(*environments)
P
Pat Allan 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    configurations.compact.each do |configuration|
      yield configuration unless configuration['database'].blank?
    end
  end

  def self.each_local_configuration
    ActiveRecord::Base.configurations.each_value do |configuration|
      next unless configuration['database']

      if local_database?(configuration)
        yield configuration
      else
        $stderr.puts "This task only modifies local databases. #{configuration['database']} is on a remote host."
      end
    end
  end

  def self.local_database?(configuration)
    configuration['host'].in?(LOCAL_HOSTS) || configuration['host'].blank?
  end
P
Pat Allan 已提交
82
end