Use support for check constraints builtin to Rails 6.1
Rails 6.1 added some support for check constraints in migrations: https://github.com/rails/rails/pull/31323
This currently can't replace all our use cases, since it doesn't seem to support adding the constraint and validating as a separate operation. However we can look into removing create_table_with_constraints
, or at least turn it into a thin compatibility layer with the Rails version.
An example:
[17] pry(main)> ApplicationRecord.connection.create_table :foo do |t|
[17] pry(main)* t.text :bar, null: false
[17] pry(main)* t.check_constraint 'char_length(bar) < 100', name: :baz_constraint
[17] pry(main)* end
(4.8ms) CREATE TABLE "foo" ("id" bigserial primary key, "bar" text NOT NULL, CONSTRAINT baz_constraint CHECK (char_length(bar) < 100))