-
Notifications
You must be signed in to change notification settings - Fork 369
Add TooManyMigrationRuns cop to test performance #4850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johha
wants to merge
1
commit into
main
Choose a base branch
from
migration-cop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| module RuboCop | ||
| module Cop | ||
| module Migration | ||
| class TooManyMigrationRuns < Base | ||
| MSG = 'Too many migration runs (%d). Combine tests to reduce migrations. See spec/migrations/README.md for further guidance.'.freeze | ||
| MAX_CALLS = 4 | ||
|
|
||
| def on_new_investigation | ||
| calls = 0 | ||
| migrator_subject_names = [] | ||
| migrator_method_names = [] | ||
| migrator_let_names = [] | ||
| migrator_before_after_blocks = Set.new | ||
|
|
||
| extract_migrator_definitions(migrator_subject_names, migrator_method_names, | ||
| migrator_let_names, migrator_before_after_blocks) | ||
|
|
||
| count_migrator_calls(calls, migrator_subject_names, migrator_method_names, | ||
| migrator_let_names, migrator_before_after_blocks) | ||
| end | ||
|
|
||
| def extract_migrator_definitions(subject_names, method_names, let_names, before_after_blocks) | ||
| processed_source.ast.each_descendant(:def) do |node| | ||
| method_name = extract_migrator_method_name(node) | ||
| method_names << method_name if method_name | ||
| end | ||
|
|
||
| processed_source.ast.each_descendant(:block) do |node| | ||
| subject_name = extract_migrator_subject_name(node) | ||
| subject_names << subject_name if subject_name | ||
|
|
||
| let_name = extract_migrator_let_name(node) | ||
| let_names << let_name if let_name | ||
|
|
||
| before_after_blocks.add(node.object_id) if is_before_after_around_with_migrator?(node) | ||
| end | ||
| end | ||
|
|
||
| def count_migrator_calls(_calls, subjects, methods, lets, before_after_blocks) | ||
| call_count = count_before_after_migrations(before_after_blocks) | ||
| call_count += count_send_node_migrations(subjects, methods, lets, before_after_blocks) | ||
|
|
||
| add_offense(processed_source.ast, message: sprintf(MSG, call_count)) if call_count > MAX_CALLS | ||
| end | ||
|
|
||
| def count_before_after_migrations(before_after_blocks) | ||
| call_count = 0 | ||
| processed_source.ast.each_descendant(:block) do |node| | ||
| call_count += count_direct_migrations_in_node(node) if before_after_blocks.include?(node.object_id) | ||
| end | ||
| call_count | ||
| end | ||
|
|
||
| def count_send_node_migrations(subjects, methods, lets, before_after_blocks) | ||
| call_count = 0 | ||
| processed_source.ast.each_descendant(:send) do |node| | ||
| next if node.each_ancestor(:block).any? { |a| before_after_blocks.include?(a.object_id) } | ||
|
|
||
| call_count += count_migration_call(node, subjects, methods, lets) | ||
| end | ||
| call_count | ||
| end | ||
|
|
||
| def count_migration_call(node, subjects, methods, lets) | ||
| return 1 if direct_migrator_call?(node) | ||
| return 1 if helper_migration_call?(node, subjects, methods, lets) | ||
|
|
||
| 0 | ||
| end | ||
|
|
||
| def direct_migrator_call?(node) | ||
| return false unless node.method_name == :run && node.receiver&.source&.include?('Migrator') | ||
|
|
||
| !inside_definition?(node) | ||
| end | ||
|
|
||
| def helper_migration_call?(node, subjects, methods, lets) | ||
| subjects.include?(node.method_name) || | ||
| lets.include?(node.method_name) || | ||
| methods.include?(node.method_name) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def extract_migrator_method_name(node) | ||
| return nil unless node.type == :def | ||
| return nil unless node.source.include?('Sequel::Migrator.run') | ||
|
|
||
| node.method_name | ||
| end | ||
|
|
||
| def extract_migrator_subject_name(node) | ||
| return nil unless node.send_node.method_name == :subject | ||
| return nil unless node.source.include?('Sequel::Migrator.run') | ||
|
|
||
| first_arg = node.send_node.first_argument | ||
| first_arg&.sym_type? ? first_arg.value : nil | ||
| end | ||
|
|
||
| def extract_migrator_let_name(node) | ||
| return nil unless %i[let let!].include?(node.send_node.method_name) | ||
| return nil unless node.source.include?('Sequel::Migrator.run') | ||
|
|
||
| first_arg = node.send_node.first_argument | ||
| first_arg&.sym_type? ? first_arg.value : nil | ||
| end | ||
|
|
||
| def is_before_after_around_with_migrator?(node) | ||
| return false unless node.send_node | ||
| return false unless %i[before after around].include?(node.send_node.method_name) | ||
|
|
||
| node.source.include?('Sequel::Migrator.run') | ||
| end | ||
|
|
||
| def count_direct_migrations_in_node(node) | ||
| count = 0 | ||
| node.each_descendant(:send) do |descendant| | ||
| count += 1 if descendant.method_name == :run && descendant.receiver&.source&.include?('Migrator') | ||
| end | ||
| count | ||
| end | ||
|
|
||
| def inside_definition?(node) | ||
| node.each_ancestor(:def).any? { |a| a.source.include?('Sequel::Migrator.run') } || | ||
| node.each_ancestor(:block).any? do |a| | ||
| %i[subject let let!].include?(a.send_node&.method_name) && a.source.include?('Sequel::Migrator.run') | ||
| end || | ||
| node.each_ancestor(:block).any? do |a| | ||
| %i[before after around].include?(a.send_node&.method_name) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable
calls. Can also be removed from the parameters ofcount_migrator_calls.