When would you want to use disable_ddl_transaction in Rails?
Asked Answered
S

2

33

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?

Sorbose answered 6/9, 2017 at 4:30 Comment(0)
M
35

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.

Malta answered 6/9, 2017 at 5:48 Comment(1)
One example that comes to mind is when adding a custom/user-defined Enum in PGSQL. They cant be defined in a transaction, so adding them via a Rails migration requires disable_ddl_transaction! called when the class is loaded, before its #up method is evoked.Spooky
Q
11

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.

Quantize answered 12/12, 2022 at 16:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.