rails database migrations using transactions

 ̄綄美尐妖づ 提交于 2021-02-16 16:27:43

问题


I'm just learning Rails and have begun the section on database migrations. I built 2 migrations and both migrated up successfully. Migrating down, the latest migration, the one that runs first, failed because of a typo in my code. I fixed the typo but the migration continued to fail after that. I discovered the reason why was that the migrating down aborted half way through changes and then when I tried to migrate down again it failed because some of the changes had already been made and so column names were different and other issues like that. I ended up fixing it by fiddling with the schema_migrations table and manually rolling back my changes to a previous version and then migrating back up and down from there.

My question is, is there a way to tell Rails to run migrations in transaction mode and if the code fails, not to commit the transaction?


回答1:


Rails will already run your migrations inside a transaction if your database supports it:

On databases that support transactions with statements that change the schema, migrations are wrapped in a transaction. If the database does not support this 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.

It's up to you to use a database that respects transactions when performing schema changes. MySQL (as far as I know) doesn't, but Postgres absolutely does.



来源:https://stackoverflow.com/questions/37820740/rails-database-migrations-using-transactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!