yujiroのプログラミング

勉強内容をアウトプットし、サボらないようにする為のブログ

<DAY190>マイグレーションファイルを整理する時に注意する事

\ Follow me!! /

●8/30(金)
●学習日数 190日
●学習時間(本日)1時間
●累計学習時間 821.0時間
●一日あたりの平均学習時間 4.34時間

f:id:yujiro0320:20190512223713p:plain

どんな状況

マイグレーションファイルを整理する。
このような形でカラムを追加していくと見えにくくなる。

f:id:yujiro0320:20190827191244p:plain


最終的にこのような形に整理する。

f:id:yujiro0320:20190827191335p:plain



手順1

マイグレーションファイルをdowmの状態に戻す。
STEPで行数指定すると指定したマイグレーションファイルまで一気に戻す事ができる。

rake db:rollback STEP=17


down状態に戻ったか確認する。

bundle exec rake db:migrate:status

手順2

down状態であれば、マイグレーションファイルを消しても問題ない。
dbベースに必要なカラムを記入し、マイグレーションファイルを作成する。

class CreateTweets < ActiveRecord::Migration[5.2]
  def change
    create_table :tweets do |t|
      t.text :content, null: false
      t.text :point, null: false
      t.string :title, null: false
      t.integer :type_problem, null: false
      t.integer :judge_problem1, null: false
      t.integer :judge_problem2, null: false
      t.integer :judge_problem3, null: false
      t.integer :judge_problem4, null: false
      t.integer :judge_problem5, null: false
      t.string :select_problem1, null: false
      t.string :select_problem2, null: false
      t.string :select_problem3, null: false
      t.string :select_problem4, null: false
      t.string :select_problem5, null: false
      t.references :user, foreign_key: true #user_idを加えたい。<user>でOK
      t.timestamps 
    end
  end
end