djezzzl / database_consistency

The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models.
MIT License
1.02k stars 43 forks source link

[Feature] Check constraint checker #181

Open chaadow opened 1 year ago

chaadow commented 1 year ago

Hi, First, thanks for this wonderful gem.

I was wondering if adding a checker between "validates_numericality_of" and a CHECK constraint can be easily implemented.

I can try to look into it by looking into how the presence validator is implemented if you consider this valuable.

djezzzl commented 1 year ago

Hi @chaadow,

Sorry that it took so long for me to answer you!

First, thank you for your kind words; I'm glad the gem is valuable.

Could you please say if the CHECK constraint is somewhat standard? I just think what would be the way to compare what is defined in CHECK with what should be there.

The most effortless check would ensure some CHECK constraint on the field with validates_numericality_of validation.

Would you find this helpful?

chaadow commented 1 year ago

Yes I believe it's quite standard

Yes i'm thinking of 2-way checkers. One that would autocorrect by adding a migration like this :

class CreateBooks < ActiveRecord::Migration
  def change
    add_check_constraint :books, "price > 100", name: "price_check"
  end
end

and the other way around, where a check constraint exists on a column ( for numericality) but no validates_numericality_of in the model.

Another use case for a check constraint is for a NOT NULL :

image

Since it is functionally equivalent to the column constraint NOT NULL, it can avoid false positives for the validates_presence_of checker.