database_tasks.rb 3.2 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

K
kennyj 已提交
63 64 65 66 67 68
      def structure_dump(*arguments)
        configuration = arguments.first
        filename = arguments.delete_at 1
        class_for_adapter(configuration['adapter']).new(*arguments).structure_dump(filename)
      end

69
      private
P
Pat Allan 已提交
70

71
      def class_for_adapter(adapter)
72 73 74
        key = TASKS_PATTERNS.keys.detect { |pattern| adapter[pattern] }
        TASKS_PATTERNS[key]
      end
P
Pat Allan 已提交
75

76
      def each_current_configuration(environment)
77 78
        environments = [environment]
        environments << 'test' if environment.development?
P
Pat Allan 已提交
79

80 81 82 83 84 85
        configurations = ActiveRecord::Base.configurations.values_at(*environments)
        configurations.compact.each do |configuration|
          yield configuration unless configuration['database'].blank?
        end
      end

86
      def each_local_configuration
87 88
        ActiveRecord::Base.configurations.each_value do |configuration|
          next unless configuration['database']
P
Pat Allan 已提交
89

90 91 92 93 94 95
          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 已提交
96 97
      end

98
      def local_database?(configuration)
99 100 101
        configuration['host'].in?(LOCAL_HOSTS) || configuration['host'].blank?
      end
    end
P
Pat Allan 已提交
102
  end
103
end