My understanding is that all migrations are wrapped in a transaction, meaning, if a change for 1 row, like adding null: false, in a table fails, every other row will fail. When would you not want this to happen with disable_ddl_transaction
?
When would you want to use disable_ddl_transaction in Rails?
Asked Answered
All migrations are wrapped in transaction by default so that they can be rolled back when they are failed.
You need to use disable_ddl_transaction
when you want to do something that can not excute inside a transaction. You can check Transactional Migrations
section of Migration document for example.
When you want to add an index on a large table using a concurrent (algorithm) approach, which prevents the table from getting locked while the index is being added, you need to disable_ddl_transaction
since the concurrent approach can't be executed inside the transaction. Please find more details here.
© 2022 - 2025 — McMap. All rights reserved.
disable_ddl_transaction!
called when the class is loaded, before its#up
method is evoked. – Spooky