Skip to content

Act II — Full House#

Week 2. Word got out. Every room in Lisbon is booked, half the guests brought relatives, and two elephants claim they reserved the penthouse for Friday.

Week 2. Word got out. Every room in Lisbon is booked, half the guests brought relatives, and two elephants claim they reserved the penthouse for Friday.

Maya: Last week we had one guest and a brass suitcase. Tonight we have group bookings, three payment methods, and a housekeeping riot.

Ferris: Good news: PostgreSQL was built for full houses. Let's make the schema carry the weight.

Week 2, Monday to Sunday. Act I put guests and rooms on disk. Act II connects them: group bookings and line-item folios, PostgreSQL temporal keys that make double bookings impossible, polymorphic payments and maintenance tasks backed by real foreign keys, and a transactional work queue with zero external brokers.

This week's shifts#

Ch. Shift What you'll get Superpowers
5 Group Bookings & Folios Joins that write themselves, refuse to be ambiguous, and load parents plus children in 2 round trips. JOIN … USING JoinLintError link tables
6 No Double Bookings PostgreSQL makes double bookings impossible inside the primary key itself. TemporalPrimaryKey WITHOUT OVERLAPS Range[date]
7 Payments & Chores Polymorphic models backed by real foreign keys instead of magic strings. discriminator() single-table inheritance class-table inheritance
8 The 3 AM Task Board A transactional work queue with no broker: PostgreSQL does the locking and the messaging. savepoint() advisory_xact_lock() retry_transaction()

The Act II schema#

Six core tables join the building from examples/schema.py: Booking and BookingGuest for group stays, Charge for line-item bills, RoomAssignment and Rate with temporal constraints, Payment (single-table inheritance), and Task (class-table inheritance).

examples/schema.py · Booking, BookingGuest, Charge, RoomAssignment
class Booking(pg.Table):
    booking_id: Col[int] = pg.pk(default=pg.identity())
    dormitory_id: Col[int] = pg.fk(Dormitory, on_delete=pg.enums.OnDelete.CASCADE)
    primary_guest_id: Col[int] = pg.fk(Guest)
    status: Col[BookingStatus] = pg.fk(BookingStatusType, default=BookingStatus.CONFIRMED)
    channel: Col[BookingChannel] = pg.fk(BookingChannelType, default=BookingChannel.DIRECT)
    stay: Col[Range[date]]
    reference_code: Col[str] = pg.col(unique=True)
    created_at: Col[datetime] = pg.col(default=pg.now(), server_managed=True)


class BookingGuest(pg.Table):
    booking_guest_id: Col[int] = pg.pk(default=pg.identity())
    booking_id: Col[int] = pg.fk(Booking, on_delete=pg.enums.OnDelete.CASCADE)
    guest_id: Col[int] = pg.fk(Guest)

    @classmethod
    def __constraints__(cls) -> tuple[pg.TableConstraint, ...]:
        return (pg.Unique(cls.booking_id, cls.guest_id),)


class Charge(pg.Table):
    charge_id: Col[int] = pg.pk(default=pg.identity())
    booking_id: Col[int] = pg.fk(Booking, on_delete=pg.enums.OnDelete.CASCADE)
    kind: Col[ChargeKind] = pg.fk(ChargeKindType)
    description: Col[str]
    quantity: Col[pg.Int2] = pg.col(default=1)
    unit_price: Col[Money]
    amount: Col[Money] = pg.generated(mode=pg.enums.GeneratedMode.STORED)

    @classmethod
    def __generated__(cls) -> tuple[pg.Generated, ...]:
        return (pg.Generated(cls.amount, cls.quantity * cls.unit_price),)


class RoomAssignment(pg.Table):
    room_id: Col[int] = pg.fk(Room, on_delete=pg.enums.OnDelete.CASCADE)
    stay: Col[Range[date]]
    booking_id: Col[int] = pg.fk(Booking, on_delete=pg.enums.OnDelete.CASCADE)

    @classmethod
    def __constraints__(cls) -> tuple[pg.TableConstraint, ...]:
        return (pg.TemporalPrimaryKey(cls.room_id, cls.stay),)

Here is the full Act II data model rendered directly from those classes:

Entity-relationship diagram of Act 2, generated by pg.to_svg_erd()