はじめに
Herokuまわりのゴタゴタがあって、個人開発はRailwayを使うようになったのですが、ちょっと困ったことがあり、自分用メモとして残しておきます
db:migrate:resetができない?
開発の初期って、DBの設計が決まっていなくて、実装していくうちに、あ、あれ足りなかったな。
ということは多いでしょう。
そんなとき、結構気軽にマイグレーションを一からやり直すコマンド
rake db:migrate:reset
をしたらいいか!くらいのノリで個人開発をやっているのですが、ちょっと問題が発生しました。
% railway run rake db:migrate:reset
Database 'railway' does not exist
We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.
To resolve this issue:
- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.
To create your database, run:
bin/rails db:create
Couldn't create 'railway' database. Please check your configuration.
rake aborted!
ActiveRecord::NoDatabaseError: We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.
To resolve this issue:
- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.
To create your database, run:
bin/rails db:create
Caused by:
PG::ConnectionBad: could not translate host name "postgres.railway.internal" to address: nodename nor servname provided, or not known
DBがないとは、、、いや、あるが??
そんなつまづくとこだっけ?と思って調べてると公式のQAのやり取りが
なにやら、「DATABASE_URLにtimeoutまでの時間を設定して3秒待たせてみる」とか、「ENABLE_ALPINE_PRIVATE_NETWORKING=true」の環境変数を設定するとか、
いろいろとTipsがあるようでしたが、どれもうまくいきませんでした。
はずかしながら回避策
真正面からの解決策ではないのですが、とりあえず、なんとか実行できた方法が以下です。
Railwayにデプロイされた際に実行させるProcfileのマイグレーションは問題なく実行されていたので、一時的にそこで実行しました
Procfile
web: rake db:migrate && bin/rails server -b 0.0.0.0 -p ${PORT:-3000}
を以下に変更
Procfile
web: rake rake db:migrate:reset && bin/rails server -b 0.0.0.0 -p ${PORT:-3000}
もちろん、ことが終わったら戻しましょう!デプロイのたびにDBが初期化されます!
釈然としないですが、とりあえずこれで回避できたので備忘録として。