1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #32299 from davidstosik/expose-fk-ignore-pattern

Expose foreign key name ignore pattern in configuration
This commit is contained in:
Guillermo Iguaran 2018-03-27 11:41:12 -05:00 committed by GitHub
commit 7b668e2d42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 3 deletions

View file

@ -43,6 +43,7 @@ module ActiveRecord
autoload :DatabaseConfigurations
autoload :DynamicMatchers
autoload :Enum
autoload :ForeignKeys
autoload :InternalMetadata
autoload :Explain
autoload :Inheritance

View file

@ -101,6 +101,10 @@ module ActiveRecord
end
alias validated? validate?
def export_name_on_schema_dump?
name !~ ActiveRecord::SchemaDumper.fk_ignore_pattern
end
def defined_for?(to_table_ord = nil, to_table: nil, **options)
if to_table_ord
self.to_table == to_table_ord.to_s

View file

@ -1324,7 +1324,7 @@ module ActiveRecord
identifier = "#{table_name}_#{options.fetch(:column)}_fk"
hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)
"fk_rails_#{hashed_identifier}"
"#{ActiveRecord::ForeignKeys::PREFIX}_#{hashed_identifier}"
end
end

View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
module ActiveRecord
module ForeignKeys
# The prefix used by Rails to name unnamed foreign keys.
PREFIX = "fk_rails"
# Default regular expression used by Rails to determine if a foreign key
# name was generated.
DEFAULT_IGNORE_PATTERN = /^#{PREFIX}_[0-9a-f]{10}$/
end
end

View file

@ -17,6 +17,12 @@ module ActiveRecord
# Only strings are accepted if ActiveRecord::Base.schema_format == :sql.
cattr_accessor :ignore_tables, default: []
##
# :singleton-method:
# Specify a custom regular expression matching foreign keys which name
# should not be dumped to db/schema.rb.
cattr_accessor :fk_ignore_pattern, default: ActiveRecord::ForeignKeys::DEFAULT_IGNORE_PATTERN
class << self
def dump(connection = ActiveRecord::Base.connection, stream = STDOUT, config = ActiveRecord::Base)
connection.create_schema_dumper(generate_options(config)).dump(stream)
@ -210,7 +216,7 @@ HEADER
parts << "primary_key: #{foreign_key.primary_key.inspect}"
end
if foreign_key.name !~ /^fk_rails_[0-9a-f]{10}$/
if foreign_key.export_name_on_schema_dump?
parts << "name: #{foreign_key.name.inspect}"
end

View file

@ -306,6 +306,17 @@ if ActiveRecord::Base.connection.supports_foreign_keys?
assert_match %r{\s+add_foreign_key "fk_test_has_fk", "fk_test_has_pk", column: "fk_id", primary_key: "pk_id", name: "fk_name"$}, output
end
def test_schema_dumping_with_custom_fk_ignore_pattern
original_pattern = ActiveRecord::SchemaDumper.fk_ignore_pattern
ActiveRecord::SchemaDumper.fk_ignore_pattern = /^ignored_/
@connection.add_foreign_key :astronauts, :rockets, name: :ignored_fk_astronauts_rockets
output = dump_table_schema "astronauts"
assert_match %r{\s+add_foreign_key "astronauts", "rockets"$}, output
ActiveRecord::SchemaDumper.fk_ignore_pattern = original_pattern
end
def test_schema_dumping_on_delete_and_on_update_options
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :nullify, on_update: :cascade

View file

@ -400,10 +400,16 @@ by adding the following to your `application.rb` file:
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
```
The schema dumper adds one additional configuration option:
The schema dumper adds two additional configuration options:
* `ActiveRecord::SchemaDumper.ignore_tables` accepts an array of tables that should _not_ be included in any generated schema file.
* `ActiveRecord::SchemaDumper.fk_ignore_pattern` allows setting a different regular
expression that will be used to decide whether a foreign key's name should be
dumped to db/schema.rb or not. By default, foreign key names starting with
`fk_rails_` are not exported to the database schema dump.
Defaults to `/^fk_rails_[0-9a-f]{10}$/`.
### Configuring Action Controller
`config.action_controller` includes a number of configuration settings: