diff --git a/lib/db_charmer.rb b/lib/db_charmer.rb index c773c50..3898340 100644 --- a/lib/db_charmer.rb +++ b/lib/db_charmer.rb @@ -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) diff --git a/lib/db_charmer/railtie.rb b/lib/db_charmer/railtie.rb new file mode 100644 index 0000000..ba82997 --- /dev/null +++ b/lib/db_charmer/railtie.rb @@ -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 \ No newline at end of file diff --git a/lib/tasks/databases.rake b/lib/tasks/databases.rake index cc9ce5b..5485cae 100644 --- a/lib/tasks/databases.rake +++ b/lib/tasks/databases.rake @@ -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 @@ -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 @@ -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