Jonah Jonah - 1 year ago 81
Ruby Question

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...

mwp mwp
Answer Source

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, '&&']]

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download