Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the rake tasks so that they work in Rails 3 and added an option to exclude db's from Rake. #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/db_charmer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,6 @@ def inherited_with_hijacking(subclass)
alias_method_chain :inherited, :hijacking
end
end

# Include the railtie file so that rake tasks are added
require 'db_charmer/railtie' if defined?(Rails)
11 changes: 11 additions & 0 deletions lib/db_charmer/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'db_charmer'
require 'rails'
module DbCharmer
class Railtie < Rails::Railtie
railtie_name :db_charmer

rake_tasks do
load "tasks/databases.rake"
end
end
end
23 changes: 13 additions & 10 deletions lib/tasks/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ namespace :db_charmer do
end
end

desc 'Create the databases defined in config/database.yml for the current RAILS_ENV'
desc 'Create the databases defined in config/database.yml based on RAILS_ENV.'
task :create => "db:load_config" do
create_core_and_sub_database(ActiveRecord::Base.configurations[RAILS_ENV])
configs_for_environment.each { |config| create_core_and_sub_database(config) }
end

def create_core_and_sub_database(config)
create_database(config)
create_database(config) unless config['exclude_from_rake_create_drop']
config.each_value do | sub_config |
next unless sub_config.is_a?(Hash)
next unless sub_config['database']
next if sub_config['exclude_from_rake_create_drop']
create_database(sub_config)
end
end
Expand All @@ -47,13 +48,14 @@ namespace :db_charmer do
end
end

desc 'Drops the database for the current RAILS_ENV'
desc 'Drops the database based on RAILS_ENV'
task :drop => "db:load_config" do
config = ::ActiveRecord::Base.configurations[RAILS_ENV || 'development']
begin
drop_core_and_sub_database(config)
rescue Exception => e
puts "Couldn't drop #{config['database']} : #{e.inspect}"
configs_for_environment.each do |config|
begin
drop_core_and_sub_database(config)
rescue Exception => e
puts "Couldn't drop #{config['database']} : #{e.inspect}"
end
end
end

Expand All @@ -68,10 +70,11 @@ namespace :db_charmer do
end

def drop_core_and_sub_database(config)
drop_database(config)
drop_database(config) unless config['exclude_from_rake_create_drop']
config.each_value do | sub_config |
next unless sub_config.is_a?(Hash)
next unless sub_config['database']
next if sub_config['exclude_from_rake_create_drop']
begin
drop_database(sub_config)
rescue
Expand Down