Commit 270e1c20 by Tim Vergenz Committed by Cuong Tran

Do not include auto-generated foreign key names in schema dump (#408)

* add(annotate_models_spec): test coverage * change(annotate_models#get_foreign_key_info): elide auto-generated fk names Closes #404.
parent 2d7694ef
......@@ -355,17 +355,19 @@ module AnnotateModels
foreign_keys = klass.connection.foreign_keys(klass.table_name)
return '' if foreign_keys.empty?
max_size = foreign_keys.collect{|fk| fk.name.size}.max + 1
foreign_keys.sort_by(&:name).each do |fk|
format_name = ->(fk) { fk.name.gsub(/(?<=^fk_rails_)[0-9a-f]{10}$/, '...') }
max_size = foreign_keys.map(&format_name).map(&:size).max + 1
foreign_keys.sort_by {|fk| [format_name.call(fk), fk.column]}.each do |fk|
ref_info = "#{fk.column} => #{fk.to_table}.#{fk.primary_key}"
constraints_info = ''
constraints_info += "ON DELETE => #{fk.on_delete} " if fk.on_delete
constraints_info += "ON UPDATE => #{fk.on_update} " if fk.on_update
constraints_info.strip!
if options[:format_markdown]
fk_info << sprintf("# * `%s`%s:\n# * **`%s`**\n", fk.name, constraints_info.blank? ? '' : " (_#{constraints_info}_)", ref_info)
fk_info << sprintf("# * `%s`%s:\n# * **`%s`**\n", format_name.call(fk), constraints_info.blank? ? '' : " (_#{constraints_info}_)", ref_info)
else
fk_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", fk.name, "(#{ref_info})", constraints_info).rstrip + "\n"
fk_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", format_name.call(fk), "(#{ref_info})", constraints_info).rstrip + "\n"
end
end
......
......@@ -184,9 +184,19 @@ EOS
],
[
mock_foreign_key(
'fk_rails_02e851e3b7',
'fk_rails_cf2568e89e',
'foreign_thing_id',
'foreign_things'
),
mock_foreign_key(
'custom_fk_name',
'other_thing_id',
'other_things'
),
mock_foreign_key(
'fk_rails_a70234b26c',
'third_thing_id',
'third_things'
)
])
expect(AnnotateModels.get_schema_info(klass, "Schema Info", :show_foreign_keys => true)).to eql(<<-EOS)
......@@ -199,7 +209,9 @@ EOS
#
# Foreign Keys
#
# fk_rails_02e851e3b7 (foreign_thing_id => foreign_things.id)
# custom_fk_name (other_thing_id => other_things.id)
# fk_rails_... (foreign_thing_id => foreign_things.id)
# fk_rails_... (third_thing_id => third_things.id)
#
EOS
end
......@@ -229,7 +241,7 @@ EOS
#
# Foreign Keys
#
# fk_rails_02e851e3b7 (foreign_thing_id => foreign_things.id) ON DELETE => on_delete_value ON UPDATE => on_update_value
# fk_rails_... (foreign_thing_id => foreign_things.id) ON DELETE => on_delete_value ON UPDATE => on_update_value
#
EOS
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment