Rake file is seeing old version of database on Her

2019-08-03 02:12发布

问题:

I'm using a rakefile to seed my database. I was seeing weird behavior (see Additional user attributes results in UnknownAttributeError and NoMethodError) and have concluded that it is operating on an old version of my database (at the very least, an old version of my Users table, perhaps more).

  • Running the rakefile on localhost works fine
  • On Heroku, printing User.column_names within the rakefile shows the old version of the table
  • On Heroku, printing User.column_names from within the main app shows the new version of the table
  • Within Heroku rails console, User.column_names shows the new version of the table

Any ideas how to resolve?

回答1:

One thing to make sure you do on heroku is restart your dynos correctly. A client of mine once tried something like this:

heroku run rake db:migrate db:seed_data

Heroku's documentation at https://devcenter.heroku.com/articles/rake mentions that you should restart your app in between migrations:

After running a migration you’ll want to restart your app with heroku restart to reload the schema and pickup any schema changes.

So the answer might be to not batch it in the same process; i.e. try something like

heroku run rake db:migrate; heroku run rake db:seed_data