000007_add_booking_exclusion_constraint.up.sql 441 B

123456789
  1. -- Create extension for gist index if not exists
  2. CREATE EXTENSION IF NOT EXISTS btree_gist;
  3. -- Add exclusion constraint to prevent overlapping bookings for the same place
  4. -- Only applies to non-cancelled bookings
  5. -- tstzrange because columns are timestamp with time zone
  6. ALTER TABLE bookings ADD CONSTRAINT no_overlapping_bookings
  7. EXCLUDE USING gist (place_id WITH =, tstzrange(start_time, end_time) WITH &&)
  8. WHERE (status != 'cancelled');