From 4b030f44be97087180855959fafb09438f44956a Mon Sep 17 00:00:00 2001 From: mateusz_kmiecinski Date: Thu, 25 Sep 2014 18:20:44 +0200 Subject: [PATCH 1/2] 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/2] 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