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

6
      LOCAL_HOSTS    = ['127.0.0.1', 'localhost']
7

8 9 10 11 12 13 14 15 16
      def register_task(pattern, task)
        @tasks ||= {}
        @tasks[pattern] = task
      end

      register_task(/mysql/, ActiveRecord::Tasks::MySQLDatabaseTasks)
      register_task(/postgresql/, ActiveRecord::Tasks::PostgreSQLDatabaseTasks)
      register_task(/sqlite/, ActiveRecord::Tasks::SQLiteDatabaseTasks)

17
      def create(*arguments)
18 19 20 21 22 23
        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 已提交
24

25
      def create_all
26 27
        each_local_configuration { |configuration| create configuration }
      end
P
Pat Allan 已提交
28

29
      def create_current(environment = Rails.env)
30 31 32 33 34
        each_current_configuration(environment) { |configuration|
          create configuration
        }
        ActiveRecord::Base.establish_connection environment
      end
P
Pat Allan 已提交
35

36
      def drop(*arguments)
37 38 39 40 41 42
        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 已提交
43

44
      def drop_all
45 46
        each_local_configuration { |configuration| drop configuration }
      end
P
Pat Allan 已提交
47

48
      def drop_current(environment = Rails.env)
49 50 51 52
        each_current_configuration(environment) { |configuration|
          drop configuration
        }
      end
P
Pat Allan 已提交
53

S
Simon Jefford 已提交
54 55 56 57 58 59 60 61 62
      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

63 64 65 66 67 68 69 70 71
      def collation_current(environment = Rails.env)
        collation ActiveRecord::Base.configurations[environment]
      end

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

72
      def purge(configuration)
73 74
        class_for_adapter(configuration['adapter']).new(configuration).purge
      end
P
Pat Allan 已提交
75

K
kennyj 已提交
76 77 78 79 80 81
      def structure_dump(*arguments)
        configuration = arguments.first
        filename = arguments.delete_at 1
        class_for_adapter(configuration['adapter']).new(*arguments).structure_dump(filename)
      end

K
kennyj 已提交
82 83 84 85 86 87
      def structure_load(*arguments)
        configuration = arguments.first
        filename = arguments.delete_at 1
        class_for_adapter(configuration['adapter']).new(*arguments).structure_load(filename)
      end

88
      private
P
Pat Allan 已提交
89

90
      def class_for_adapter(adapter)
91 92
        key = @tasks.keys.detect { |pattern| adapter[pattern] }
        @tasks[key]
93
      end
P
Pat Allan 已提交
94

95
      def each_current_configuration(environment)
96 97
        environments = [environment]
        environments << 'test' if environment.development?
P
Pat Allan 已提交
98

99 100 101 102 103 104
        configurations = ActiveRecord::Base.configurations.values_at(*environments)
        configurations.compact.each do |configuration|
          yield configuration unless configuration['database'].blank?
        end
      end

105
      def each_local_configuration
106 107
        ActiveRecord::Base.configurations.each_value do |configuration|
          next unless configuration['database']
P
Pat Allan 已提交
108

109 110 111 112 113 114
          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 已提交
115 116
      end

117
      def local_database?(configuration)
118
        configuration['host'].blank? || LOCAL_HOSTS.include?(configuration['host'])
119 120
      end
    end
P
Pat Allan 已提交
121
  end
122
end