Skip to content

Commit 0070422

Browse files
committed
missing_non_null_constraint: ignore database views
1 parent b0d597f commit 0070422

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/active_record_doctor/detectors/missing_non_null_constraint.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ def message(column:, table:)
2424

2525
def detect
2626
table_models = models.select(&:table_exists?).group_by(&:table_name)
27+
views = connection.views
2728

2829
table_models.each do |table, models|
2930
next if ignored?(table, config(:ignore_tables))
31+
next if views.include?(table)
3032

3133
concrete_models = models.reject do |model|
3234
model.abstract_class? || sti_base_model?(model)

test/active_record_doctor/detectors/missing_non_null_constraint_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,26 @@ def test_not_null_check_constraint
259259
refute_problems
260260
end
261261

262+
def test_views_are_ignored
263+
connection = ActiveRecord::Base.connection
264+
connection.create_table(:old_comments, force: true) do |t|
265+
t.integer :user_id
266+
end
267+
268+
connection.execute(<<-SQL)
269+
CREATE VIEW comments AS SELECT * FROM old_comments
270+
SQL
271+
272+
Context.define_model(:Comment) do
273+
belongs_to :user, required: true
274+
end
275+
276+
refute_problems
277+
ensure
278+
connection.execute("DROP VIEW comments")
279+
connection.drop_table(:old_comments)
280+
end
281+
262282
def test_config_ignore_tables
263283
Context.create_table(:users) do |t|
264284
t.string :name, null: true

0 commit comments

Comments
 (0)