database_tasks.rb 2.8 KB
Newer Older
1 2 3
module ActiveRecord
  module Tasks # :nodoc:
    class DatabaseTasks # :nodoc:
P
Pat Allan 已提交
4

5 6 7 8 9 10
      TASKS_PATTERNS = {
        /mysql/      => ActiveRecord::Tasks::MySQLDatabaseTasks,
        /postgresql/ => ActiveRecord::Tasks::PostgreSQLDatabaseTasks,
        /sqlite/     => ActiveRecord::Tasks::SQLiteDatabaseTasks
      }
      LOCAL_HOSTS    = ['127.0.0.1', 'localhost']
11

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

20 21 22
      def self.create_all
        each_local_configuration { |configuration| create configuration }
      end
P
Pat Allan 已提交
23

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

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

39 40 41
      def self.drop_all
        each_local_configuration { |configuration| drop configuration }
      end
P
Pat Allan 已提交
42

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

49 50 51
      def self.purge(configuration)
        class_for_adapter(configuration['adapter']).new(configuration).purge
      end
P
Pat Allan 已提交
52

53
      private
P
Pat Allan 已提交
54

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

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

64 65 66 67 68 69 70 71 72
        configurations = ActiveRecord::Base.configurations.values_at(*environments)
        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']
P
Pat Allan 已提交
73

74 75 76 77 78 79
          if local_database?(configuration)
            yield configuration
          else
            $stderr.puts "This task only modifies local databases. #{configuration['database']} is on a remote host."
          end
        end
P
Pat Allan 已提交
80 81
      end

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