rails 使用事务的数据库迁移
rails database migrations using transactions
我刚刚开始学习 Rails 并开始了有关数据库迁移的部分。我构建了 2 个迁移并且都成功迁移了。向下迁移,最新的迁移,第一个 运行 的迁移,由于我的代码中的错字而失败。我修正了拼写错误,但此后迁移继续失败。我发现原因是向下迁移在更改中途中止,然后当我再次尝试向下迁移时它失败了,因为已经进行了一些更改,因此列名不同以及其他类似问题。我最终通过摆弄 schema_migrations
table 修复了它,并手动将我的更改回滚到以前的版本,然后从那里上下迁移。
我的问题是,有没有办法告诉 Rails 到 运行 以事务模式迁移,如果代码失败,不提交事务?
Rails 已经 运行 您在事务中的迁移 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.
在执行模式更改时使用尊重事务的数据库取决于您。 MySQL(据我所知)没有,但 Postgres 绝对有。
我刚刚开始学习 Rails 并开始了有关数据库迁移的部分。我构建了 2 个迁移并且都成功迁移了。向下迁移,最新的迁移,第一个 运行 的迁移,由于我的代码中的错字而失败。我修正了拼写错误,但此后迁移继续失败。我发现原因是向下迁移在更改中途中止,然后当我再次尝试向下迁移时它失败了,因为已经进行了一些更改,因此列名不同以及其他类似问题。我最终通过摆弄 schema_migrations
table 修复了它,并手动将我的更改回滚到以前的版本,然后从那里上下迁移。
我的问题是,有没有办法告诉 Rails 到 运行 以事务模式迁移,如果代码失败,不提交事务?
Rails 已经 运行 您在事务中的迁移 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.
在执行模式更改时使用尊重事务的数据库取决于您。 MySQL(据我所知)没有,但 Postgres 绝对有。