SHARE
TWEET

Untitled

a guest May 1st, 2016 214 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. task spec: ['stats:db:test:prepare']
  2.  
  3. namespace :stats do
  4.   namespace :db do |ns|
  5.     %i(drop create setup migrate rollback seed version).each do |task_name|
  6.       task task_name do
  7.         Rake::Task["db:#{task_name}"].invoke
  8.       end
  9.     end
  10.  
  11.     namespace :schema do
  12.       %i(load dump).each do |task_name|
  13.         task task_name do
  14.           Rake::Task["db:schema:#{task_name}"].invoke
  15.         end
  16.       end
  17.     end
  18.  
  19.     namespace :test do
  20.       task :prepare do
  21.         Rake::Task['db:test:prepare'].invoke
  22.       end
  23.     end
  24.  
  25.     # append and prepend proper tasks to all the tasks defined here above
  26.     ns.tasks.each do |task|
  27.       task.enhance ['stats:set_custom_config'] do
  28.         Rake::Task['stats:revert_to_original_config'].invoke
  29.       end
  30.     end
  31.   end
  32.  
  33.   task :set_custom_config do
  34.     # save current vars
  35.     @original_config = {
  36.       env_schema: ENV['SCHEMA'],
  37.       config: Rails.application.config.dup
  38.     }
  39.  
  40.     # set config variables for custom database
  41.     ENV['SCHEMA'] = 'db_stats/schema.rb'
  42.     Rails.application.config.paths['db'] = ['db_stats']
  43.     Rails.application.config.paths['db/migrate'] = ['db_stats/migrate']
  44.     Rails.application.config.paths['db/seeds'] = ['db_stats/seeds.rb']
  45.     Rails.application.config.paths['config/database'] = ['config/database_stats.yml']
  46.   end
  47.  
  48.   task :revert_to_original_config do
  49.     # reset config variables to original values
  50.     ENV['SCHEMA'] = @original_config[:env_schema]
  51.     Rails.application.config = @original_config[:config]
  52.   end
  53. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top