Blog
EngineeringFor engineers

Hostels by id, not by name

A hostel's name was its key everywhere, so renaming one left references behind and checks compared ids with names. How we're moving to ids, why a rename now runs references first and the hostel last, and why closing a hostel is a switch, not a delete.

Lodge Internet · 14 November 2026 · 6 min read

An admin fixes a typo in a hostel's name. One field, one save. On the hostel's own record, the new name is perfect.

Everywhere else, nothing changed. The hostel's plans still named the old spelling. So did its codes, its customers, its purchases, its TV subscriptions, its open checkouts, its partner splits and its feedback. To every one of them, the hostel they belonged to no longer existed.

That's what happens when a name is a key.

How names became keys

Lodge started on Firestore, a document database. Documents don't join, so the easy way to say "this plan belongs to that hostel" was to write the hostel's name on the plan. Then on the code, the customer, the purchase, and so on. It's readable, and it works until the name changes.

The docstring of backend/app/services/hostel_rename.py is blunt about it: until hostels are referenced by id, every document that belongs to a hostel holds its name, and a rename that changes only the hostel document leaves all of them pointing at a name nothing has any more. Two different rename paths in the admin console got this wrong in two different ways. One renamed the hostel alone. The other remembered some references and missed the rest.

The second symptom: ids meeting names

Names as keys caused a quieter bug in the other direction.

Some admins are restricted to certain hostels. Their list of allowed hostels is saved by the admin screens as hostel document ids, and other parts of the console, partner splits among them, already read it that way. But plans, codes, users and TV subscriptions name their hostel. So a few permission checks compared a hostel's name straight against a list of ids.

A name never equals an id. Every restricted admin was refused on those screens, for every hostel, including their own.

backend/app/services/hostel_access.py fixes it in one function. allowed_hostel_names turns the admin's ids into names first, then checks against those:

allowed = {str(entry) for entry in ctx.hostels}
for doc in hostels_repo.get_many([str(entry) for entry in ctx.hostels]):
    name = doc.to_dict().get("name")
    if name:
        allowed.add(name)
return allowed

Note the first line. Each entry is kept as it is as well as being looked up, so an older list entry that is itself a name still counts. The fix was written so that nothing allowed before is refused now.

The real fix: an id beside every name

When we moved to Postgres, migration 0003 (dated 1 October 2026) gave every table that names a hostel a real reference to it. Eight tables gained a hostel id column: codes, feedback, plans, purchases, checkouts, splits, TV subscriptions and users. Each one is a foreign key to the hostels table.

Two decisions in that migration matter.

The name stays. The id says which hostel. The name beside it records what the hostel was called when the row was written. A purchase from before a rename still says what the receipt said.

A missing hostel is NULL, not an error. Old data has rows naming hostels that were deleted long ago. The column is filled where the name resolves to exactly one hostel, and left empty otherwise. Deleting a hostel sets its references to NULL rather than failing. Refusing those rows would have meant refusing writes the application already makes.

The money ledger carries both too. Each posting records the hostel's id where it resolves and the name exactly as the sale recorded it, so the books can always be read by hostel.

Renaming safely, until names are gone

Ids don't remove the need for a rename yet, because most lookups in the code still match on the name. So hostel_rename.rename updates every place the name is stored. The list of fields comes from a census of what the production data actually contained, not from what we assumed it contained.

It deliberately leaves two things alone: the waitlist's hostel name, which is free text people typed, and the analytics snapshot, which is rebuilt anyway. Admin lists and partner splits hold ids, so a rename doesn't need to touch them.

The interesting part is the order. References are renamed first, and the hostel itself last:

for collection, field, stamp in NAME_FIELDS:
    found = store.query(collection, where=[(field, "==", old_name)])
    ...
    store.update_many(collection, [(doc.id, fields) for doc in found])
...
hostels_repo.update(hostel_id, {"name": new_name, "updatedAt": NOW})

If the rename fails halfway, the hostel still has its old name, so running it again is just running it again. Whatever was already renamed no longer matches the old name and is skipped. Whatever wasn't is picked up. There's no half-renamed state that needs a person to untangle it.

Closing a hostel is a switch

When an installation is removed, the hostel has to stop trading. The tempting move is to delete it. backend/app/services/hostel_status.py describes the alternative: a hard stop, not a quiet delisting, and a soft delete, not a removal.

The flag is the hostel's active field, and absent means active, so no existing hostel needed a backfill. Once it's switched off:

  • nothing more can be sold there: assert_can_trade is called on every purchase and reservation path;
  • the plans list for that hostel comes back empty, so customers aren't sent to a checkout that will refuse them;
  • TV devices come off the router's allow list straight away.

The customer's message is plain on purpose: internet service is no longer available at this hostel. The comment explains why. A customer told "temporarily unavailable" will keep retrying. A customer told the service has been withdrawn won't.

Closing writes the flag first, before touching any TV device. If a router is unreachable, sales have still stopped, and the devices it couldn't reach are listed so the step can be run again. A hostel is never left half-closed and still selling.

Switching a hostel back on restores TV access for subscriptions still within their dates. One whose dates passed while the hostel was closed is left to the expiry job, which owns that decision. Re-granting it here would hand back access nobody is paying for.

Why closed hostels don't deny TV devices

Our TV access has two lists on the router: allow and deny. When a hostel closes, its TV devices come off the allow list, but they are deliberately not added to the deny list.

Deny means something specific to us: this customer's subscription lapsed, and someone should look before they buy again. A building closing is our decision, not the customer's. Denying their device would blame them for it, and would block them from buying at whichever hostel they move to next.

The trade-offs

Keeping the name beside the id means two columns that can disagree. We accept that, because the name is history and the id is the reference, and they answer different questions.

A soft delete keeps every record of a closed hostel in the database. That's what lets reopening restore service, and what keeps its past sales in partner statements.

What's still open

The id exists everywhere, but the name is still the join key in most of the code, and the id is derived from the name each time a row is written. The rename service is the bridge until lookups move onto ids. When they have, a rename becomes what it should have been from the start: one field on one record.

Last checked against the product on 14 November 2026.

More in Engineering