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

Issue #34 - Add rake task to remove existing seed files #48

Open
wants to merge 4 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
4 changes: 4 additions & 0 deletions lib/sprig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module Sprig
autoload :SprigRecordStore, 'sprig/sprig_record_store'
autoload :Data, 'sprig/data'
autoload :Seed, 'sprig/seed'
autoload :Task, 'sprig/task'
autoload :Railtie, 'sprig/railtie'

class << self
def configuration
Expand All @@ -36,3 +38,5 @@ def logger
end
end
end

require 'sprig/railtie' if defined?(Rails)
7 changes: 7 additions & 0 deletions lib/sprig/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Sprig
class Railtie < Rails::Railtie
rake_tasks do
load "tasks/sprig.rake"
end
end
end
5 changes: 5 additions & 0 deletions lib/sprig/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Sprig
module Task
autoload :PurgeSeeds, 'sprig/task/purge_seeds'
end
end
55 changes: 55 additions & 0 deletions lib/sprig/task/purge_seeds.rb
Original file line number Diff line number Diff line change
@@ -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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a space before unless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surprised that still works. cool.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need the ternary here. input == 'yes' will give you true or false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy pasta. will update

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
10 changes: 10 additions & 0 deletions lib/tasks/sprig.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace :db do
namespace :seed do

desc 'Purge seed files for specified environment'
task :purge do
Sprig::Task::PurgeSeeds.new.perform
end

end
end
7 changes: 7 additions & 0 deletions spec/lib/sprig/railtie_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

describe Sprig::Railtie do
subject { described_class.instance }

its(:railtie_name) { should == 'sprig_railtie'}
end
51 changes: 51 additions & 0 deletions spec/lib/sprig/task/purge_seeds_spec.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions spec/lib/tasks/sprig_rake_spec.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions spec/support/shared_context/rake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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__)}/../../.."))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wat?

Rake.application = rake
Sprig::Railtie.instance.load_tasks
Rake::Task.define_task(:environment)
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has some :neckbeard: going on.