Files
conference-room-booking-system/backend/migrations/versions/0002_add_booking_exclusion_constraint.py
T
2025-09-06 13:08:06 -04:00

36 lines
886 B
Python

"""Add exclusion constraint to prevent overlapping bookings for the same room
Revision ID: 0002_add_booking_exclusion_constraint
Revises: 0001_initial_migration
Create Date: 2025-08-26
"""
from alembic import op
# revision identifiers, used by Alembic.
revision = "0002"
down_revision = "0001"
branch_labels = None
depends_on = None
def upgrade():
# Ensure btree_gist extension is enabled
op.execute("CREATE EXTENSION IF NOT EXISTS btree_gist;")
# Add exclusion constraint to prevent overlapping bookings for the same room
op.execute(
"""
ALTER TABLE bookings
ADD CONSTRAINT bookings_no_overlap
EXCLUDE USING gist (
room_id WITH =,
tstzrange(start_time, end_time, '[)') WITH &&
);
"""
)
def downgrade():
op.execute("ALTER TABLE bookings DROP CONSTRAINT IF EXISTS bookings_no_overlap;")