Skip to content
This repository
Browse code

Merge pull request #6766 from frodsan/refactor_db_tasks

add :nodoc: to AR::Tasks and update to follow the coding conventions
  • Loading branch information...
commit a6cda5416bfb639a035bf5a7d24fa68b6381a540 2 parents b9660bd + 588e80f
Rafael Mendonça França authored June 17, 2012
137  activerecord/lib/active_record/tasks/database_tasks.rb
... ...
@@ -1,82 +1,87 @@
1  
-class ActiveRecord::Tasks::DatabaseTasks
2  
-  TASKS_PATTERNS = {
3  
-    /mysql/      => ActiveRecord::Tasks::MySQLDatabaseTasks,
4  
-    /postgresql/ => ActiveRecord::Tasks::PostgreSQLDatabaseTasks,
5  
-    /sqlite/     => ActiveRecord::Tasks::SQLiteDatabaseTasks
6  
-  }
7  
-  LOCAL_HOSTS    = ['127.0.0.1', 'localhost']
  1
+module ActiveRecord
  2
+  module Tasks # :nodoc:
  3
+    class DatabaseTasks # :nodoc:
8 4
 
9  
-  def self.create(*arguments)
10  
-    configuration = arguments.first
11  
-    class_for_adapter(configuration['adapter']).new(*arguments).create
12  
-  rescue Exception => error
13  
-    $stderr.puts error, *(error.backtrace)
14  
-    $stderr.puts "Couldn't create database for #{configuration.inspect}"
15  
-  end
  5
+      TASKS_PATTERNS = {
  6
+        /mysql/      => ActiveRecord::Tasks::MySQLDatabaseTasks,
  7
+        /postgresql/ => ActiveRecord::Tasks::PostgreSQLDatabaseTasks,
  8
+        /sqlite/     => ActiveRecord::Tasks::SQLiteDatabaseTasks
  9
+      }
  10
+      LOCAL_HOSTS    = ['127.0.0.1', 'localhost']
16 11
 
17  
-  def self.create_all
18  
-    each_local_configuration { |configuration| create configuration }
19  
-  end
  12
+      def self.create(*arguments)
  13
+        configuration = arguments.first
  14
+        class_for_adapter(configuration['adapter']).new(*arguments).create
  15
+      rescue Exception => error
  16
+        $stderr.puts error, *(error.backtrace)
  17
+        $stderr.puts "Couldn't create database for #{configuration.inspect}"
  18
+      end
20 19
 
21  
-  def self.create_current(environment = Rails.env)
22  
-    each_current_configuration(environment) { |configuration|
23  
-      create configuration
24  
-    }
25  
-    ActiveRecord::Base.establish_connection environment
26  
-  end
  20
+      def self.create_all
  21
+        each_local_configuration { |configuration| create configuration }
  22
+      end
27 23
 
28  
-  def self.drop(*arguments)
29  
-    configuration = arguments.first
30  
-    class_for_adapter(configuration['adapter']).new(*arguments).drop
31  
-  rescue Exception => error
32  
-    $stderr.puts error, *(error.backtrace)
33  
-    $stderr.puts "Couldn't drop #{configuration['database']}"
34  
-  end
  24
+      def self.create_current(environment = Rails.env)
  25
+        each_current_configuration(environment) { |configuration|
  26
+          create configuration
  27
+        }
  28
+        ActiveRecord::Base.establish_connection environment
  29
+      end
35 30
 
36  
-  def self.drop_all
37  
-    each_local_configuration { |configuration| drop configuration }
38  
-  end
  31
+      def self.drop(*arguments)
  32
+        configuration = arguments.first
  33
+        class_for_adapter(configuration['adapter']).new(*arguments).drop
  34
+      rescue Exception => error
  35
+        $stderr.puts error, *(error.backtrace)
  36
+        $stderr.puts "Couldn't drop #{configuration['database']}"
  37
+      end
39 38
 
40  
-  def self.drop_current(environment = Rails.env)
41  
-    each_current_configuration(environment) { |configuration|
42  
-      drop configuration
43  
-    }
44  
-  end
  39
+      def self.drop_all
  40
+        each_local_configuration { |configuration| drop configuration }
  41
+      end
45 42
 
46  
-  def self.purge(configuration)
47  
-    class_for_adapter(configuration['adapter']).new(configuration).purge
48  
-  end
  43
+      def self.drop_current(environment = Rails.env)
  44
+        each_current_configuration(environment) { |configuration|
  45
+          drop configuration
  46
+        }
  47
+      end
49 48
 
50  
-  private
  49
+      def self.purge(configuration)
  50
+        class_for_adapter(configuration['adapter']).new(configuration).purge
  51
+      end
51 52
 
52  
-  def self.class_for_adapter(adapter)
53  
-    key = TASKS_PATTERNS.keys.detect { |pattern| adapter[pattern] }
54  
-    TASKS_PATTERNS[key]
55  
-  end
  53
+      private
56 54
 
57  
-  def self.each_current_configuration(environment)
58  
-    environments = [environment]
59  
-    environments << 'test' if environment.development?
  55
+      def self.class_for_adapter(adapter)
  56
+        key = TASKS_PATTERNS.keys.detect { |pattern| adapter[pattern] }
  57
+        TASKS_PATTERNS[key]
  58
+      end
60 59
 
61  
-    configurations = ActiveRecord::Base.configurations.values_at(*environments)
62  
-    configurations.compact.each do |configuration|
63  
-      yield configuration unless configuration['database'].blank?
64  
-    end
65  
-  end
  60
+      def self.each_current_configuration(environment)
  61
+        environments = [environment]
  62
+        environments << 'test' if environment.development?
66 63
 
67  
-  def self.each_local_configuration
68  
-    ActiveRecord::Base.configurations.each_value do |configuration|
69  
-      next unless configuration['database']
  64
+        configurations = ActiveRecord::Base.configurations.values_at(*environments)
  65
+        configurations.compact.each do |configuration|
  66
+          yield configuration unless configuration['database'].blank?
  67
+        end
  68
+      end
  69
+
  70
+      def self.each_local_configuration
  71
+        ActiveRecord::Base.configurations.each_value do |configuration|
  72
+          next unless configuration['database']
70 73
 
71  
-      if local_database?(configuration)
72  
-        yield configuration
73  
-      else
74  
-        $stderr.puts "This task only modifies local databases. #{configuration['database']} is on a remote host."
  74
+          if local_database?(configuration)
  75
+            yield configuration
  76
+          else
  77
+            $stderr.puts "This task only modifies local databases. #{configuration['database']} is on a remote host."
  78
+          end
  79
+        end
75 80
       end
76  
-    end
77  
-  end
78 81
 
79  
-  def self.local_database?(configuration)
80  
-    configuration['host'].in?(LOCAL_HOSTS) || configuration['host'].blank?
  82
+      def self.local_database?(configuration)
  83
+        configuration['host'].in?(LOCAL_HOSTS) || configuration['host'].blank?
  84
+      end
  85
+    end
81 86
   end
82  
-end
  87
+end
145  activerecord/lib/active_record/tasks/mysql_database_tasks.rb
... ...
@@ -1,88 +1,93 @@
1  
-class ActiveRecord::Tasks::MySQLDatabaseTasks
2  
-  DEFAULT_CHARSET     = ENV['CHARSET']   || 'utf8'
3  
-  DEFAULT_COLLATION   = ENV['COLLATION'] || 'utf8_unicode_ci'
4  
-  ACCESS_DENIED_ERROR = 1045
  1
+module ActiveRecord
  2
+  module Tasks # :nodoc:
  3
+    class MySQLDatabaseTasks # :nodoc:
5 4
 
6  
-  delegate :connection, :establish_connection, :to => ActiveRecord::Base
  5
+      DEFAULT_CHARSET     = ENV['CHARSET']   || 'utf8'
  6
+      DEFAULT_COLLATION   = ENV['COLLATION'] || 'utf8_unicode_ci'
  7
+      ACCESS_DENIED_ERROR = 1045
7 8
 
8  
-  def initialize(configuration)
9  
-    @configuration = configuration
10  
-  end
  9
+      delegate :connection, :establish_connection, to: ActiveRecord::Base
11 10
 
12  
-  def create
13  
-    establish_connection configuration_without_database
14  
-    connection.create_database configuration['database'], creation_options
15  
-    establish_connection configuration
16  
-  rescue error_class => error
17  
-    raise error unless error.errno == ACCESS_DENIED_ERROR
  11
+      def initialize(configuration)
  12
+        @configuration = configuration
  13
+      end
18 14
 
19  
-    $stdout.print error.error
20  
-    establish_connection root_configuration_without_database
21  
-    connection.create_database configuration['database'], creation_options
22  
-    connection.execute grant_statement.gsub(/\s+/, ' ').strip
23  
-    establish_connection configuration
24  
-  rescue error_class => error
25  
-    $stderr.puts error.error
26  
-    $stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}"
27  
-    $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['charset']
28  
-  end
  15
+      def create
  16
+        establish_connection configuration_without_database
  17
+        connection.create_database configuration['database'], creation_options
  18
+        establish_connection configuration
  19
+      rescue error_class => error
  20
+        raise error unless error.errno == ACCESS_DENIED_ERROR
29 21
 
30  
-  def drop
31  
-    establish_connection configuration
32  
-    connection.drop_database configuration['database']
33  
-  end
  22
+        $stdout.print error.error
  23
+        establish_connection root_configuration_without_database
  24
+        connection.create_database configuration['database'], creation_options
  25
+        connection.execute grant_statement.gsub(/\s+/, ' ').strip
  26
+        establish_connection configuration
  27
+      rescue error_class => error
  28
+        $stderr.puts error.error
  29
+        $stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}"
  30
+        $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['charset']
  31
+      end
34 32
 
35  
-  def purge
36  
-    establish_connection :test
37  
-    connection.recreate_database configuration['database'], creation_options
38  
-  end
  33
+      def drop
  34
+        establish_connection configuration
  35
+        connection.drop_database configuration['database']
  36
+      end
39 37
 
40  
-  private
  38
+      def purge
  39
+        establish_connection :test
  40
+        connection.recreate_database configuration['database'], creation_options
  41
+      end
41 42
 
42  
-  def configuration
43  
-    @configuration
44  
-  end
  43
+      private
45 44
 
46  
-  def configuration_without_database
47  
-    configuration.merge('database' => nil)
48  
-  end
  45
+      def configuration
  46
+        @configuration
  47
+      end
49 48
 
50  
-  def creation_options
51  
-    {
52  
-      :charset   => (configuration['charset']   || DEFAULT_CHARSET),
53  
-      :collation => (configuration['collation'] || DEFAULT_COLLATION)
54  
-    }
55  
-  end
  49
+      def configuration_without_database
  50
+        configuration.merge('database' => nil)
  51
+      end
56 52
 
57  
-  def error_class
58  
-    case configuration['adapter']
59  
-    when /jdbc/
60  
-      require 'active_record/railties/jdbcmysql_error'
61  
-      ArJdbcMySQL::Error
62  
-    when /mysql2/
63  
-      Mysql2::Error
64  
-    else
65  
-      Mysql::Error
66  
-    end
67  
-  end
  53
+      def creation_options
  54
+        {
  55
+          charset:   (configuration['charset']   || DEFAULT_CHARSET),
  56
+          collation: (configuration['collation'] || DEFAULT_COLLATION)
  57
+        }
  58
+      end
  59
+
  60
+      def error_class
  61
+        case configuration['adapter']
  62
+        when /jdbc/
  63
+          require 'active_record/railties/jdbcmysql_error'
  64
+          ArJdbcMySQL::Error
  65
+        when /mysql2/
  66
+          Mysql2::Error
  67
+        else
  68
+          Mysql::Error
  69
+        end
  70
+      end
68 71
 
69  
-  def grant_statement
70  
-    <<-SQL
  72
+      def grant_statement
  73
+        <<-SQL
71 74
 GRANT ALL PRIVILEGES ON #{configuration['database']}.*
72 75
   TO '#{configuration['username']}'@'localhost'
73 76
 IDENTIFIED BY '#{configuration['password']}' WITH GRANT OPTION;
74  
-    SQL
75  
-  end
  77
+        SQL
  78
+      end
76 79
 
77  
-  def root_configuration_without_database
78  
-    configuration_without_database.merge(
79  
-      'username' => 'root',
80  
-      'password' => root_password
81  
-    )
82  
-  end
  80
+      def root_configuration_without_database
  81
+        configuration_without_database.merge(
  82
+          'username' => 'root',
  83
+          'password' => root_password
  84
+        )
  85
+      end
83 86
 
84  
-  def root_password
85  
-    $stdout.print "Please provide the root password for your mysql installation\n>"
86  
-    $stdin.gets.strip
  87
+      def root_password
  88
+        $stdout.print "Please provide the root password for your mysql installation\n>"
  89
+        $stdin.gets.strip
  90
+      end
  91
+    end
87 92
   end
88  
-end
  93
+end
93  activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
... ...
@@ -1,45 +1,50 @@
1  
-class ActiveRecord::Tasks::PostgreSQLDatabaseTasks
2  
-  DEFAULT_ENCODING = ENV['CHARSET'] || 'utf8'
3  
-
4  
-  delegate :connection, :establish_connection, :clear_active_connections!,
5  
-    :to => ActiveRecord::Base
6  
-
7  
-  def initialize(configuration)
8  
-    @configuration = configuration
9  
-  end
10  
-
11  
-  def create(master_established = false)
12  
-    establish_master_connection unless master_established
13  
-    connection.create_database configuration['database'],
14  
-      configuration.merge('encoding' => encoding)
15  
-    establish_connection configuration
16  
-  end
17  
-
18  
-  def drop
19  
-    establish_master_connection
20  
-    connection.drop_database configuration['database']
21  
-  end
22  
-
23  
-  def purge
24  
-    clear_active_connections!
25  
-    drop
26  
-    create true
27  
-  end
28  
-
29  
-  private
30  
-
31  
-  def configuration
32  
-    @configuration
33  
-  end
34  
-
35  
-  def encoding
36  
-    configuration['encoding'] || DEFAULT_ENCODING
37  
-  end
38  
-
39  
-  def establish_master_connection
40  
-    establish_connection configuration.merge(
41  
-      'database'           => 'postgres',
42  
-      'schema_search_path' => 'public'
43  
-    )
  1
+module ActiveRecord
  2
+  module Tasks # :nodoc:
  3
+    class PostgreSQLDatabaseTasks # :nodoc:
  4
+
  5
+      DEFAULT_ENCODING = ENV['CHARSET'] || 'utf8'
  6
+
  7
+      delegate :connection, :establish_connection, :clear_active_connections!,
  8
+        to: ActiveRecord::Base
  9
+
  10
+      def initialize(configuration)
  11
+        @configuration = configuration
  12
+      end
  13
+
  14
+      def create(master_established = false)
  15
+        establish_master_connection unless master_established
  16
+        connection.create_database configuration['database'],
  17
+          configuration.merge('encoding' => encoding)
  18
+        establish_connection configuration
  19
+      end
  20
+
  21
+      def drop
  22
+        establish_master_connection
  23
+        connection.drop_database configuration['database']
  24
+      end
  25
+
  26
+      def purge
  27
+        clear_active_connections!
  28
+        drop
  29
+        create true
  30
+      end
  31
+
  32
+      private
  33
+
  34
+      def configuration
  35
+        @configuration
  36
+      end
  37
+
  38
+      def encoding
  39
+        configuration['encoding'] || DEFAULT_ENCODING
  40
+      end
  41
+
  42
+      def establish_master_connection
  43
+        establish_connection configuration.merge(
  44
+          'database'           => 'postgres',
  45
+          'schema_search_path' => 'public'
  46
+        )
  47
+      end
  48
+    end
44 49
   end
45  
-end
  50
+end
59  activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
... ...
@@ -1,37 +1,40 @@
1  
-class ActiveRecord::Tasks::SQLiteDatabaseTasks
2  
-  delegate :connection, :establish_connection, to: ActiveRecord::Base
  1
+module ActiveRecord
  2
+  module Tasks # :nodoc:
  3
+    class SQLiteDatabaseTasks # :nodoc:
3 4
 
4  
-  def initialize(configuration, root = Rails.root)
5  
-    @configuration, @root = configuration, root
6  
-  end
  5
+      delegate :connection, :establish_connection, to: ActiveRecord::Base
7 6
 
8  
-  def create
9  
-    if File.exist?(configuration['database'])
10  
-      $stderr.puts "#{configuration['database']} already exists"
11  
-      return
12  
-    end
  7
+      def initialize(configuration, root = Rails.root)
  8
+        @configuration, @root = configuration, root
  9
+      end
13 10
 
14  
-    establish_connection configuration
15  
-    connection
16  
-  end
  11
+      def create
  12
+        if File.exist?(configuration['database'])
  13
+          $stderr.puts "#{configuration['database']} already exists"
  14
+          return
  15
+        end
17 16
 
18  
-  def drop
19  
-    require 'pathname'
20  
-    path = Pathname.new configuration['database']
21  
-    file = path.absolute? ? path.to_s : File.join(root, path)
  17
+        establish_connection configuration
  18
+        connection
  19
+      end
22 20
 
23  
-    FileUtils.rm(file) if File.exist?(file)
24  
-  end
  21
+      def drop
  22
+        require 'pathname'
  23
+        path = Pathname.new configuration['database']
  24
+        file = path.absolute? ? path.to_s : File.join(root, path)
25 25
 
26  
-  alias :purge :drop
  26
+        FileUtils.rm(file) if File.exist?(file)
  27
+      end
27 28
 
28  
-  private
  29
+      alias :purge :drop
29 30
 
30  
-  def configuration
31  
-    @configuration
32  
-  end
  31
+      private
33 32
 
34  
-  def root
35  
-    @root
36  
-  end
37  
-end
  33
+      def configuration
  34
+        @configuration
  35
+      end
  36
+
  37
+      def root
  38
+        @root
  39
+      end
  40
+    end

0 notes on commit a6cda54

Please sign in to comment.
Something went wrong with that request. Please try again.