Repository navigation
HMS-2698: Skip init-db when development db is already filled - #1895
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 60d86cb827
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
The backend runs `flask init-db $DB_TYPE` on every start. When the backend container restarts or is recreated while the database container is kept, dev-small/dev-big tried to insert the development data again and crashed with "Duplicate entry 'someunit' for key 'internal_ref'", putting the backend in a restart loop. Check for an existing Unit and skip filling if one is found, the way the production option already checks for an existing super admin. Update the dev-small test to expect the skip.
bf92151 to
eb51889
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #1895 +/- ##
==========================================
- Coverage 92.94% 92.88% -0.06%
==========================================
Files 30 30
Lines 5074 5077 +3
==========================================
Hits 4716 4716
- Misses 358 361 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
fill_db() committed after adding unit_1 and only then created and committed the research users' project keys. If the fill failed in between, the db had a unit, so init-db treated it as filled on the next start: research users could not access the seeded project, and the original error was hidden. Flush instead of committing after unit_1, so the whole fill is committed at once and a failed fill leaves nothing behind. dev-big (factories.create_all) already commits once at the end. Document fresh local database and init-db skip in README.
SevLG
left a comment
There was a problem hiding this comment.
Looks good! Changes make sense, comment from codex was resolved. I tested setting up fresh with docker compose and restarting backend:
Changing an env variable for the backend:
And running pytest tests/test_commands.py -k fill_db_wrapper:
All works. Many warnings in the test but I think they're not introduced in this PR.
The backend runs
flask init-db $DB_TYPEon every start. When the backend container restarts or is recreated while the database container is kept, dev-small/dev-big tried to insert the development data again and crashed with "Duplicate entry 'someunit' for key 'internal_ref'", putting the backend in a restart loop.Check for an existing Unit and skip filling if one is found, the way the production option already checks for an existing super admin. Update the dev-small test to expect the skip.
Pull Request Template
Before Marking as Ready for Review
dev(or other targeted branch)SPRINTLOG.mdif neededIf the target branch is
master:Summary
The backend container runs
flask db upgrade && flask init-db $DB_TYPE && flask runon every start. Withdev-small/dev-big,init-dbinserted the development data without checking whether it was already there. Wheneverdds_backendrestarted or was recreated whiledds_databasewas kept, it crashed with:and, because of
restart: on-failure, looped on the same error. This happens, for example, after a backend container restart or after changing the backend's env indocker-compose.yml(which recreates only the backend). The workaround was to remove all containers and images and rebuild from scratch.This PR makes
init-dbcheck for an existingUnitwith the development options. If one is found, it logsDevelopment db already contains data, skipping init-db.and returns. Theproductionoption already checks for an existing super admin, and this follows the same pattern.dds_web/commands.py: skip filling when the development db already has datatests/test_commands.py:test_fill_db_wrapper_devsmallnow expects the skip message instead of documenting the duplicate failureTo get a fresh database locally, use
docker compose down -v && docker compose up.Related Issue/Ticket
HMS-2698
Testing
pytest tests/test_commands.py -k fill_db_wrapperdocker compose down -v && docker compose up: database is filled as before ("DB filled" in the logs)docker compose restart backend: logs show "skipping init-db" and the backend starts without errorsdocker-compose.ymland randocker compose up: the recreated backend skipsinit-dband startsReviewer Notes
dev-small/dev-bigoptions are affected.productionis unchanged.Unitexists". Bothfill_db()and thedev-bigfactories create units, so a filled db always has at least one.Once all boxes are checked, mark the PR as Ready for Review and tag at least one team member as the initial reviewer.