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
0 commit comments