LoginSignup
5
6

More than 5 years have passed since last update.

HerokuでプロダクションDBと開発用DBのスキーマを比較する

Last updated at Posted at 2016-02-01

要件

開発用DBとプロダクションDBで一部文字列カラムの長さが異なっていることが発覚したため検証を行った。(evolution用のSQLの内容が元のスキーマに反映されていなかったと思われる)

ただし、開発用DBは常にALTER TABLEで列追加されているのに対し、プロダクションDBではその中間段階でCREATE TABLEしているため、カラムの順番は無視する必要がある

herokuのpg:psqlコマンドでは-cオプションでSQLを即時実行できるので、スキーマ取得のコマンドを発行してそのdiffを取れば良い。

\dだと列の順序が無視できないので、システムテーブルのinformation_schema.columnsテーブルからSELECTする

コマンド

APP_NAMEの部分を適宜書き換えればすぐに使えます。

heroku pg:psql -a APP_NAME -c "select table_schema as sch, table_name as tbl, column_name as col, column_default as def, is_nullable as null, data_type as type, character_maximum_length as maxlen, numeric_precision as npc, numeric_precision_radix as npcr, numeric_scale as ns, datetime_precision as dp from information_schema.columns where table_schema = 'public' order by tbl, col;" > APP_NAME.txt
5
6
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
5
6