Skip to content

Commit cd21a73

Browse files
[Fix #630] do not re-annotate when wrapper matches column_pattern
1 parent f602bd9 commit cd21a73

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

lib/annotate/annotate_models.rb

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -509,23 +509,13 @@ def annotate_one_file(file_name, info_block, position, options = {})
509509
return false unless File.exist?(file_name)
510510
old_content = File.read(file_name)
511511
return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
512-
513-
# Ignore the Schema version line because it changes with each migration
514-
header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
515-
old_header = old_content.match(header_pattern).to_s
516-
new_header = info_block.match(header_pattern).to_s
517-
518-
column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/
519-
old_columns = old_header && old_header.scan(column_pattern).sort
520-
new_columns = new_header && new_header.scan(column_pattern).sort
521-
522-
return false if old_columns == new_columns && !options[:force]
512+
return false if columns_unchanged?(old_content, info_block, options) && !options[:force]
523513

524514
abort "annotate error. #{file_name} needs to be updated, but annotate was run with `--frozen`." if options[:frozen]
525515

526516
# Replace inline the old schema info with the new schema info
527-
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
528-
wrapper_close = options[:wrapper_close] ? "# #{options[:wrapper_close]}\n" : ""
517+
wrapper_open = options[:wrapper_open] ? "#{wrapper_line(options[:wrapper_open])}\n" : ""
518+
wrapper_close = options[:wrapper_close] ? "#{wrapper_line(options[:wrapper_close])}\n" : ""
529519
wrapped_info_block = "#{wrapper_open}#{info_block}#{wrapper_close}"
530520

531521
old_annotation = old_content.match(annotate_pattern(options)).to_s
@@ -932,6 +922,34 @@ def mb_chars_ljust(string, length)
932922
string[0..length-1]
933923
end
934924
end
925+
926+
def wrapper_line(wrapper)
927+
"# #{wrapper}"
928+
end
929+
930+
def columns_unchanged?(old_content, info_block, options)
931+
# Ignore the Schema version line because it changes with each migration
932+
header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
933+
old_header = old_content.match(header_pattern).to_s
934+
new_header = info_block.match(header_pattern).to_s
935+
936+
column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/
937+
old_columns = []
938+
new_columns = []
939+
940+
if old_header
941+
old_columns = old_header.scan(column_pattern).sort
942+
old_columns.delete(wrapper_line(options[:wrapper_open])) if options[:wrapper_open]
943+
old_columns.delete(wrapper_line(options[:wrapper_close])) if options[:wrapper_close]
944+
end
945+
if new_header
946+
new_columns = new_header.scan(column_pattern).sort
947+
new_columns.delete(wrapper_line(options[:wrapper_open])) if options[:wrapper_open]
948+
new_columns.delete(wrapper_line(options[:wrapper_close])) if options[:wrapper_close]
949+
end
950+
951+
old_columns == new_columns
952+
end
935953
end
936954

937955
class BadModelFileError < LoadError

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,13 +1634,14 @@ def write_model(file_name, file_content)
16341634
def annotate_one_file(options = {})
16351635
Annotate.set_defaults(options)
16361636
options = Annotate.setup_options(options)
1637-
AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options)
1637+
result = AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options)
16381638

16391639
# Wipe settings so the next call will pick up new values...
16401640
Annotate.instance_variable_set('@has_set_defaults', false)
16411641
Annotate::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' }
16421642
Annotate::FLAG_OPTIONS.each { |key| ENV[key.to_s] = '' }
16431643
Annotate::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
1644+
result
16441645
end
16451646

16461647
def magic_comments_list_each
@@ -1793,6 +1794,21 @@ class User < ActiveRecord::Base
17931794
end
17941795
end
17951796

1797+
describe 'wrapper value matches column pattern' do
1798+
let(:wrapper) { 'Schema generated with `annotate`' }
1799+
1800+
it 'does not consider this as a new column and does not try to re-annotate' do
1801+
res = annotate_one_file wrapper_open: wrapper, wrapper_close: wrapper
1802+
expect(res).to eq(true)
1803+
1804+
expect(File.read(@model_file_name))
1805+
.to eq("# #{wrapper}\n#{@schema_info}# #{wrapper}\n\n#{@file_content}")
1806+
1807+
res = annotate_one_file wrapper_open: wrapper, wrapper_close: wrapper
1808+
expect(res).to eq(false) # Model file unchanged
1809+
end
1810+
end
1811+
17961812
describe "if a file can't be annotated" do
17971813
before do
17981814
allow(AnnotateModels).to receive(:get_loaded_model_by_path).with('user').and_return(nil)

0 commit comments

Comments
 (0)