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.
每次我跑测试。 Rails将从表中删除我的数据。我的表中有数百万的记录用于测试搜索性能和纠正。我每次运行测试时都无法添加数据。
How to tell rails "Please don't delete data in this table" when i run test.
当我运行测试时,如何告诉rails“请不要删除此表中的数据”。
ps.
I found this link
我找到了这个链接
How do I run Rails integration tests without dropping DB contents?
如何在不丢弃DB内容的情况下运行Rails集成测试?
It's maybe relate to my problems but I don't know where to put his code in my rails project.
这可能与我的问题有关,但我不知道将他的代码放在我的rails项目中。
4 个解决方案
#1
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:
与neokain上一篇文章非常相似,然而,他并没有为我的Rails 3工作。我继续把它放到应用程序根目录下的Rakefile中,当我运行test:units时,它不会吹走我现有的所有表:
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
#2
In Rails 4 I end up with following
在Rails 4中,我最终得到了以下内容
# 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
#3
The "rake test" task always runs db:test:prepare which will recreate your database.
“rake test”任务总是运行db:test:prepare,它将重新创建数据库。
You can add this somewhere in lib/tasks:
您可以在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.
然后运行NO_DB = 1 rake test。此外,当您使用自动测试而不是rake任务时,DB将不会更改。
#4
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:
我搜索并找到了这个www.pervasivecode.com,我修改了第一个答案中的代码,如下所示:
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.
然后运行命令test允许第一个答案。数据库测试不会丢失。
#1
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:
与neokain上一篇文章非常相似,然而,他并没有为我的Rails 3工作。我继续把它放到应用程序根目录下的Rakefile中,当我运行test:units时,它不会吹走我现有的所有表:
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
#2
In Rails 4 I end up with following
在Rails 4中,我最终得到了以下内容
# 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
#3
The "rake test" task always runs db:test:prepare which will recreate your database.
“rake test”任务总是运行db:test:prepare,它将重新创建数据库。
You can add this somewhere in lib/tasks:
您可以在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.
然后运行NO_DB = 1 rake test。此外,当您使用自动测试而不是rake任务时,DB将不会更改。
#4
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:
我搜索并找到了这个www.pervasivecode.com,我修改了第一个答案中的代码,如下所示:
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.
然后运行命令test允许第一个答案。数据库测试不会丢失。