@@ -207,12 +207,17 @@ def table(table, stream)
207
207
end
208
208
209
209
indexes_in_create ( table , tbl )
210
- check_constraints_in_create ( table , tbl ) if @connection . supports_check_constraints?
210
+ remaining = check_constraints_in_create ( table , tbl ) if @connection . supports_check_constraints?
211
211
exclusion_constraints_in_create ( table , tbl ) if @connection . supports_exclusion_constraints?
212
212
unique_constraints_in_create ( table , tbl ) if @connection . supports_unique_constraints?
213
213
214
214
tbl . puts " end"
215
215
216
+ if remaining
217
+ tbl . puts
218
+ tbl . print remaining . string
219
+ end
220
+
216
221
stream . print tbl . string
217
222
rescue => e
218
223
stream . puts "# Could not dump table #{ table . inspect } because of following #{ e . class } "
@@ -277,24 +282,37 @@ def index_parts(index)
277
282
278
283
def check_constraints_in_create ( table , stream )
279
284
if ( check_constraints = @connection . check_constraints ( table ) ) . any?
280
- add_check_constraint_statements = check_constraints . map do |check_constraint |
281
- parts = [
282
- "t.check_constraint #{ check_constraint . expression . inspect } "
283
- ]
285
+ check_valid , check_invalid = check_constraints . partition { |chk | chk . validate? }
284
286
285
- if check_constraint . export_name_on_schema_dump?
286
- parts << "name: #{ check_constraint . name . inspect } "
287
+ unless check_valid . empty?
288
+ check_constraint_statements = check_valid . map do |check |
289
+ " t.check_constraint #{ check_parts ( check ) . join ( ', ' ) } "
287
290
end
288
291
289
- parts << "validate: #{ check_constraint . validate? . inspect } " unless check_constraint . validate?
290
-
291
- " #{ parts . join ( ', ' ) } "
292
+ stream . puts check_constraint_statements . sort . join ( "\n " )
292
293
end
293
294
294
- stream . puts add_check_constraint_statements . sort . join ( "\n " )
295
+ unless check_invalid . empty?
296
+ remaining = StringIO . new
297
+ table_name = remove_prefix_and_suffix ( table ) . inspect
298
+
299
+ add_check_constraint_statements = check_invalid . map do |check |
300
+ " add_check_constraint #{ ( [ table_name ] + check_parts ( check ) ) . join ( ', ' ) } "
301
+ end
302
+
303
+ remaining . puts add_check_constraint_statements . sort . join ( "\n " )
304
+ remaining
305
+ end
295
306
end
296
307
end
297
308
309
+ def check_parts ( check )
310
+ check_parts = [ check . expression . inspect ]
311
+ check_parts << "name: #{ check . name . inspect } " if check . export_name_on_schema_dump?
312
+ check_parts << "validate: #{ check . validate? . inspect } " unless check . validate?
313
+ check_parts
314
+ end
315
+
298
316
def foreign_keys ( table , stream )
299
317
if ( foreign_keys = @connection . foreign_keys ( table ) ) . any?
300
318
add_foreign_key_statements = foreign_keys . map do |foreign_key |
0 commit comments