@@ -131,7 +131,7 @@ def retrieve_indexes_from_table(klass)
131
131
# to create a comment block containing a line for
132
132
# each column. The line contains the column name,
133
133
# the type (and length), and any optional attributes
134
- def get_schema_info ( klass , header , options = { } )
134
+ def get_schema_info ( klass , header , options = { } ) # rubocop:disable Metrics/MethodLength
135
135
info = "# #{ header } \n "
136
136
info << get_schema_header_text ( klass , options )
137
137
@@ -178,6 +178,10 @@ def get_schema_info(klass, header, options = {})
178
178
info << get_foreign_key_info ( klass , options )
179
179
end
180
180
181
+ if options [ :show_check_constraints ] && klass . table_exists?
182
+ info << get_check_constraint_info ( klass , options )
183
+ end
184
+
181
185
info << get_schema_footer_text ( klass , options )
182
186
end
183
187
@@ -352,6 +356,35 @@ def get_foreign_key_info(klass, options = {})
352
356
fk_info
353
357
end
354
358
359
+ def get_check_constraint_info ( klass , options = { } )
360
+ cc_info = if options [ :format_markdown ]
361
+ "#\n # ### Check Constraints\n #\n "
362
+ else
363
+ "#\n # Check Constraints\n #\n "
364
+ end
365
+
366
+ return '' unless klass . connection . respond_to? ( :supports_check_constraints? ) &&
367
+ klass . connection . supports_check_constraints? && klass . connection . respond_to? ( :check_constraints )
368
+
369
+ check_constraints = klass . connection . check_constraints ( klass . table_name )
370
+ return '' if check_constraints . empty?
371
+
372
+ max_size = check_constraints . map { |check_constraint | check_constraint . name . size } . max + 1
373
+ check_constraints . sort_by ( &:name ) . each do |check_constraint |
374
+ expression = check_constraint . expression ? "(#{ check_constraint . expression . squish } )" : nil
375
+
376
+ cc_info << if options [ :format_markdown ]
377
+ cc_info_markdown = sprintf ( "# * `%s`" , check_constraint . name )
378
+ cc_info_markdown << sprintf ( ": `%s`" , expression ) if expression
379
+ cc_info_markdown << "\n "
380
+ else
381
+ sprintf ( "# %-#{ max_size } .#{ max_size } s %s" , check_constraint . name , expression ) . rstrip + "\n "
382
+ end
383
+ end
384
+
385
+ cc_info
386
+ end
387
+
355
388
# Add a schema block to a file. If the file already contains
356
389
# a schema info block (a comment starting with "== Schema Information"),
357
390
# check if it matches the block that is already there. If so, leave it be.
0 commit comments