実現したいこと
Railsによるアプリケーション開発で、カラムのデータ型の変更を行いたいです。
(postsテーブルのrateカラムをstringからfloat型に変更)
データベースをdevelopment環境とproduction環境でそれぞれ別のものを利用しているためか、この点が
ネックとなり型の変更を行えておりproduction環境でのみエラーが出てマイグレーションの実行ができておりません。
production環境のみマイグレーションを適用するする方法があれば教えていただきたいです。
環境
言語:Ruby 2.5.3
フレームワーク:Rails 5.2.3
データベース(development):Mysql
データベース(production):Postgresql
デプロイ:Heroku
試したこととエラー内容
1.下記マイグレーーションの実行
下記マイグレーションの実行により、development環境のデータ型の変更は完了しました。
class ChangeDataRateToPost < ActiveRecord::Migration[5.2] def change add_column :posts, :new_rate, :float end end
2.エラー内容
production環境でのマイグレーションの実行時に下記のエラーが出ています。
$ heroku run rails db:migrate Running rails db:migrate on ⬢ myapp... up, run.4444 (Hobby) D, [2019-05-18T13:20:05.582744 #4] DEBUG -- : (1.0ms) SELECT pg_try_advisory_lock(6921524284597967520) D, [2019-05-18T13:20:05.603255 #4] DEBUG -- : (3.4ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC I, [2019-05-18T13:20:05.604879 #4] INFO -- : Migrating to ChangeDataRateToPost (20190518120113) D, [2019-05-18T13:20:05.607894 #4] DEBUG -- : (0.8ms) BEGIN == 20190518120113 ChangeDataRateToPost: migrating ============================= -- add_column(:posts, :new_rate, :float) D, [2019-05-18T13:20:05.615485 #4] DEBUG -- : (7.1ms) ALTER TABLE "posts" ADD "new_rate" float -> 0.0075s == 20190518120113 ChangeDataRateToPost: migrated (0.0076s) ==================== D, [2019-05-18T13:20:05.622064 #4] DEBUG -- : ActiveRecord::SchemaMigration Create (1.2ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version" [["version", "20190518120113"]] D, [2019-05-18T13:20:05.624289 #4] DEBUG -- : (1.9ms) COMMIT I, [2019-05-18T13:20:05.624393 #4] INFO -- : Migrating to RenameRateToPosts (20190518124329) D, [2019-05-18T13:20:05.625879 #4] DEBUG -- : (0.7ms) BEGIN == 20190518124329 RenameRateToPosts: migrating ================================ -- change_column(:posts, :rate, :float) D, [2019-05-18T13:20:05.628843 #4] DEBUG -- : (2.5ms) ALTER TABLE "posts" ALTER COLUMN "rate" TYPE float D, [2019-05-18T13:20:05.633732 #4] DEBUG -- : (4.7ms) ROLLBACK D, [2019-05-18T13:20:05.636263 #4] DEBUG -- : (2.3ms) SELECT pg_advisory_unlock(6921524284597967520) rails aborted! StandardError: An error has occurred, this and all later migrations canceled: PG::DatatypeMismatch: ERROR: column "rate" cannot be cast automatically to type double precision HINT: You might need to specify "USING rate::double precision". : ALTER TABLE "posts" ALTER COLUMN "rate" TYPE float /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `async_exec' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `block (2 levels) in execute' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:74:in `block in execute' (字数制限のため中略) /app/bin/rails:9:in `<main>' Caused by: ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "rate" cannot be cast automatically to type double precision HINT: You might need to specify "USING rate::double precision". : ALTER TABLE "posts" ALTER COLUMN "rate" TYPE float /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `async_exec' (字数制限のため中略) /app/bin/rails:9:in `<main>' Caused by: PG::DatatypeMismatch: ERROR: column "rate" cannot be cast automatically to type double precision HINT: You might need to specify "USING rate::double precision". /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `async_exec' (字数制限のため中略) /app/bin/rails:9:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)
想定していること
エラー文などにより、production環境のPostgresqlのデータ型を変更する際には、cast
するオプションを利用することが実行可能であることがわかりました。
development環境のマイグレーションはすでに実行済みのためエラーが出てしまいそうだと感じています。
加えて、Postgresqlのデータ型の変更には下記のマイグレーションが必要そうです。しかし、development環境のmysqlはcast
が利用できません。
def up change_column :posts, :rate, 'float USING CAST(rate AS float)' end def down change_column :posts, :rate, :string end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/19 13:07
2019/05/19 13:10