From 4b030f44be97087180855959fafb09438f44956a Mon Sep 17 00:00:00 2001 From: mateusz_kmiecinski Date: Thu, 25 Sep 2014 18:20:44 +0200 Subject: [PATCH 1/4] Add rake task to remove existing seed files --- lib/sprig.rb | 2 ++ lib/sprig/railtie.rb | 7 +++++++ lib/tasks/sprig.rake | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 lib/sprig/railtie.rb create mode 100644 lib/tasks/sprig.rake diff --git a/lib/sprig.rb b/lib/sprig.rb index 8658178..033c730 100644 --- a/lib/sprig.rb +++ b/lib/sprig.rb @@ -36,3 +36,5 @@ def logger end end end + +require 'sprig/railtie' if defined?(Rails) diff --git a/lib/sprig/railtie.rb b/lib/sprig/railtie.rb new file mode 100644 index 0000000..44016d9 --- /dev/null +++ b/lib/sprig/railtie.rb @@ -0,0 +1,7 @@ +module Sprig + class Railtie < Rails::Railtie + rake_tasks do + load "tasks/sprig.rake" + end + end +end diff --git a/lib/tasks/sprig.rake b/lib/tasks/sprig.rake new file mode 100644 index 0000000..f23fb9a --- /dev/null +++ b/lib/tasks/sprig.rake @@ -0,0 +1,43 @@ +namespace :db do + namespace :seed do + + desc 'Purge seed files' + task :purge do + check_args + list_seed_files + delete_seed_files if deletion_confirmed? + end + + def deletion_confirmed? + puts "\nAre you sure? (yes/no)" + input = STDIN.gets.chomp + input == 'yes' ? true : false + end + + def check_args + unless ENV['ENVIRONMENT'] + abort('Expected environment argument e.g. ENVIRONMENT=development. Aborting.') + end + end + + def list_seed_files + filenames = seed_filenames + if filenames.empty? + abort('No files to delete. Aborting') + else + puts 'Files to be deleted:' + filenames.each { |filename| puts "#{File.basename(filename)}" } + end + end + + def delete_seed_files + seed_filenames.each { |filename| File.delete(filename) } + puts 'Seed files deleted.' + end + + def seed_filenames + seed_files_path = Rails.root.join('db', 'seeds', ENV['ENVIRONMENT']) + Dir.glob("#{seed_files_path}/*.{yml,yaml,json,csv}") + end + end +end From d5334bd462cb9f5434b904b14708311c8969e51f Mon Sep 17 00:00:00 2001 From: mateusz_kmiecinski Date: Thu, 25 Sep 2014 19:34:16 +0200 Subject: [PATCH 2/4] Change globbing files to be removed by rake task to all --- lib/tasks/sprig.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/sprig.rake b/lib/tasks/sprig.rake index f23fb9a..e089663 100644 --- a/lib/tasks/sprig.rake +++ b/lib/tasks/sprig.rake @@ -37,7 +37,7 @@ namespace :db do def seed_filenames seed_files_path = Rails.root.join('db', 'seeds', ENV['ENVIRONMENT']) - Dir.glob("#{seed_files_path}/*.{yml,yaml,json,csv}") + Dir.glob("#{seed_files_path}/*") end end end From b343e7bb44a38d0f31649ed4b63bda697aae1d25 Mon Sep 17 00:00:00 2001 From: Lawson Kurtz Date: Thu, 30 Oct 2014 22:51:43 -0600 Subject: [PATCH 3/4] Refactor seed purge task, add tests --- lib/sprig.rb | 1 + lib/sprig/task.rb | 5 +++ lib/sprig/task/purge_seeds.rb | 55 +++++++++++++++++++++++++ lib/tasks/sprig.rake | 37 +---------------- spec/lib/sprig/task/purge_seeds_spec.rb | 51 +++++++++++++++++++++++ spec/lib/tasks/sprig_rake_spec.rb | 16 +++++++ spec/spec_helper.rb | 9 ++++ spec/support/shared_context/rake.rb | 20 +++++++++ 8 files changed, 159 insertions(+), 35 deletions(-) create mode 100644 lib/sprig/task.rb create mode 100644 lib/sprig/task/purge_seeds.rb create mode 100644 spec/lib/sprig/task/purge_seeds_spec.rb create mode 100644 spec/lib/tasks/sprig_rake_spec.rb create mode 100644 spec/support/shared_context/rake.rb diff --git a/lib/sprig.rb b/lib/sprig.rb index 033c730..dff1f22 100644 --- a/lib/sprig.rb +++ b/lib/sprig.rb @@ -17,6 +17,7 @@ module Sprig autoload :SprigRecordStore, 'sprig/sprig_record_store' autoload :Data, 'sprig/data' autoload :Seed, 'sprig/seed' + autoload :Task, 'sprig/task' class << self def configuration diff --git a/lib/sprig/task.rb b/lib/sprig/task.rb new file mode 100644 index 0000000..d87fe12 --- /dev/null +++ b/lib/sprig/task.rb @@ -0,0 +1,5 @@ +module Sprig + module Task + autoload :PurgeSeeds, 'sprig/task/purge_seeds' + end +end diff --git a/lib/sprig/task/purge_seeds.rb b/lib/sprig/task/purge_seeds.rb new file mode 100644 index 0000000..aa89ca4 --- /dev/null +++ b/lib/sprig/task/purge_seeds.rb @@ -0,0 +1,55 @@ +module Sprig + module Task + class PurgeSeeds + include Logging + + def perform + log_info "Preparing to purge seeds from the #{Rails.env} environment. To purge a different environment, specify the environment as an environment variable (e.g. RAILS_ENV=production)." + stop 'No seed files to delete. Aborting.' unless seed_files.any? + list_seed_files + stop 'Purge aborted.'unless deletion_confirmed? + delete_seed_files + log_info 'Seed files deleted.' + end + + private + + def stop(error_message) + log_error error_message + abort + end + + def list_seed_files + log_debug 'Files to be deleted:' + seed_files.each do |filename| + log_debug "- #{File.basename(filename)}" + end + end + + def seed_files + @seed_files ||= Dir.glob("#{seed_directory}/*") + end + + def seed_directory + Sprig.configuration.directory + end + + def deletion_confirmed? + log_debug "Are you sure you want to delete these files? (Type 'yes' to confirm.)" + input = $stdin.gets.chomp + input == 'yes' ? true : false + end + + def delete_seed_files + seed_files.each do |file| + if File.directory?(file) + FileUtils.rm_rf(file) + else + File.delete(file) + end + end + end + + end + end +end diff --git a/lib/tasks/sprig.rake b/lib/tasks/sprig.rake index e089663..6ed3e40 100644 --- a/lib/tasks/sprig.rake +++ b/lib/tasks/sprig.rake @@ -1,43 +1,10 @@ namespace :db do namespace :seed do - desc 'Purge seed files' + desc 'Purge seed files for specified environment' task :purge do - check_args - list_seed_files - delete_seed_files if deletion_confirmed? + Sprig::Task::PurgeSeeds.new.perform end - def deletion_confirmed? - puts "\nAre you sure? (yes/no)" - input = STDIN.gets.chomp - input == 'yes' ? true : false - end - - def check_args - unless ENV['ENVIRONMENT'] - abort('Expected environment argument e.g. ENVIRONMENT=development. Aborting.') - end - end - - def list_seed_files - filenames = seed_filenames - if filenames.empty? - abort('No files to delete. Aborting') - else - puts 'Files to be deleted:' - filenames.each { |filename| puts "#{File.basename(filename)}" } - end - end - - def delete_seed_files - seed_filenames.each { |filename| File.delete(filename) } - puts 'Seed files deleted.' - end - - def seed_filenames - seed_files_path = Rails.root.join('db', 'seeds', ENV['ENVIRONMENT']) - Dir.glob("#{seed_files_path}/*") - end end end diff --git a/spec/lib/sprig/task/purge_seeds_spec.rb b/spec/lib/sprig/task/purge_seeds_spec.rb new file mode 100644 index 0000000..cb0468f --- /dev/null +++ b/spec/lib/sprig/task/purge_seeds_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe Sprig::Task::PurgeSeeds do + subject { described_class.new } + + before do + stub_rails_root + end + + describe "#perform" do + context "when no seed files are present" do + it "aborts with an error message" do + log_should_receive :error, with: 'No seed files to delete. Aborting.' + + expect { + subject.perform + }.to raise_error SystemExit + end + end + + context "when seed files are present" do + around do |example| + load_seeds('posts.yml', &example) + end + + context "and deletion is not confirmed" do + it "aborts with an error message" do + log_should_receive :error, with: 'Purge aborted.' + + expect { + fake_stdin_gets 'no' do + subject.perform + end + }.to raise_error SystemExit + end + end + + context "and deletion is confirmed" do + it "deletes the seed files" do + Dir.glob("#{Sprig.configuration.directory}/*").should_not be_empty + + fake_stdin_gets 'yes' do + subject.perform + end + + Dir.glob("#{Sprig.configuration.directory}/*").should be_empty + end + end + end + end +end diff --git a/spec/lib/tasks/sprig_rake_spec.rb b/spec/lib/tasks/sprig_rake_spec.rb new file mode 100644 index 0000000..c8be61b --- /dev/null +++ b/spec/lib/tasks/sprig_rake_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe "db:seed:purge" do + include_context "rake" + + let(:task) { double('Sprig::Task::PurgeSeeds') } + + before do + Sprig::Task::PurgeSeeds.stub(:new).and_return(task) + end + + it "purges seed files" do + task.should_receive(:perform) + subject.invoke + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a195cd5..76732e1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -94,3 +94,12 @@ def load_seeds(*files) `rm ./spec/fixtures/db/seeds/#{env}/#{file}` end end + +def fake_stdin_gets(input) + begin + $stdin = double('STDIN', gets: input) + yield + ensure + $stdin = STDIN + end +end diff --git a/spec/support/shared_context/rake.rb b/spec/support/shared_context/rake.rb new file mode 100644 index 0000000..b2ae604 --- /dev/null +++ b/spec/support/shared_context/rake.rb @@ -0,0 +1,20 @@ +require "rake" + +shared_context "rake" do + let(:rake) { Rake::Application.new } + let(:task_name) { self.class.top_level_description } + let(:task_path) { "lib/tasks/sprig" } + subject { rake[task_name] } + + def loaded_files_excluding_current_rake_file + $".reject {|file| file == Rails.root.join("#{task_path}.rake").to_s } + end + + before do + Rails.stub(:root).and_return(Pathname.new("#{File.dirname(__FILE__)}/../../..")) + Rake.application = rake + Rake.application.rake_require(task_path, [Rails.root.to_s], loaded_files_excluding_current_rake_file) + + Rake::Task.define_task(:environment) + end +end From a170852d9b8a3f8f67d4dbdd5dc493c89126e83d Mon Sep 17 00:00:00 2001 From: Lawson Kurtz Date: Thu, 30 Oct 2014 23:25:44 -0600 Subject: [PATCH 4/4] Test Railtie --- lib/sprig.rb | 1 + spec/lib/sprig/railtie_spec.rb | 7 +++++++ spec/support/shared_context/rake.rb | 3 +-- 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 spec/lib/sprig/railtie_spec.rb diff --git a/lib/sprig.rb b/lib/sprig.rb index dff1f22..715075f 100644 --- a/lib/sprig.rb +++ b/lib/sprig.rb @@ -18,6 +18,7 @@ module Sprig autoload :Data, 'sprig/data' autoload :Seed, 'sprig/seed' autoload :Task, 'sprig/task' + autoload :Railtie, 'sprig/railtie' class << self def configuration diff --git a/spec/lib/sprig/railtie_spec.rb b/spec/lib/sprig/railtie_spec.rb new file mode 100644 index 0000000..b9eec3f --- /dev/null +++ b/spec/lib/sprig/railtie_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe Sprig::Railtie do + subject { described_class.instance } + + its(:railtie_name) { should == 'sprig_railtie'} +end diff --git a/spec/support/shared_context/rake.rb b/spec/support/shared_context/rake.rb index b2ae604..93f2f42 100644 --- a/spec/support/shared_context/rake.rb +++ b/spec/support/shared_context/rake.rb @@ -13,8 +13,7 @@ def loaded_files_excluding_current_rake_file before do Rails.stub(:root).and_return(Pathname.new("#{File.dirname(__FILE__)}/../../..")) Rake.application = rake - Rake.application.rake_require(task_path, [Rails.root.to_s], loaded_files_excluding_current_rake_file) - + Sprig::Railtie.instance.load_tasks Rake::Task.define_task(:environment) end end