djezzzl / database_consistency

The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash on inexistant column validation

ojab opened this issue · comments

commented
rails new tst
cd tst
rails g model book
rails db:create db:migrate

and model

class XxxValidator < ActiveModel::Validator
  def validate(_record)
  end
end

class Book < ApplicationRecord
  validates :title, presence: true, xxx: true
end

leads to crash and report a bug request:

…/database_consistency-1.1.5/lib/database_consistency/checkers/validators_fraction_checkers/column_presence_checker.rb:47:in `column': Missing column in books for title (DatabaseConsistency::Errors::MissingField)
	from …/database_consistency-1.1.5/lib/database_consistency/checkers/validators_fraction_checkers/column_presence_checker.rb:32:in `check'
	from …/database_consistency-1.1.5/lib/database_consistency/checkers/base_checker.rb:25:in `report'
	from …/database_consistency-1.1.5/lib/database_consistency/checkers/base_checker.rb:34:in `report_if_enabled?'
	from …/database_consistency-1.1.5/lib/database_consistency/processors/validators_fractions_processor.rb:23:in `block (3 levels) in check'
	from …/database_consistency-1.1.5/lib/database_consistency/processors/validators_fractions_processor.rb:21:in `map'

Hi @ojab,

Thank you for pointing this out!

Looking at your error: Missing column in books for title and the way you generated model: rails g model book, it seems that you don't have title column defined in the schema but you have a presence validation for it.

commented

Yep, it's completely bogus code, but we have this in one, ugh, proprietary project and got after trying to update the gem.
While we cannot do anything about it, crash is not good.

I see and agree with you, we have a rescue per each check to create a file with all failures, probably, this one happens sooner.

I'll have a look and fix it this week or do you want to contribute?

commented

Unfotunately don't think that I would have the capacity to read the code in the coming weeks, otherwise it would be PR already 😿

Just released 1.1.6, please try it out and say if it works fine for you.