Skip to content

Instantly share code, notes, and snippets.

@umairabid
Last active October 21, 2023 22:54
Show Gist options
  • Save umairabid/7fe9619d73e0a17558145b5d4fe6e9fe to your computer and use it in GitHub Desktop.
Save umairabid/7fe9619d73e0a17558145b5d4fe6e9fe to your computer and use it in GitHub Desktop.
blog/traveling-time-with-postgres-range-columns/
class AddStateTaxes < ActiveRecord::Migration[7.0]
def change
create_table :state_taxes do |t|
t.integer :state_id, null: false
t.string :tax_type, null: false
t.decimal :rate, null: false
t.daterange :effective_range, null: false
t.tsrange :system_range, null: false
end
reversible do |direction|
direction.up do
execute <<-SQL
CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE state_taxes
ADD CONSTRAINT prevent_overlapping_state_taxes EXCLUDE USING gist
(
state_id WITH =,
tax_type WITH =,
effective_range WITH &&,
system_range WITH &&
);
SQL
end
direction.down do
execute <<-SQL
ALTER TABLE state_taxes
DROP CONSTRAINT IF EXISTS prevent_overlapping_state_taxes;
SQL
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment