Skip to content

Commit 47e1b1e

Browse files
committed
Merge pull request #358 from sashabelozerov/on_delete_on_update
Annotate on_delete/on_update foreign key constraints
2 parents 91da93d + 7cf6457 commit 47e1b1e

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Metrics/MethodLength:
105105
# Offense count: 2
106106
# Configuration parameters: CountComments.
107107
Metrics/ModuleLength:
108-
Max: 507
108+
Max: 513
109109

110110
# Offense count: 7
111111
Metrics/PerceivedComplexity:

lib/annotate/annotate_models.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,14 @@ def get_foreign_key_info(klass, options={})
330330
max_size = foreign_keys.collect{|fk| fk.name.size}.max + 1
331331
foreign_keys.sort_by(&:name).each do |fk|
332332
ref_info = "#{fk.column} => #{fk.to_table}.#{fk.primary_key}"
333+
constraints_info = ''
334+
constraints_info += "ON DELETE => #{fk.on_delete} " if fk.on_delete
335+
constraints_info += "ON UPDATE => #{fk.on_update} " if fk.on_update
336+
constraints_info.strip!
333337
if options[:format_markdown]
334-
fk_info << sprintf("# * `%s`:\n# * **`%s`**\n", fk.name, ref_info)
338+
fk_info << sprintf("# * `%s`%s:\n# * **`%s`**\n", fk.name, constraints_info.blank? ? '' : " (_#{constraints_info}_)", ref_info)
335339
else
336-
fk_info << sprintf("# %-#{max_size}.#{max_size}s %s", fk.name, "(#{ref_info})").rstrip + "\n"
340+
fk_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", fk.name, "(#{ref_info})", constraints_info).rstrip + "\n"
337341
end
338342
end
339343

spec/annotate/annotate_models_spec.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
require 'active_support/core_ext/string'
66

77
describe AnnotateModels do
8-
def mock_foreign_key(name, from_column, to_table, to_column = 'id')
8+
def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
99
double("ForeignKeyDefinition",
1010
:name => name,
1111
:column => from_column,
1212
:to_table => to_table,
1313
:primary_key => to_column,
14+
:on_delete => constraints[:on_delete],
15+
:on_update => constraints[:on_update]
1416
)
1517
end
1618

@@ -197,6 +199,36 @@ def mock_column(name, type, options={})
197199
EOS
198200
end
199201

202+
it "should get foreign key info if on_delete/on_update options present" do
203+
klass = mock_class(:users, :id, [
204+
mock_column(:id, :integer),
205+
mock_column(:foreign_thing_id, :integer),
206+
],
207+
[
208+
mock_foreign_key(
209+
'fk_rails_02e851e3b7',
210+
'foreign_thing_id',
211+
'foreign_things',
212+
'id',
213+
on_delete: 'on_delete_value',
214+
on_update: 'on_update_value'
215+
)
216+
])
217+
expect(AnnotateModels.get_schema_info(klass, "Schema Info", :show_foreign_keys => true)).to eql(<<-EOS)
218+
# Schema Info
219+
#
220+
# Table name: users
221+
#
222+
# id :integer not null, primary key
223+
# foreign_thing_id :integer not null
224+
#
225+
# Foreign Keys
226+
#
227+
# fk_rails_02e851e3b7 (foreign_thing_id => foreign_things.id) ON DELETE => on_delete_value ON UPDATE => on_update_value
228+
#
229+
EOS
230+
end
231+
200232
it "should get schema info as RDoc" do
201233
klass = mock_class(:users, :id, [
202234
mock_column(:id, :integer),
@@ -261,7 +293,7 @@ def self.when_called_with(options = {})
261293
# notes :text(55) not null
262294
#
263295
EOS
264-
296+
265297
when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns:
266298
<<-EOS.strip_heredoc
267299
# Schema Info

0 commit comments

Comments
 (0)