Rails
Heroku

railsのdeployをherokuにしたときに詰まった件

できごと

開発環境ではdeployできたのにheroku環境だとmigrationfileが途中までしか反映されないという事象が起こっていた。

対策

heroku run rake db:abort_if_pending_migrations

これでmigrationfileで反映できていないものを反映させようとしたところ

SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
You have 5 pending migrations:
  20170924135457 RemoveColumnUsers
  20170924140750 AddColumnPosts
  20171001082556 RenameSexColumnToGender
  20171007030115 CreateComments
  20171007030215 Comments
Run `rails db:migrate` to update your database then try again.

とでてrails db:migrateしたんだが結局ダメだった。

[2017-11-13T14:50:26.775253 #4] DEBUG -- :    (3.9ms)  SELECT pg_try_advisory_lock(92685191377748025)
D, [2017-11-13T14:50:26.805445 #4] DEBUG -- :    (3.3ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
I, [2017-11-13T14:50:26.807693 #4]  INFO -- : Migrating to RemoveColumnUsers (20170924135457)
D, [2017-11-13T14:50:26.812604 #4] DEBUG -- :    (1.0ms)  BEGIN
== 20170924135457 RemoveColumnUsers: migrating ================================
-- remove_columns(:users, :sex, :integer)
D, [2017-11-13T14:50:26.814865 #4] DEBUG -- :    (1.5ms)  ALTER TABLE "users" DROP "sex"
D, [2017-11-13T14:50:26.821438 #4] DEBUG -- :    (6.3ms)  ALTER TABLE "users" DROP "integer"
D, [2017-11-13T14:50:26.830247 #4] DEBUG -- :    (6.6ms)  ROLLBACK
D, [2017-11-13T14:50:26.831768 #4] DEBUG -- :    (0.8ms)  SELECT pg_advisory_unlock(92685191377748025)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedColumn: ERROR:  column "integer" of relation "users" does not exist
: ALTER TABLE "users" DROP "integer"

エラー文みると上記のようにRemoveColumnUsersがなんかうまく行っていない

class RemoveColumnUsers < ActiveRecord::Migration[5.1]
  def up
    remove_column :users, :sex, :integer
    remove_column :users, :line_id, :text
    remove_column :users, :icon_id, :integer
    remove_column :users, :icon_back_id, :integer
  end
  def down
    remove_column :users, :sex, :integer
    remove_column :users, :line_id, :text
    remove_column :users, :icon_id, :integer
    remove_column :users, :icon_back_id, :integer
  end
end

remove_columnにsがついていて型が指定できない形だった。このせいでエラーが出ていたのでmigrationfileを書き換えてrails db:migrateで解決。