Environments that use sample data fail to start if sample user has been deleted
Description
We recently had an incident where training would not boot because some of the sample users had been deleted. During the data initialization step during startup, the app was running into errors because it was trying to create those sample users again even though they already existed in the DB.
Steps to reproduce
- Using the UI, delete a sample user in an environment that has the
create-sample-dataspring profile. Probably not one of the customer-facing environments. You can find the list of sample users in the yaml for the sample data profile. https://github.com/CDCgov/prime-simplereport/blob/9aa08e8c3d60790f738913a5ad7be4ec6a73bb61/backend/src/main/resources/application-create-sample-data.yaml#L208 - Start the app in that env. The app will fail to start and log error
ERROR: duplicate key value violates unique constraint "api_user_login_email_key"from theOrganizatinIntializingService.
Expected behavior
2 options, to be picked in refinement:
- App should boot properly even if sample users are deleted. Optionally, have the org initializing code undelete a sample user if they have been deleted.
- Prevent sample users from being deleted in environments that generate sample data.
Additional context
It's suspected but not confirmed that during the org initializing step, the code that checks if the sample user exists before trying to create them is using a query that excludes results where is_deleted = t. This query should probably be updated to include deleted users.
Slack thread of debugging original incident
May impact or be relevant to: https://github.com/CDCgov/prime-simplereport/issues/7492
Added context: this impacts training env but not prod. Costs eng time to fix it each time.
Added context: this impacts
trainingenv but notprod. Costs eng time to fix it each time.
I think this will also impact demo env
Prioritizing this because sample data being deleted becomes significantly more common with the latest data retention work.
Suggesting we update the sample data seeding process to look for deleted users and just set is_deleted and pii_deleted false
PII delete doesn't affect API Users, which is the cause of the bug. Patients/the Person table doesn't have a unique constraint that would affect the sample data. BUT - the next time we see this bug, we should fix it by just addressing this ticket rather than updating the db directly.