Skip to content

Commit 6449e7d

Browse files
committed
Handle missing expression
Signed-off-by: Lovro Bikic <[email protected]>
1 parent b850e9b commit 6449e7d

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/annotate/annotate_models.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,14 @@ def get_check_constraint_info(klass, options = {})
371371

372372
max_size = check_constraints.map { |check_constraint| check_constraint.name.size }.max + 1
373373
check_constraints.sort_by(&:name).each do |check_constraint|
374+
expression = check_constraint.expression ? "(#{check_constraint.expression.squish})" : nil
375+
374376
cc_info << if options[:format_markdown]
375-
sprintf("# * `%s`: `(%s)`\n", check_constraint.name, check_constraint.expression.squish)
377+
cc_info_markdown = sprintf("# * `%s`", check_constraint.name)
378+
cc_info_markdown << sprintf(": `%s`", expression) if expression
379+
cc_info_markdown << "\n"
376380
else
377-
sprintf("# %-#{max_size}.#{max_size}s (%s)\n", check_constraint.name, check_constraint.expression.squish)
381+
sprintf("# %-#{max_size}.#{max_size}s %s", check_constraint.name, expression).rstrip + "\n"
378382
end
379383
end
380384

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ def mock_column(name, type, options = {})
784784
[
785785
mock_check_constraint('alive', 'age < 150'),
786786
mock_check_constraint('must_be_adult', 'age >= 18'),
787+
mock_check_constraint('missing_expression', nil),
787788
mock_check_constraint('multiline_test', <<~SQL)
788789
CASE
789790
WHEN (age >= 18) THEN (age <= 21)
@@ -804,9 +805,10 @@ def mock_column(name, type, options = {})
804805
#
805806
# Check Constraints
806807
#
807-
# alive (age < 150)
808-
# multiline_test (CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)
809-
# must_be_adult (age >= 18)
808+
# alive (age < 150)
809+
# missing_expression
810+
# multiline_test (CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)
811+
# must_be_adult (age >= 18)
810812
#
811813
EOS
812814
end
@@ -1585,6 +1587,7 @@ def mock_column(name, type, options = {})
15851587
let :check_constraints do
15861588
[
15871589
mock_check_constraint('min_name_length', 'LENGTH(name) > 2'),
1590+
mock_check_constraint('missing_expression', nil),
15881591
mock_check_constraint('multiline_test', <<~SQL)
15891592
CASE
15901593
WHEN (age >= 18) THEN (age <= 21)
@@ -1610,6 +1613,7 @@ def mock_column(name, type, options = {})
16101613
# ### Check Constraints
16111614
#
16121615
# * `min_name_length`: `(LENGTH(name) > 2)`
1616+
# * `missing_expression`
16131617
# * `multiline_test`: `(CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)`
16141618
#
16151619
EOS

0 commit comments

Comments
 (0)