Reputation: 1793
Every time I run test. Rails will delete my data from table. I have million of record in my table for testing search performance and corrective. I can't add data every time I run test.
How to tell rails "Please don't delete data in this table" when i run test.
ps.
I found this link
How do I run Rails integration tests without dropping DB contents?
It's maybe relate to my problems but I don't know where to put his code in my rails project.
Upvotes: 4
Views: 3087
Reputation: 2230
In Rails 4 I end up with following
# lib/tasts/test.rake
# Do not drop database for tests
if ENV['RAILS_ENV'] == 'test'
Rake::TaskManager.class_eval do
def delete_task(task_name)
@tasks.delete(task_name.to_s)
end
end
Rake.application.delete_task("db:test:load")
namespace :db do
namespace :test do
task :load do
end
end
end
end
Upvotes: 2
Reputation: 1338
Very similar to neokain's previous post, however, his didn't work on Rails 3 for me. I went ahead and dropped this into my Rakefile at the root of the app and when I run test:units, it doesn't blow away all of my existing tables:
Rake::TaskManager.class_eval do
def delete_task(task_name)
@tasks.delete(task_name.to_s)
end
Rake.application.delete_task("db:test:purge")
Rake.application.delete_task("db:test:prepare")
end
namespace :db do
namespace :test do
task :purge do
end
task :prepare do
end
end
end
Upvotes: 7
Reputation: 507
I try to do allow first answer but not work.
I search and found this www.pervasivecode.com and I modify code from first answer like this:
if ENV['NO_DB']
Rake::TaskManager.class_eval do
def delete_task(task_name)
@tasks.delete(task_name.to_s)
end
Rake.application.delete_task("db:test:purge")
end
namespace :db do
namespace :test do
task :prepare do
end
end
end
end
Then run command test allow first answer. Database test is not drop.
Upvotes: 1
Reputation: 46
The "rake test" task always runs db:test:prepare which will recreate your database.
You can add this somewhere in lib/tasks:
if ENV['NO_DB']
namespace :db do
namespace :test do
task :prepare do
end
end
end
end
And then run NO_DB=1 rake test
. Also when you use autotest instead of the rake tasks the DB won't be changed.
Upvotes: 1