Moving Program Response Into the Database
A few days ago I wrote about figuring out the architecture for Program Response before actually building it.
Today I started turning that architecture into something real.
Most of the work was in the persistence layer. Tables, constraints, activation rules, ownership, snapshots, and the things that need to stay true once real workout data starts getting stored.
This was also one of those days where fixing one problem immediately exposed another one.
Moving From Types to Actual Data
Up until this point, a lot of the work had been about defining what a training block, workout occurrence, exercise placement, set slot, and execution actually are.
Today those concepts started moving into PostgreSQL.
One thing I wanted to protect from the beginning was the original state of a training block.
Once a block is activated, I need to know what existed at that moment.
Not what the program looks like three weeks later after something has been edited.
So activation now creates a snapshot of the prescription that can be referenced later without depending on the current version of the program.
The database also has to maintain the relationships between all of these records without leaving behind weird partial states.
That meant working through foreign keys, uniqueness rules, cascading behavior, transaction boundaries, and ownership.
A lot of stuff nobody will ever see, but stuff I definitely do not want to figure out after people already have workout history stored.
I Added Limits Before Activation
I also added boundaries around what can become an active training block.
Right now that includes:
- 1 to 52 weeks
- 1 to 6 sessions per week
- 1 to 6 prescribed sets per exercise placement
- no more than 60 prescribed sets per week
- a maximum serialized snapshot size of 1 MiB
Most normal programs will never get close to some of those limits.
That is the point.
I would rather reject something obviously wrong when it enters the system than let it turn into a much stranger problem later.
The Migration Immediately Broke
Once I started testing the database migration, PostgreSQL stopped on this:
program_set_executions_load_mode_check
It said the constraint already existed.
My first thought was that I had somehow created it twice.
That was basically true, but not in the way I expected.
One check constraint was declared inline, so PostgreSQL generated a name for it automatically.
I had also explicitly named a different compatibility constraint.
Both ended up with the same name.
Nothing was wrong with either rule individually. They just could not exist under the same name.
I renamed the explicit one to:
program_set_executions_external_load_mode_check
Ran the migration again, and that problem was gone.
Small issue, but exactly the kind of thing I want to find while everything is still isolated.
Then My Test Failed Before Testing Anything
After getting past the migration issue, I started running transactional tests against the schema.
The first adversarial test failed almost immediately.
At first glance, that is not exactly what you want to see after spending all that time building the persistence layer.
Except the failure had nothing to do with Program Response.
The test created an auth.users record as part of its setup.
There is already a trigger in the database that creates the corresponding profile for a new user, and that trigger expects the user to have an email.
My fixture did not provide one.
So PostgreSQL rejected the setup and rolled the transaction back before a single Program Response assertion ran.
Basically, my test was broken.
Not the feature.
I fixed the fixture to create a valid auth user and reran it.
That distinction is something I have gotten much better about paying attention to.
A red test tells me something failed.
It does not automatically tell me what failed.
Keeping the Database Changes Isolated
None of this touched the hosted database.
The migration work and the tests stayed local and rollback-only while I worked through the failures.
That was intentional.
Database changes make me much more cautious than normal application code because bad assumptions can affect existing data instead of just breaking a screen.
I wanted to know the schema could build cleanly, the constraints behaved correctly, invalid operations were rejected, and the tests could tear everything back down before anything got near production.
Where It Ended Today
The focused Program Response work passed:
- 64 tests across 4 test files
- TypeScript type checking
- lint
The larger integrated codebase also passed:
- 926 out of 926 tests
- 91 out of 91 focused analyzer tests
- typecheck
- lint
- production build
There is still more to connect.
But the difference between where this was a few days ago and where it is now is pretty big.
A few days ago I had an architecture describing how the system should behave.
Now parts of that architecture actually exist in the database, have rules around what they are allowed to store, and have tests trying to prove those rules wrong.
That is a much better place to build the next part from.