db-charmer 1.4.3 → 1.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.3
1
+ 1.4.4
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{db-charmer}
8
- s.version = "1.4.3"
8
+ s.version = "1.4.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexey Kovyrin"]
@@ -34,7 +34,8 @@ Gem::Specification.new do |s|
34
34
  "lib/db_charmer/finder_overrides.rb",
35
35
  "lib/db_charmer/multi_db_migrations.rb",
36
36
  "lib/db_charmer/multi_db_proxy.rb",
37
- "lib/db_charmer/scope_proxy.rb"
37
+ "lib/db_charmer/scope_proxy.rb",
38
+ "lib/tasks/databases.rake"
38
39
  ]
39
40
  s.homepage = %q{http://github.com/kovyrin/db-charmer}
40
41
  s.rdoc_options = ["--charset=UTF-8"]
@@ -24,6 +24,12 @@ module DbCharmer
24
24
  EOF
25
25
  end
26
26
 
27
+ def find(*args, &block)
28
+ options = args.extract_options!
29
+ on_master { super(*args, &block) } if options[:lock]
30
+ super(*args, &block)
31
+ end
32
+
27
33
  private
28
34
 
29
35
  def first_level_on_slave
@@ -0,0 +1,82 @@
1
+ namespace :db_charmer do
2
+ namespace :create do
3
+ desc 'Create all the local databases defined in config/database.yml'
4
+ task :all => "db:load_config" do
5
+ ActiveRecord::Base.configurations.each_value do |config|
6
+ # Skip entries that don't have a database key, such as the first entry here:
7
+ #
8
+ # defaults: &defaults
9
+ # adapter: mysql
10
+ # username: root
11
+ # password:
12
+ # host: localhost
13
+ #
14
+ # development:
15
+ # database: blog_development
16
+ # <<: *defaults
17
+ next unless config['database']
18
+ # Only connect to local databases
19
+ local_database?(config) { create_core_and_sub_database(config) }
20
+ end
21
+ end
22
+ end
23
+
24
+ desc 'Create the databases defined in config/database.yml for the current RAILS_ENV'
25
+ task :create => "db:load_config" do
26
+ create_core_and_sub_database(ActiveRecord::Base.configurations[RAILS_ENV])
27
+ end
28
+
29
+ def create_core_and_sub_database(config)
30
+ create_database(config)
31
+ config.each_value do | sub_config |
32
+ next unless sub_config.is_a?(Hash)
33
+ next unless sub_config['database']
34
+ create_database(sub_config)
35
+ end
36
+ end
37
+
38
+ namespace :drop do
39
+ desc 'Drops all the local databases defined in config/database.yml'
40
+ task :all => "db:load_config" do
41
+ ActiveRecord::Base.configurations.each_value do |config|
42
+ # Skip entries that don't have a database key
43
+ next unless config['database']
44
+ # Only connect to local databases
45
+ local_database?(config) { drop_core_and_sub_database(config) }
46
+ end
47
+ end
48
+ end
49
+
50
+ desc 'Drops the database for the current RAILS_ENV'
51
+ task :drop => "db:load_config" do
52
+ config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
53
+ begin
54
+ drop_core_and_sub_database(config)
55
+ rescue Exception => e
56
+ puts "Couldn't drop #{config['database']} : #{e.inspect}"
57
+ end
58
+ end
59
+
60
+
61
+ def local_database?(config, &block)
62
+ if %w( 127.0.0.1 localhost ).include?(config['host']) || config['host'].blank?
63
+ yield
64
+ else
65
+ puts "This task only modifies local databases. #{config['database']} is on a remote host."
66
+ end
67
+ end
68
+ end
69
+
70
+ def drop_core_and_sub_database(config)
71
+ drop_database(config)
72
+ config.each_value do | sub_config |
73
+ next unless sub_config.is_a?(Hash)
74
+ next unless sub_config['database']
75
+ begin
76
+ drop_database(sub_config)
77
+ rescue
78
+ $stderr.puts "#{config['database']} not exists"
79
+ end
80
+ end
81
+ end
82
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-charmer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 1.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Kovyrin
@@ -50,6 +50,7 @@ files:
50
50
  - lib/db_charmer/multi_db_migrations.rb
51
51
  - lib/db_charmer/multi_db_proxy.rb
52
52
  - lib/db_charmer/scope_proxy.rb
53
+ - lib/tasks/databases.rake
53
54
  has_rdoc: true
54
55
  homepage: http://github.com/kovyrin/db-charmer
55
56
  licenses: []