From 5e9dc652f2036cee5bfffd84cd8513c0eebe9390 Mon Sep 17 00:00:00 2001 From: Alexander Belozerov Date: Sat, 26 Oct 2019 23:22:08 +0600 Subject: [PATCH] [Fix #570] Change of foreign key should be considered as a column change --- .rubocop_todo.yml | 2 +- lib/annotate/annotate_models.rb | 2 +- spec/lib/annotate/annotate_models_spec.rb | 43 +++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index aa519ba7f..bf7aeaab8 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -414,7 +414,7 @@ Metrics/AbcSize: # Configuration parameters: CountComments, ExcludedMethods. # ExcludedMethods: refine Metrics/BlockLength: - Max: 244 + Max: 259 # Offense count: 1 # Configuration parameters: CountBlocks. diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index b306d0b84..6ac1ed282 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -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 ]+.+$/ old_columns = old_header && old_header.scan(column_pattern).sort new_columns = new_header && new_header.scan(column_pattern).sort diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index f95a9e290..c430092ae 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -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