LoginSignup
0
0

More than 1 year has passed since last update.

Rails マイグレーションファイルでミスがあった時

Posted at
db/migrate/202*********_create_items.rb
class CreateItems < ActiveRecord::Migration[6.0]
  def change
    create_table :items do |t|
      t.string :name, null: false
      t.text :info, null: false
      t.integer :category_id, null: false
      t.integer :status_id, null: false
      t.integer :fee_status_id, null: false
      t.integer :prefecture_id, null: false
      t.integer :schedule_delivery_id, null: false
      t.integer :price, null: false
      t.references :user, null: false, foreign_key: true
      t.timestamps
    end
  end
end

例えば、nameカラムをstringにしているわけですが、これをtextに変更したいという時ですね。

まずはマイグレーションの状況を確認します。

rails db:migrate:status
database: app_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20210*********  Devise create users
   up     20210*********  Create items

こんな感じで出力されて、upとなっているのが、すでにマイグレーション済みとなります。

rails db:rollback

このコマンドを実行すると

database: app_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20210*********  Devise create users
 down     20210*********  Create items

というふうに、最新のマイグレーションファイルがdownになります。
この状態であれば、2番目のマイグレーションファイルを修正して、再度、

rails db:migrate

を実行すればOKです。

仮に上のマイグレーションファイルを修正したいときはもう一度

rails db:rollback

を実行して、ファイルを修正したのちにいつも通り、マイグレーションを実行です。
そうすると、全てのファイルがマイグレーションされるので、2番目のファイルも自動的にマイグレーションされます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0