blog.tkeo.info

日記

railsアプリ開発中にmigrationでエラーが起こって、一部だけ変更が適用された戻すのがめんどくさい中途半端な状態になることがよくあって今日もうぎゃーってなったので、トランザクションで囲んでくれるような仕組みないのかなと思って調べてみると

http://guides.rubyonrails.org/migrations.html

On databases that support transactions with statements that change the schema (such as PostgreSQL or SQLite3), migrations are wrapped in a transaction. If the database does not support this (for example MySQL) then when a migration fails the parts of it that succeeded will not be rolled back. You will have to rollback the changes that were made by hand.

なんだってー

postgresはデフォルトで対応しているが、mysqlはサポートされてなくて非対応。知らなかった…。

一応mysqlのドキュメントにもあたってみる。

http://dev.mysql.com/doc/refman/5.6/en/cannot-roll-back.html

Some statements cannot be rolled back. In general, these include data definition language (DDL) statements, such as those that create or drop databases, those that create, drop, or alter tables or stored routines.

DDLはロールバックできない、と。

開発環境だけDBサーバ変えるとかしたくないし、どうしたらいいかなあ。

という日記。

追記

change_tablebulk: trueオプションを付けてあげればいいらしい。 ひとつのALTERでまとめてカラム追加・削除されるので中途半端な状態は作られない。