问题描述
我在将迁移推送到生产数据库时遇到问题.
I have a problem with pushing my migrations to the production database.
问题:
- 我通过添加 1 列更改了数据库架构.
我已将其迁移到生产数据库:
- I've altered database schema by adding 1 column.
I've migrated it to the production database:
MacBook-Air-Mac:app msc$ rake db:migrate RAILS_ENV="生产"[RailsAdmin] 默认情况下禁用 RailsAdmin 初始化.如果需要,请传递 SKIP_RAILS_ADMIN_INITIALIZER=false .== AddLengthColumnToBooks:迁移 ==========================================-- add_column(:books, :length, :integer)-> 0.0017s== AddLengthColumnToBooks:迁移(0.0019s)================================
MacBook-Air-Mac:app msc$ rake db:migrate RAILS_ENV="production"[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.== AddLengthColumnToBooks: migrating =========================================-- add_column(:books, :length, :integer) -> 0.0017s== AddLengthColumnToBooks: migrated (0.0019s) ================================
考虑到新的 DB 模式现已投入生产,我已经部署了使用 :length
执行某些操作的代码.
Thinking that the new DB schema is now in production, I've deployed the code which does some things with :length
.
在生产中,我收到以下错误:
In production, I got the following error:
未定义的方法`length=' for #
undefined method `length=' for #
我执行了 heroku 回滚
并将应用程序降级到最新的可靠版本.
I did heroku rollback
and downgraded the app to the latest reliable version.
然后(可能有点晚了)我发现我必须heroku restart
应用程序来加载新索引.我做了好几次.
THEN (a bit too late probably) I found out that I have to heroku restart
the app to load the new indexes. I did this several times.
然后我打开控制台并检查了Book.column_names
,但是没有length
I opened the console then and checked Book.column_names
, but there was no length
我又做了一次 heroku run rake db:migrate
之后是 heroku restart
,没有任何变化.
I did heroku run rake db:migrate
followed by heroku restart
one more time, no change.
我尝试将另一列迁移到生产数据库,但根本没有收到任何消息,甚至没有收到第 2 页的消息.
I've tried migrating another column to the production db, but didn't get any message at all, not even the one from p.2.
我在这里做错了什么?
更新
根据 Philipe 的回答,我做了一些额外的步骤:
Based on the answers of Philipe, I did a number of additional steps:
git add db/schema.rb
,git add db/migrate/20130325103953_add_length_column_to_books.rb
和git add db/migrate/20130401041910_add_duration_column_to_books.rb".Git 的回答是:要提交的更改:(使用git reset HEAD ..."取消暂存)
git add db/schema.rb
,git add db/migrate/20130325103953_add_length_column_to_books.rb
and 'git add db/migrate/20130401041910_add_duration_column_to_books.rb'. Git's answer was:Changes to be committed: (use "git reset HEAD ..." to unstage)
新文件:db/migrate/20130325103953_add_length_column_to_books.rb新文件:db/migrate/20130401041910_add_duration_column_to_books.rb修改:db/schema.rb
new file: db/migrate/20130325103953_add_length_column_to_books.rbnew file: db/migrate/20130401041910_add_duration_column_to_books.rbmodified: db/schema.rb
然后我做了git commit -m更新架构"
.
同样的输出是:
3 files changed, 168 insertions(+), 156 deletions(-)
创建模式 100644 db/migrate/20130325103953_add_length_column_to_books.rb创建模式 100644 db/migrate/20130401041910_add_duration_column_to_books.rb
create mode 100644 db/migrate/20130325103953_add_length_column_to_books.rb create mode 100644 db/migrate/20130401041910_add_duration_column_to_books.rb
然后我运行 heroku run rake db:migrate
.不幸的是,没有迁移的迹象,只是得到了:
Then I run heroku run rake db:migrate
. Unfortunately there was no sign of migrations, simply got:
运行 rake db:migrate
附加到终端... up, run.5428就是这样.
Running rake db:migrate
attached to terminal... up, run.5428and that's it.
在生产的 Rails 控制台中,运行 Book.column_names
仍然缺乏长度和持续时间.
In the production Rails Console, running Book.column_names
still lacks both length and duration.
现在我更没有想法了.`
Now I'm even more out of ideas.`
推荐答案
您似乎没有将更改推送到 Heroku.步骤应该如下;
It doesn't look like you are pushing changes to Heroku. Steps should be as follows;
- 更改本地代码
- 在本地运行任何迁移
- 将所有更改的文件添加到 Git
git add .
- 提交所有添加的文件到 git
git commit -m "Adding features"
- 将更改推送到 Heroku
git push heroku master
- 假设您使用heroku
作为远程名称并且您正在使用master
分支 - 如果您有迁移,请运行
heroku run rake db:migrate
以在 HEROKU 上运行迁移 - 以下迁移执行
heroku restart
- Make changes to local code
- Run any migrations LOCALLY
- Add all changed files to Git
git add .
- Commit all added files to git
git commit -m "Adding features"
- Push the changes to Heroku
git push heroku master
- assuming you are usingheroku
as your remote name and you are working in themaster
branch - If you have migrations run
heroku run rake db:migrate
to run the migrations ON HEROKU - Following migrations do
heroku restart
这应该是您工作所需的全部.
That should be all you need to get you working.
这篇关于Heroku 运行 rake db:migrate 导致数据库没有变化,应用程序重新启动了几次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!