26
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

migration の代わりに schema.rb のみで DB 状態を管理する

Posted at

設計がきちんとできていないせいで,開発途中でテーブルにカラムの追加や変更がガンガン出てくる訳だが,いちいちコマンドラインを叩いて migration を作るのが面倒臭いし,何より migration ファイルがずらりと並んでいるのが計画性の無さの証左のようで見苦しい.

どうやら,

reset
```で,schema.rb の内容に基づいてテーブルを再作成してくれるらしい.

ただ当然ながら,開発途中に投入したデータも全て消去されてしまう.これも
seeds.rb にきちんとテスト用データを入れておけば良いのだが,いかんせん面倒臭い.

そこで 'yaml_db' を使って,データをダンプ → db:reset → データをロード,としてやれば良い.
もちろん,追加したカラムにデータは入らないし,カラムを削除したところにデータをロードしようとするとエラーになるだろうが(未確認),このあたりはダンプした yaml を編集してやらないと仕方ない.


```Gemfile
gem 'yaml_db'

# Rails4 でエラーが出る場合はこっち.
# gem 'yaml_db', github: 'jetthoughts/yaml_db', ref: 'fb4b6bd7e12de3cffa93e0a298a1e5253d7e92ba'

db/data.yamlを生成

rake db:data:dump

db/data.yamlを読み込み

rake db:data:load```いろいろあるが,こういう開発スタイルを取っているなら工数は少なく済むと思うのだがどうだろう.

26
22
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
26
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?