Fix: Preventing Duplicate Migrations on the Same Shard #580
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
Currently, the migration process runs multiple times on a given shard due to how shards are fetched and processed. This occurs because the list of shards is obtained using the expression
migrations.map(&:shards).flatten.map(&:to_s)
, resulting in an array of shard names. As a consequence, if a shard has 'n' associated migrations, the migration process will be executed 'n' times on that shard.Proposed Solution
To address this issue and ensure that migrations are not duplicated on the same shard, we can modify the process as follows:
Instead of directly flattening the
migrations.map(&:shards)
array, we get unique elementsmigrations.map(&:shards).uniq
.This approach guarantees that each shard will only appear once in the resulting array, preventing duplicate migrations on the same shard.
Benefits
Improves the efficiency of the migration process by preventing unnecessary repeated migrations on the same shard.