Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/active_record_doctor/detectors/missing_foreign_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def detect
next unless looks_like_foreign_key?(column)
next if foreign_key?(table, column)
next if polymorphic_foreign_key?(table, column)
next if model_destroyed_async?(table, column)

problem!(table: table, column: column.name)
end
Expand All @@ -49,6 +50,18 @@ def polymorphic_foreign_key?(table, column)
another_column.name == type_column_name
end
end

def model_destroyed_async?(table, column)
# Check if there are any models having `has_many ..., dependent: :destroy_async`
# referencing the specified table.
models.any? do |model|
model.reflect_on_all_associations(:has_many).any? do |reflection|
reflection.options[:dependent] == :destroy_async &&
reflection.foreign_key == column.name &&
reflection.klass.table_name == table
end
end
end
end
end
end
15 changes: 15 additions & 0 deletions test/active_record_doctor/detectors/missing_foreign_keys_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ def test_uuid_missing_foreign_key_is_reported
OUTPUT
end

def test_destroy_async_is_not_reported
Context.create_table(:companies).define_model do
# We need an ActiveJob job defined to appease the ActiveRecord
class_attribute :destroy_association_async_job, default: Class.new

has_many :users, dependent: :destroy_async
end

Context.create_table(:users) do |t|
t.references :company, foreign_key: false
end.define_model

refute_problems
end

def test_config_ignore_models
Context.create_table(:companies)
Context.create_table(:users) do |t|
Expand Down