1 namespace :db do
2 desc "Fill database with sample videos"
3 task :populate => :environment do
4 require 'faker'
5 Rake::Task['db:reset'].invoke
6 100.times do |n|
7 headline = Faker::Lorem.sentence(3)
8 video = Faker::Lorem.words(5)
9 Video.create!(:headline => headline,
10 :video => video)
11 end
12 end
13 end
I currently have this rake task in lib/tasks/sample_data.rb
When running rake db:populate I get the error, Don't know how to build task 'db:populate'. How do I get around this?
Notes:
I am a newbie in Rails/Ruby. I am using Rails 3.
Try renaming the file to sample_data.rake.
I was able to get your example working (replacing the internals of the task with a p statement) by putting your code in a file called testomatic.rake in lib/tasks.
I also had this problem. In Finder, the file name was sample_data.rake, but upon clicking "Get Info" for the file, I discovered the full file name was sample_data.rake.erb -- ensure that's not your problem.
Noobie with the same problem here - my branch was named differently than what I thought when I merged branches, so it reverted back to the old files before I had created my lib/tasks/sample_data.rake file.
Simply going back and re-running git merge with the correct name and pushing to Heroku got it to work.
Related
I'd like to start over with errbit - there are millions of records in our mongodb database and we hadn't been cleaning them up. I'd like to start over, but I don't want to lose my user accounts.
I've tried to run these routines (https://mensfeld.pl/2015/01/making-errbit-work-faster-by-keeping-it-clean-and-tidy/):
bundle exec rake errbit:clear_resolved
desc 'Resolves problems that didnt occur for 2 weeks'
task :cleanup => :environment do
offset = 2.weeks.ago
Problem.where(:updated_at.lt => offset).map(&:resolve!)
Notice.where(:updated_at.lt => offset).destroy_all
end
but the second one (deleting problems and notices over 2 weeks old), just seems to run forever.
Querying problems and notices collections via mongo shell doesn't seem to show any being deleted... we're using errbit V 0.7.0-dev and mongodb version 3.2.22.
Fastest way would be to get a mongo console and drop most of the collections. I'd say stop your errbit server, get a mongo console, connect to the db you use and run:
> db.errs.drop()
true
> db.problems.drop()
true
> db.backtraces.drop()
true
> db.notices.drop()
true
> db.comments.drop()
Problem.where(:updated_at.lt => 2.months.ago).destroy_all
runs too long because of N+1 problem with recursive deletion of Err, Notice and Comment, also mongoid does not support nested eager loading, so only way to delete faster - is to manually take these ids and delete directly, without callbacks:
problem_ids = Problem.where(:updated_at.lt => 2.months.ago).pluck(:id)
err_ids = Err.where(problem_id: {:$in => problem_ids}).pluck(:id)
Notice.where(err_id:{:$in => err_ids}).delete_all
Err.where(id:{:$in => err_ids}).delete_all
Comment.where(problem_id: {:$in => problem_ids}).delete_all
Problem.where(id: {:$in => problem_ids}).delete_all
I've written my first rake task in rails, I've set it up with the Heroku scheduler and it is getting run, however the mail isn't getting sent. My mail settings are fine as I'm using it for various other things, I would imagine it's a problem with my code in the rake task. Any help would be much appreciated.
lib/tasks/uncomplete_form.rake
desc "Remind users if they haven't completed quote form"
task uncomplete_form: :environment do
puts 'Reminding users of uncomplete quote form'
date = Date.parse('december 18 2016')
quickcontacts = Quickcontact.where(created_at: date.midnight..Time.now)
quickcontacts.each do |quickcontact|
next unless quickcontact.created_at > 1.hour.ago
if quickcontact.p_p = nil
QuickcontactMailer.uncomplete_form(#quickcontact).deliver
end
end
puts 'done.'
end
By running rake uncomplete_form I get
Reminding users of uncomplete quote form
done.
And running
heroku run rake uncomplete_form
I get
Reminding users of uncomplete quote form
Quickcontact Load (1.5ms) SELECT "quickcontacts".* FROM "quickcontacts" WHERE ("quickcontacts"."created_at" BETWEEN '2016-12-18 00:00:00.000000' AND '2016-12-20 12:09:23.683977')
done.
It doesn't seem to be picking up any quickcontacts - however if in the console I run:
date = Date.parse('december 18 2016')
followed by
quickcontacts = Quickcontact.where(created_at: date.midnight..Time.now)
It does find the expected contacts
Have you tried
QuickcontactMailer.uncomplete_form(#quickcontact).deliver_now
?
Edit: What's .p_p supposed to be? You're doing assignment in that if clause instead of what I presume should have been comparison (?)
if quickcontact.p_p = nil
Solved: Two bad errors on my part. Thanks for the help with the first #matija. The problem was with my rake task code. The first error was where I had 'if quickcontact.p_p = nil' needed to be changed to 'if quickcontact.p_p.nil?' - I was accidentally assigning nil value, rather than checking it.
The second error was that in the next line down, quickcontact should not have been an instance variable. This is the updated, functioning code:
desc "Remind users if they haven't completed quote form"
task uncomplete_form: :environment do
puts 'Reminding users of uncomplete quote form'
date = Date.parse('december 18 2016')
quickcontacts = Quickcontact.where(created_at: date.midnight..Time.now)
quickcontacts.each do |quickcontact|
next unless quickcontact.created_at > 1.hour.ago
if quickcontact.p_p.nil?
QuickcontactMailer.uncomplete_form(quickcontact).deliver
end
end
puts 'done.'
end
Our test suite has a problem.
Whenever the whole suite starts up, a stray record is created.
Whenever I run a single file or context of specs, this stray record does not get created.
I wrote config.before(:each) { puts "COUNT = #{Model.count}\n" } in spec_helper.rb and no matter which order the tests ran, this record persists somehow. Seemingly before the examples even started.
We seed our database, so I tried a clean setup.
RAILS_ENV=test rake db:setup
RAILS_ENV=test rake db:seed
echo "Model.count" | rails c test
=> 0
Then whenever I run all tests (checked the order and checked for before(:all)).
rspec
COUNT = 1
.
COUNT = 1
.
COUNT = 1
.
etc
I've meticulously examined spec_helper.rb (RSpec2, pre rails_helper.rb) and commented out every support file with no luck.
I'm suspecting the code base at this point, maybe some weird call to something where someone left behind a find_or_create, a weird callback or something, I have no idea.
What I would like to know is:
How do I tackle breaking down the suites startup?
Can I get a backtrace of my test suite just starting up?
Anyone successfully chased down this kind of persistent record in RSpec?
EDIT
I added config.before(:suite) { debugger } and the record is still created before this!
How can I break down this code even further?
[23, 32] in /Users/yourname/.rvm/gems/ruby-1.9.3-p327#projectname/gems/rspec-core-2.14.6/lib/rspec/core/command_line.rb
23 #world.announce_filters
24
25 #configuration.reporter.report(#world.example_count, #configuration.randomize? ? #configuration.seed : nil) do |reporter|
26 begin
27 #configuration.run_hook(:before, :suite)
=> 28 #world.example_groups.ordered.map {|g| g.run(reporter)}.all? ? 0 : #configuration.failure_exit_code
29 ensure
30 #configuration.run_hook(:after, :suite)
31 end
32 end
(rdb:1) ModelName.count
1
For the record of how I found the persistent record:
By using before(:suite) { debugger }, I removed the record and ran the entire test suite and notice a test was failing.
Within this test, was something similar to the following:
context "blah" do
model = FactoryGirl.create(:model)
end
I simply put the code within a before(:each) block, and the test was passing and now so were mine.
TIL: Whenever RSpec loads the suite, it evaluates all the code that is not within a transaction. Which is why I could not try to pin point a problematic file by looking at when the record was being created.
I have the following thor command:
require 'highline'
class Import < Thor
desc "files", "Import files into the database"
method_option "path", :required => true, :desc => "Path to folder containing new files", :aliases => "-p", :type => :string
def files
require './config/environment'
line = HighLine.new
line.say(line.color("Identified files as Version 15 (English)", :green))
if line.agree(line.color("Are you sure you want to import?", :yellow))
line.say(line.color("Finished. Imported 70,114 items", :green))
else
line.say(line.color("Aborting...", :red))
end
end
end
Now, obviously, at the moment this is just outputting some language to the screen. However, what I need to do is write a test for the command that tests the output is as I would expect, and that when I start hooking in the heavy lifting that I can stub that stuff out.
I've had a look at Aruba, but this doesn't appear to like interactivity for some reason, and it's not clear why.
Therefore, does anyone have any ideas on how this might be testable (with RSpec)?
Aruba is a pretty complete set of steps for testing command line apps. If it's not working for you it might be because aruba defaults all file operations into tmp/aruba.
But neimOo is right about how to write the scenario with aruba
When I run `thor import` interactively
And I type "yes"
Here is how you can do this with Aruba
Scenario: Test import
When I run `thor import` interactively
And I type "yes"
Then the stdout should contain "Finished. Imported 70,114 items"
Here you can find a lot of aruba examples
https://github.com/cucumber/aruba/blob/master/features/interactive.feature
And here is implementation itself
https://github.com/cucumber/aruba/blob/master/lib/aruba/cucumber.rb
I'm adding a test task in my Rakefile, similar to this:
namespace :test do
desc "Test lib source"
Rake::TestTask.new(:lib) do |t|
t.libs << "test"
t.pattern = 'test/lib/**/*_test.rb'
t.verbose = true
end
end
and then adding (have also done using "enhance" with the same result:
task :test => [ 'test:lib' ]
My problem is that if there is an error encountered in test:lib, the suite stops running. That's not a terrible thing, but ideally it would go on to run the rest of the suite to let me know that there are more issues later in the suite.
Anyone know how to make it report the errors/failures in test:lib but go on to run the full suite?
Thanks!
Used something along these lines (minus the "on the fly" bit):
http://toolmantim.com/articles/creating_rake_testtasks_on_the_fly