Skip to content

[Fix #570] Change of foreign key should be considered as a column change #678

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

Merged
merged 1 commit into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ Metrics/AbcSize:
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 244
Max: 259

# Offense count: 1
# Configuration parameters: CountBlocks.
Expand Down
2 changes: 1 addition & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def annotate_one_file(file_name, info_block, position, options = {})
old_header = old_content.match(header_pattern).to_s
new_header = info_block.match(header_pattern).to_s

column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/
column_pattern = /^#[\t ]+[\w\*\.`]+[\t ]+.+$/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you leave a note as to why this would fix the problem?

Copy link
Contributor Author

@sashabelozerov sashabelozerov Dec 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctran Line with a foreign key info in an annotation text looks like:

# fk_rails_... (foreign_thing_id => foreign_things.id) ON DELETE => restrict

It didn't match the previous pattern because of dots at the end of the foreign key name (we have dots there because we truncate generated foreign key names at https://github.com/ctran/annotate_models/blob/develop/lib/annotate/annotate_models.rb#L477).

In turn, because a foreign key line didn't match the pattern, we didn't check it for changes at https://github.com/ctran/annotate_models/blob/develop/lib/annotate/annotate_models.rb#L525

Changes in the pattern solves this case.

Hope this helps.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean if the name doesn't end with "...", this pattern won't match anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctran No. [\w\*\.`] - this part of the regexp represents characters which a name can consist of, in any order. It will match both names with "..." and without them.

old_columns = old_header && old_header.scan(column_pattern).sort
new_columns = new_header && new_header.scan(column_pattern).sort

Expand Down
43 changes: 43 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,49 @@ def magic_comments_list_each
.to eq("# START\n#{@schema_info}# END\n\n#{@file_content}")
end

describe 'with existing annotation' do
context 'of a foreign key' do
before do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
],
[],
[
mock_foreign_key('fk_rails_cf2568e89e',
'foreign_thing_id',
'foreign_things',
'id',
on_delete: :cascade)
])
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_foreign_keys: true)
annotate_one_file
end

it 'should update foreign key constraint' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
],
[],
[
mock_foreign_key('fk_rails_cf2568e89e',
'foreign_thing_id',
'foreign_things',
'id',
on_delete: :restrict)
])
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', show_foreign_keys: true)
annotate_one_file
expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}")
end
end
end

describe 'with existing annotation => :before' do
before do
annotate_one_file position: :before
Expand Down