database_tasks.rb 3.0 KB
Newer Older
1 2
module ActiveRecord
  module Tasks # :nodoc:
3 4
    module DatabaseTasks # :nodoc:
      extend self
P
Pat Allan 已提交
5

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

13
      def create(*arguments)
14 15 16 17 18 19
        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 已提交
20

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

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

32
      def drop(*arguments)
33 34 35 36 37 38
        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 已提交
39

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

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

S
Simon Jefford 已提交
50 51 52 53 54 55 56 57 58
      def charset_current(environment = Rails.env)
        charset ActiveRecord::Base.configurations[environment]
      end

      def charset(*arguments)
        configuration = arguments.first
        class_for_adapter(configuration['adapter']).new(*arguments).charset
      end

59
      def purge(configuration)
60 61
        class_for_adapter(configuration['adapter']).new(configuration).purge
      end
P
Pat Allan 已提交
62

63
      private
P
Pat Allan 已提交
64

65
      def class_for_adapter(adapter)
66 67 68
        key = TASKS_PATTERNS.keys.detect { |pattern| adapter[pattern] }
        TASKS_PATTERNS[key]
      end
P
Pat Allan 已提交
69

70
      def each_current_configuration(environment)
71 72
        environments = [environment]
        environments << 'test' if environment.development?
P
Pat Allan 已提交
73

74 75 76 77 78 79
        configurations = ActiveRecord::Base.configurations.values_at(*environments)
        configurations.compact.each do |configuration|
          yield configuration unless configuration['database'].blank?
        end
      end

80
      def each_local_configuration
81 82
        ActiveRecord::Base.configurations.each_value do |configuration|
          next unless configuration['database']
P
Pat Allan 已提交
83

84 85 86 87 88 89
          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 已提交
90 91
      end

92
      def local_database?(configuration)
93 94 95
        configuration['host'].in?(LOCAL_HOSTS) || configuration['host'].blank?
      end
    end
P
Pat Allan 已提交
96
  end
97
end