Skip to content

Commit 2bf28a4

Browse files
authored
Merge pull request rails#53735 from kamipo/not_valid_constraint_should_not_in_create_table
NOT VALID constraints should not dump in `create_table`
2 parents dd33918 + 91301f4 commit 2bf28a4

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

activerecord/lib/active_record/schema_dumper.rb

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,17 @@ def table(table, stream)
207207
end
208208

209209
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?
211211
exclusion_constraints_in_create(table, tbl) if @connection.supports_exclusion_constraints?
212212
unique_constraints_in_create(table, tbl) if @connection.supports_unique_constraints?
213213

214214
tbl.puts " end"
215215

216+
if remaining
217+
tbl.puts
218+
tbl.print remaining.string
219+
end
220+
216221
stream.print tbl.string
217222
rescue => e
218223
stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
@@ -277,24 +282,37 @@ def index_parts(index)
277282

278283
def check_constraints_in_create(table, stream)
279284
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? }
284286

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(', ')}"
287290
end
288291

289-
parts << "validate: #{check_constraint.validate?.inspect}" unless check_constraint.validate?
290-
291-
" #{parts.join(', ')}"
292+
stream.puts check_constraint_statements.sort.join("\n")
292293
end
293294

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
295306
end
296307
end
297308

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+
298316
def foreign_keys(table, stream)
299317
if (foreign_keys = @connection.foreign_keys(table)).any?
300318
add_foreign_key_statements = foreign_keys.map do |foreign_key|

activerecord/test/cases/migration/check_constraint_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def test_schema_dumping_with_validate_false
196196

197197
output = dump_table_schema "trades"
198198

199-
assert_match %r{\s+t.check_constraint "quantity > 0", name: "quantity_check", validate: false$}, output
199+
assert_match %r{\s+add_check_constraint "trades", "quantity > 0", name: "quantity_check", validate: false$}, output
200200
end
201201

202202
def test_schema_dumping_with_validate_true

0 commit comments

Comments
 (0)