Code RoomPhantom read allows double booking
HardPrep Room Coding #1837

Phantom read allows double booking

Code reviewDatabases & SQLSenior–Staff~30 min

Review this Python booking function meant to prevent double-booking a room.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 30 min
Mark a line and say what kind of problem it is.0 findings
1def book_room(conn, room_id, start, end):
2 conn.set_isolation_level(REPEATABLE_READ)
3 cur = conn.cursor()
4 cur.execute(
5 "SELECT count(*) FROM bookings "
6 "WHERE room_id = %s AND tsrange(start, stop) && tsrange(%s, %s)",
7 (room_id, start, end))
8 if cur.fetchone()[0] == 0:
9 cur.execute(
10 "INSERT INTO bookings (room_id, start, stop) VALUES (%s,%s,%s)",
11 (room_id, start, end))
12 conn.commit()
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.