Rake 顺序任务 [英] Rake sequential tasks

查看:47
本文介绍了Rake 顺序任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的问题.我有一个任务可以重置我的数据库:

I have run into a very strange problem. I have a task which resets my database as so:

task :reset => [:drop, :create, :migrate, :seed]

问题是,由于在后期迁移文件中添加的列丢失,我在播种时收到错误.一个例子:

The problem is, I am receiving errors when seeding because of missing columns which are added in late migration files. One example:

undefined method new_attr= for User

但此属性已添加到迁移中.奇怪的是,如果我单独运行上述任务,我会没有 收到错误.有人可以解释一下吗?这些任务当然不能异步运行.

Yet this attribute is already added in a migration. The strange part is, I receive no errors if I run the above tasks separately. Can anybody shed some light? Surely these tasks cannot be run asynchronously.

另一种避免错误的方法是使用新属性修改我之前的迁移 create_.然后运行 ​​:reset 不会触发这些属性的错误.

Another way to avoid errors is to amend my earlier migrations create_ with the new attributes. Then running :reset doesn't trigger errors for those attributes.

迁移显然没问题,因为我可以单独运行上述任务,而不是捆绑在单个任务下.

The migrations are clearly fine as I can run the above tasks separately, just not bundled under a single task.

推荐答案

也许您想让您的重置任务更加明确?

maybe you want to make your reset task more explicit?

namespace :db_tasks do
  desc "Rebuild development db"
  task :rebuild_database, [] => :environment do
    raise "Only run in development or staging" if Rails.env.production?

    Rake::Task['db:drop'].execute
    Rake::Task['db:create'].execute
    Rake::Task['db:migrate'].execute
    Rake::Task['db:seed'].execute
    Rake::Task['db:test:prepare'].execute
  end
end

这篇关于Rake 顺序任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆