vprokopchuk256 / mv-postgresql

Postgresql constraints in migrations similiar to ActiveRecord validations
MIT License
20 stars 2 forks source link

schema.rb is breaking when custom statement include single quotes. #8

Closed campreb closed 7 years ago

campreb commented 8 years ago

We have encountered a strange problem when our custom statement includes single quotes, the quotes won't be properly serialized in the schema.rb.

For instance if we define in our migration:

validates :foobar, :relation_id, custom: { statement: "(relation_id IS NOT NULL AND kind IN ('foo', 'bar'))", allow_nil: true }

Will output this in schema.rb once it is migrated:

validates :foobar, :relation_id, custom: { statement: '(relation_id IS NOT NULL AND kind IN ('foo', 'bar'))', allow_nil: true }
vprokopchuk256 commented 8 years ago

@campreb - got it. I will take care of it next two days.

vprokopchuk256 commented 8 years ago

@campreb - it should be fixed in v2.2.7. Check it, pls.

FYI: In your case I would recommend not to use custom validation but rather use present and inclusion validations. For ex:

validates :foobar, :kind, inclusion: { in: ['foo', 'bar'] }
validates :foobar, :relation_id, presence: { allow_blank: true }

or if you decide to use custom validation anyway then reference column names properly ( in curly brackets ):

validates :foobar, :relation_id, custom: { statement: "({relation_id} IS NOT NULL AND {kind} IN ('foo', 'bar'))", allow_nil: true }