Files
redefined-designs/backend/migrations/1787400000000_split-customer-name.js
T
bermudalambandClaude Opus 5 b287c07747
Tests / lint (pull_request) Successful in 1m38s
Tests / backend-unit (pull_request) Successful in 1m44s
Tests / frontend-e2e (pull_request) Failing after 9m50s
SonarQube Analysis / sonarqube (pull_request) Failing after 11m46s
feat: capture first and last name so emails can greet informally (#106)
Registration collected one optional Name, so every greeting had only a whole name to use: "Hi Thom Lamb," rather than "Hi Thom,". Both parts are now captured, and the cart reminder greets by first name.

Both are required of anyone new, refused individually rather than as a single "name is required" so a form that filled one and not the other is told which.

The columns are nullable even so, and that is deliberate. Marking them NOT NULL would mean backfilling legacy rows with empty strings, which asserts that every customer has a name — untrue of anyone who registered while the field was optional. The table records what is actually the case; the rule that new registrations must supply both lives in the route, where a missing field can produce a message naming it.

The backfill splits on the first space, and it is lossy in a way no version of this avoids. "Thom Lamb" becomes Thom and Lamb; "Mary Jane Smith" gets a last name of "Jane Smith"; names that are not two parts fare worse. It was chosen over leaving the columns empty because nothing currently lets a customer correct their own name — PUT /api/customers/me exists but no frontend calls it — so empty would have meant permanently unpersonalised for every existing customer. The migration says so, so nobody later reads backfilled values as data the customer supplied in that shape.

Verified against a seeded database rather than reasoned about, because this is the part that cannot be covered by the suite: migrations run in globalSetup before any test, and the old column is gone afterwards. Six representative rows through the real migration gave Thom/Lamb, Mary/"Jane Smith", Cher/null, "  Padded  Name  " trimmed to Padded/Name, and null and whitespace-only names left as null on both. The down migration rejoins the parts and returns all six to their original strings.

The old column is dropped rather than kept alongside, so there is one source of truth instead of two that drift.

The admin keeps receiving a single composed display name. It only ever shows one — the list cell and the drawer title — and never edits one, so giving it both parts plus the joining logic would be work for no reader.

Churn was the bulk of this: 14 backend registrations and 10 end-to-end registration forms. A first attempt at the backend fixtures also added names to login and password-reset payloads, which would still have passed since the server ignores unknown fields, but a login test implying login takes a name is a small lie; that was reverted and redone against register calls only.

Verified: 172 unit, 183 integration and 95 end-to-end passing, lint unchanged at 4 backend and 27 frontend warnings.

Not covered: the cart reminder itself, which runs from a cron and had no test before this either. The greeting change is a one-line substitution in that query.

Refs #106
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 18:23:13 -05:00

63 lines
2.8 KiB
JavaScript

exports.up = (pgm) => {
pgm.sql(`
-- Emails greet customers, and a single 'name' column only allows the formal
-- whole name: "Hi Thom Lamb," rather than "Hi Thom,". Splitting it is what
-- makes an informal greeting possible.
--
-- Both columns are nullable even though registration now requires them. The
-- requirement is enforced in the route, where a missing field can produce a
-- 400 naming it. Marking these NOT NULL would mean backfilling legacy rows
-- with empty strings, which asserts that every customer has a name — and
-- that is not true of anyone who registered while the field was optional.
-- The table should record what is actually the case.
ALTER TABLE customers ADD COLUMN IF NOT EXISTS first_name TEXT;
ALTER TABLE customers ADD COLUMN IF NOT EXISTS last_name TEXT;
-- The lossy part, and there is no version of this that is not.
--
-- Splitting on the first space is right for "Thom Lamb" and wrong for
-- "Mary Jane Smith", who ends up with a last name of "Jane Smith". Names do
-- not reliably divide into two parts at all. This was chosen over leaving
-- the columns empty because there is currently no way for a customer to
-- correct their own name — PUT /api/customers/me exists but nothing calls
-- it — so empty would mean permanently unpersonalised for everyone who
-- registered before this.
--
-- Treat backfilled values as a best guess rather than as data the customer
-- gave you in this shape.
UPDATE customers
SET first_name = CASE
WHEN position(' ' in btrim(name)) > 0 THEN split_part(btrim(name), ' ', 1)
ELSE btrim(name)
END,
last_name = CASE
WHEN position(' ' in btrim(name)) > 0
THEN btrim(substring(btrim(name) from position(' ' in btrim(name)) + 1))
ELSE NULL
END
WHERE name IS NOT NULL AND btrim(name) <> '';
-- Dropped rather than kept alongside. Two columns describing the same fact
-- drift, and the new pair is now the only place a name lives.
ALTER TABLE customers DROP COLUMN IF EXISTS name;
`);
};
exports.down = (pgm) => {
pgm.sql(`
ALTER TABLE customers ADD COLUMN IF NOT EXISTS name TEXT;
-- Rejoins the parts. Not a perfect inverse of the split above — a name that
-- was mangled on the way in stays mangled on the way out — but it restores
-- a usable whole name rather than leaving the column empty.
UPDATE customers
SET name = btrim(concat_ws(' ', first_name, last_name))
WHERE first_name IS NOT NULL OR last_name IS NOT NULL;
UPDATE customers SET name = NULL WHERE name = '';
ALTER TABLE customers DROP COLUMN IF EXISTS first_name;
ALTER TABLE customers DROP COLUMN IF EXISTS last_name;
`);
};