@@ -509,23 +509,13 @@ def annotate_one_file(file_name, info_block, position, options = {})
509
509
return false unless File . exist? ( file_name )
510
510
old_content = File . read ( file_name )
511
511
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 ]
523
513
524
514
abort "annotate error. #{ file_name } needs to be updated, but annotate was run with `--frozen`." if options [ :frozen ]
525
515
526
516
# 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 " : ""
529
519
wrapped_info_block = "#{ wrapper_open } #{ info_block } #{ wrapper_close } "
530
520
531
521
old_annotation = old_content . match ( annotate_pattern ( options ) ) . to_s
@@ -932,6 +922,34 @@ def mb_chars_ljust(string, length)
932
922
string [ 0 ..length -1 ]
933
923
end
934
924
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
935
953
end
936
954
937
955
class BadModelFileError < LoadError
0 commit comments