Skip to content

Commit 7cc5481

Browse files
committed
Squish check constraint expressions
Signed-off-by: Lovro Bikic <[email protected]>
1 parent 6501d87 commit 7cc5481

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/annotate/annotate_models.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ def get_check_constraint_info(klass, options = {})
372372
max_size = check_constraints.map { |check_constraint| check_constraint.name.size }.max + 1
373373
check_constraints.sort_by(&:name).each do |check_constraint|
374374
cc_info << if options[:format_markdown]
375-
sprintf("# * `%s`: `(%s)`\n", check_constraint.name, check_constraint.expression)
375+
sprintf("# * `%s`: `(%s)`\n", check_constraint.name, check_constraint.expression.squish)
376376
else
377-
sprintf("# %-#{max_size}.#{max_size}s (%s)\n", check_constraint.name, check_constraint.expression)
377+
sprintf("# %-#{max_size}.#{max_size}s (%s)\n", check_constraint.name, check_constraint.expression.squish)
378378
end
379379
end
380380

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,13 @@ def mock_column(name, type, options = {})
787787
let :check_constraints do
788788
[
789789
mock_check_constraint('alive', 'age < 150'),
790-
mock_check_constraint('must_be_adult', 'age >= 18')
790+
mock_check_constraint('must_be_adult', 'age >= 18'),
791+
mock_check_constraint('multiline_test', <<~SQL)
792+
CASE
793+
WHEN (age >= 18) THEN (age <= 21)
794+
ELSE true
795+
END
796+
SQL
791797
]
792798
end
793799

@@ -802,8 +808,9 @@ def mock_column(name, type, options = {})
802808
#
803809
# Check Constraints
804810
#
805-
# alive (age < 150)
806-
# must_be_adult (age >= 18)
811+
# alive (age < 150)
812+
# multiline_test (CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)
813+
# must_be_adult (age >= 18)
807814
#
808815
EOS
809816
end
@@ -1581,7 +1588,13 @@ def mock_column(name, type, options = {})
15811588
context 'when check constraints are defined' do
15821589
let :check_constraints do
15831590
[
1584-
mock_check_constraint('min_name_length', 'LENGTH(name) > 2')
1591+
mock_check_constraint('min_name_length', 'LENGTH(name) > 2'),
1592+
mock_check_constraint('multiline_test', <<~SQL)
1593+
CASE
1594+
WHEN (age >= 18) THEN (age <= 21)
1595+
ELSE true
1596+
END
1597+
SQL
15851598
]
15861599
end
15871600

@@ -1601,6 +1614,7 @@ def mock_column(name, type, options = {})
16011614
# ### Check Constraints
16021615
#
16031616
# * `min_name_length`: `(LENGTH(name) > 2)`
1617+
# * `multiline_test`: `(CASE WHEN (age >= 18) THEN (age <= 21) ELSE true END)`
16041618
#
16051619
EOS
16061620
end

0 commit comments

Comments
 (0)