Invental/Review lessons/Check, then act on stale state

Check, then act on stale state

Make a column optional without breakage

Make a column optional only after no running code reads it; until then give it a default. And make “fill if empty” writes conditional in the database, not in the browser.

01 · Before and afterThe pattern

TypeScript
// before: client saw null, so it writes unconditionally
await db.account.update({ where: { id }, data: { timezone } });
// after: the database decides whether it's still empty
const { count } = await db.account.updateMany({ where: { id, timezone: null }, data: { timezone } });
if (count) track("timezone_auto_set");
SQL
-- deprecating a column: keep it non-null for old readers
ALTER TABLE "Schedule" ALTER COLUMN "timezone" SET DEFAULT 'UTC';  -- not DROP NOT NULL

02 · Run it on your appThe check

When you stop writing a column, will an older app instance still running during the deploy get null from it?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.