[PR #28] Building the Server Foundation in Six Tasks
- What changed: Server code entered the repository for the first time. It was split into six tasks, and each task had its tests written before the code.
- Decision: One session built the six tasks in order, and the server and the app were kept in the same repository. Starting a new helper for each task would read the same context many times and multiply the cost.
- Still unknown: Whether the customer app and the admin web actually connect to this server will be checked in those tasks.
Until now the repository held only documents. PR #28 added the first server code. The foundation for the server that the customer app and admin web will connect to was built in six tasks: the skeleton and test base, the tables and history protection, phone number and password handling, login sessions, one-time codes, and terms re-consent.
What was done and why
- For each task, the tests were written first. Each test was seen failing before the code was written, and then seen passing.
- Each task became one commit, so it is easy to trace which decision produced which line later.
- Task 1 added an environment-variable check, one shape for every error response, and a one-line JSON log for each request. The health address (
/health, which says whether the server is alive) returns 503 when the database is down. - For login, a phone number written with
+82, hyphens or spaces counts as the same number. Passwords typed on iPhone and Android can store the same Korean letters in different byte orders (NFD and NFC), so these are treated as one password.
Options chosen and rejected
- How the work was run. A new helper could have been started for each task. But the tasks build on one another, so each helper would have to reread the same context, which multiplies the cost. One session implementing the six tasks in order was chosen (I chose this).
- Splitting the repository. Putting the server and the app in separate repositories was rejected. Keeping them together lets the same check tool verify the types they share, and the app framework officially supports this layout.
- Raising the refresh-key number by 2, not 1. If the number goes up by only 1 as the plan body says, an app that never received a response may send the old key again, and that old key then looks like the latest one, which causes a conflict. Going up by 2 avoids this. The cost is that an app which lost the response and retries with the same key will, rarely, be logged out. This cost was accepted.
- Renaming the module class. The table name stays the same, but the class is named
FarmModule, becauseModuleclashes with the framework’s@Moduledecorator. - The base time for dates. A date typed without a time is read as midnight in Korean time. The first version used midnight in another time zone. Running the real command exposed this, so a test was added first and then the code was fixed.
How it was checked
- The full check command (
npm run verify) passes, with 96 tests. The count includes the tests added after review. - Migrations and the default data command were run against a temporary database. Running the default data a second time skipped the rows already in place.
- A check that compared migration files by hand was replaced with a test. The check was confirmed to fail when a table definition was changed on purpose.
- A real bug caught by a test. A raw SQL
UPDATE … RETURNINGcame back from the ORM as[row, count]instead of a list of rows. As a result, running the same code twice at the same time made both calls look successful. A concurrency test failed and revealed it. The fix uses the number of changed rows from the query builder. The lesson is to check the shape of a result before trusting it.
Review from outside
- The author’s own review is weak, so the same PR was given to ChatGPT (Codex, model gpt-6-luna) for review. I supplied the model name myself. People carried the code back and forth between the two.
- The review raised three problems. Any difference in a response can reveal whether a phone number is registered. Issuing codes at the same time can leave two live codes. Failures that arrive at the same time can overwrite a long wait with a short one.
- This session checked each point against the code before accepting it. One point was fixed by making all responses the same, following my decision.
- The concurrency problems did not show up in normal runs, because the database handles requests in the order they arrive. The failure appeared only after a test was written that slowed down the save step of the first request.
Lessons
- If a failure response differs even slightly, the response shape reveals registration. Every failure response has to look the same.
- Concurrency bugs need a test that forces the order. Sending several requests at once is not enough.
- The author’s review alone is not enough. A second look from someone else gives more value for the cost.