Rails database/migration関連

#migrate reset失敗の時
自分のケースは以下のようなエラー：

```common-lisp
$ rails db:migrate:reset
Permission denied @ unlink_internal - D:/Ruby on Rails/movie_share_app/db/development.sqlite3
Couldn't drop database 'db/development.sqlite3'
rails aborted!
Errno::EACCES: Permission denied @ unlink_internal - D:/Ruby on Rails/movie_share_app/db/development.sqlite3
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:drop:_unsafe
(See full trace by running task with --trace)
```

##解決方法１：rollbackを使う
statusを確認し、一番上までstepを数え、rollbackして、再びmigrateする

```common-lisp
$ rails db:migrate:status
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181031183005  Create users
   up     20181107023324  Create movies
   up     20181107080715  Add posterpath to movies
   up     20181108132436  Add original title to movies
   up     20181108142344  Add pic path to movies
   up     20181108142408  Add genre to movies

$ rails db:rollback STEP=6
$ rails db:migrate
```

##解決方法２：gem 'database_cleaner'
マイグレーションファイル修正なしの場合のみ使用
**特定のテーブルをリセットしたい時も使える**

`rails console`を起動
`DatabaseCleaner.clean_with(:truncation, :only => ['table_name'])`を一つ一つ全テーブルに対して実行する

```common-lisp
irb(main):001:0> DatabaseCleaner.clean_with(:truncation, :only => ['list_movies'])
   (21.1ms)  DELETE FROM "list_movies";
   (1.0ms)  SELECT name FROM sqlite_master WHERE type='table' AND name='sqlite_sequence';
   (18.7ms)  DELETE FROM sqlite_sequence where name = 'list_movies';
=> [#<DatabaseCleaner::Base:0x5d2e5a8 @orm=:active_record, @strategy=#<DatabaseCleaner::ActiveRecord::Transaction:0x5d2e308 @db=:default>, @db=:default>]
```

これでlist_moviesというテーブルの中身(レコード/データ)は全部削除されました

```common-lisp
irb(main):001:0> ListMovie.count
   (1.0ms)  SELECT COUNT(*) FROM "list_movies"
=> 0
irb(main):002:0> ListMovie.all
  ListMovie Load (0.0ms)  SELECT "list_movies".* FROM "list_movies" ORDER BY "list_movies"."created_at" DESC
=> #<ActiveRecord::Relation []>
```


#マイグレーションファイルの内容が反映されてない時
※自分のケース
理由：ファイルを修正する前に`rails db:migrate`を実行した
**※特定のマイグレーションから最新のまで全部やり直したい場合も適用**

```common-lisp
$ rails db:migrate:status
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181031183005  Create users
   up     20181107023324  Create movies
   up     20181107080715  Add posterpath to movies
   up     20181108132436  Add original title to movies
   up     20181108142344  Add pic path to movies
   up     20181108142408  Add genre to movies

$ rails db:rollback STEP=2

$ rails db:migrate:status
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181031183005  Create users
   up     20181107023324  Create movies
   up     20181107080715  Add posterpath to movies
   up     20181108132436  Add original title to movies
  down    20181108142344  Add pic path to movies
  down    20181108142408  Add genre to movies
```

これで一番下の二つのファイルを更新出来る。
後でまた`rails db:migrate`を実行すればOK


#特定のマイグレーションだけやり直す
バージョンを指定する

```common-lisp
$ rails db:migrate:status
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181027104609  Create cars
   up     20181027131342  Add price to cars
   up     20181027133803  Rename make to company

$ rails db:migrate:down VERSION=20181027131342
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181027104609  Create cars
  down    20181027131342  Add price to cars
   up     20181027133803  Rename make to company
```

#複数のカラムをまとめて削除

```common-lisp
$ rails g migration remove_columns_from_movies
    invoke  active_record
    create    db/migrate/20181121020444_remove_columns_from_movies.rb
```

```ruby:20181121020444_remove_columns_from_movies.rb
class RemoveColumnsFromMovies < ActiveRecord::Migration[5.0]
  def change
  	remove_column :movies, :posterpath
  	remove_column :movies, :original_title
  	remove_column :movies, :pic_path
  	remove_column :movies, :genre
  end
end
```