Skip to content

Add new Rails/InBatchesMigrationTransaction cop#1606

Open
bart-westenenk-bex wants to merge 2 commits intorubocop:masterfrom
bart-westenenk-bex:1601-no-in-batches-in-ddl-transaction
Open

Add new Rails/InBatchesMigrationTransaction cop#1606
bart-westenenk-bex wants to merge 2 commits intorubocop:masterfrom
bart-westenenk-bex:1601-no-in-batches-in-ddl-transaction

Conversation

@bart-westenenk-bex
Copy link
Copy Markdown

@bart-westenenk-bex bart-westenenk-bex commented Apr 21, 2026

This new cop enforces the use of disable_ddl_transaction! when a migration uses in_batches to go over a lot of records.
In general, when you use in_batches within a transaction you will negate the effect of using in_batches and when deploying the migration you will put a full write lock on the full relation instead of the batched version of it.

Since you must always check yourself if it is sensible to use disable_ddl_transaction!, I have not implemented an autocorrector for this cop.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide. ==> Add Avoid in_batches without disable_ddl_transaction rails-style-guide#377

@bart-westenenk-bex bart-westenenk-bex changed the title [Feature 1601] Add a cop to enforce disable_ddl_transaction! in migra… Add new Rails/InBatchesMigrationTransaction cop Apr 23, 2026
@bart-westenenk-bex bart-westenenk-bex force-pushed the 1601-no-in-batches-in-ddl-transaction branch 2 times, most recently from 5e2c384 to e21684b Compare April 23, 2026 07:17
Introduces Rails/InBatchesMigrationTransaction cop. This will prevent write-locking parts of big (often read heavy) tables during a data migration that is performed with an online application.
@bart-westenenk-bex bart-westenenk-bex force-pushed the 1601-no-in-batches-in-ddl-transaction branch from e21684b to 4b1ee96 Compare April 23, 2026 07:19
@bart-westenenk-bex bart-westenenk-bex marked this pull request as ready for review April 23, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant