Merge pull request #46339 from TAGraves/tg-validate-in-schema-rb

Adds validate to foreign keys and check constraints in schema.rb
This commit is contained in:
Yasuo Honda 2022-10-26 08:54:16 +09:00 committed by GitHub
commit 1562651e5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 0 deletions

@ -1,3 +1,11 @@
* Adds `validate` to foreign keys and check constraints in schema.rb
Previously, `schema.rb` would not record if `validate: false` had been used when adding a foreign key or check
constraint, so restoring a database from the schema could result in foreign keys or check constraints being
incorrectly validated.
*Tommy Graves*
* Adapter `#execute` methods now accept an `allow_retry` option. When set to `true`, the SQL statement will be
retried, up to the database's configured `connection_retries` value, upon encountering connection-related errors.

@ -246,6 +246,8 @@ def check_constraints_in_create(table, stream)
parts << "name: #{check_constraint.name.inspect}"
end
parts << "validate: #{check_constraint.validate?.inspect}" unless check_constraint.validate?
" #{parts.join(', ')}"
end
@ -276,6 +278,7 @@ def foreign_keys(table, stream)
parts << "on_update: #{foreign_key.on_update.inspect}" if foreign_key.on_update
parts << "on_delete: #{foreign_key.on_delete.inspect}" if foreign_key.on_delete
parts << "deferrable: #{foreign_key.deferrable.inspect}" if foreign_key.deferrable
parts << "validate: #{foreign_key.validate?.inspect}" unless foreign_key.validate?
" #{parts.join(', ')}"
end

@ -157,6 +157,22 @@ def test_validate_non_existing_check_constraint_raises
@connection.validate_check_constraint :trades, name: "quantity_check"
end
end
def test_schema_dumping_with_validate_false
@connection.add_check_constraint :trades, "quantity > 0", name: "quantity_check", validate: false
output = dump_table_schema "trades"
assert_match %r{\s+t.check_constraint "quantity > 0", name: "quantity_check", validate: false$}, output
end
def test_schema_dumping_with_validate_true
@connection.add_check_constraint :trades, "quantity > 0", name: "quantity_check", validate: true
output = dump_table_schema "trades"
assert_match %r{\s+t.check_constraint "quantity > 0", name: "quantity_check"$}, output
end
else
# Check constraint should still be created, but should not be invalid
def test_add_invalid_check_constraint

@ -467,6 +467,22 @@ def test_validate_constraint_by_name
@connection.validate_constraint :astronauts, "fancy_named_fk"
assert_predicate @connection.foreign_keys("astronauts").first, :validated?
end
def test_schema_dumping_with_validate_false
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", validate: false
output = dump_table_schema "astronauts"
assert_match %r{\s+add_foreign_key "astronauts", "rockets", validate: false$}, output
end
def test_schema_dumping_with_validate_true
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", validate: true
output = dump_table_schema "astronauts"
assert_match %r{\s+add_foreign_key "astronauts", "rockets"$}, output
end
else
# Foreign key should still be created, but should not be invalid
def test_add_invalid_foreign_key