Last active
October 21, 2023 22:54
-
-
Save umairabid/7fe9619d73e0a17558145b5d4fe6e9fe to your computer and use it in GitHub Desktop.
blog/traveling-time-with-postgres-range-columns/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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