Skip to content

Commit

Permalink
[Fix #570] Change of foreign key should be considered as a column cha…
Browse files Browse the repository at this point in the history
…nge (#678)
  • Loading branch information
sashabelozerov authored and ctran committed Dec 18, 2019
1 parent a631f5a commit 5219c7e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
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 @@ -518,7 +518,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 ]+.+$/
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 @@ -1758,6 +1758,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

0 comments on commit 5219c7e

Please sign in to comment.