为什么不调用 db:migrate 作为先决条件?



我有一个引导任务,我打算将db:resetdb:migrate作为先决条件。我这样定义它:

task :bootstrap => [:environment,:"db:reset",:"db:migrate"] do ...

当我运行它时,我得到以下输出:

** Invoke bs:bootstrap (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:reset (first_time)
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:drop
** Invoke db:setup (first_time)
** Invoke db:create (first_time)
** Invoke db:load_config
** Execute db:create
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
-- create_table("projects", {:force=>true})
   -> 0.0770s
-- create_table("users", {:force=>true})
   -> 0.1110s
...
** Invoke db:seed (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
You have 1 pending migrations:
  20120109172252 CreateObjectives
Run "rake db:migrate" to update your database then try again.

既然db:migrate被列为先决条件,为什么不调用它?

看起来db:reset正在中止,因为您有挂起的迁移。由于db:reset将使用db:schema:load来使用db/schema.rb文件来重置数据库,因此您实际上不需要运行迁移。

相反,您可以将db:migrate放在db:reset之前——这将运行迁移,更新您的schema.rb文件,以便db:sreset在重置db时使用更新的版本(从而避免挂起的迁移错误)。

最新更新