Search code examples
rubysequel

How to write a migration for a postgres range type in sequel?


Is it possible to define postgres range types in a sequel migration (eg, tsrange)? I found the pg_range extension in the docs, but couldn't find an example migration...


Solution

  • Sequel makes the Postgres datatypes available as keywords in migrations. Here's one I just did for a bulk discount schedule table, with a constraint to disallow overlapping ranges for the same product group :

    create_table(:product_group_bulk_discounts) do
      primary_key :id
      foreign_key :product_group_id, :product_groups, :on_delete=>:cascade
      int4range :quantity
      money :unit_cost_delta, :default=>0, :null=>true
      exclude [[:product_group_id, '='], [:quantity, '&&']]
    end
    

    N.B.: This type of exclude constraint requires the btree_gist extension to be loaded on the database.