Skip to content

HMS-2698: Skip init-db when development db is already filled - #1895

Merged
valyo merged 3 commits into
devfrom
HMS-2698-init-db-skip-filled-db
Nov 25, 2026
Merged

valyo merged 3 commits into
devfrom
HMS-2698-init-db-skip-filled-db

Conversation

@valyo

@valyo valyo commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

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.

Pull Request Template

Before Marking as Ready for Review

  • Add relevant information to the sections below (Summary etc)
  • Rebase or merge the latest dev (or other targeted branch)
  • Update documentation if needed
  • Add an entry to the SPRINTLOG.md if needed
  • Choose an appropriate label. See here for information on the labelling options
  • The code follows the style guidelines
  • Perform a self-review: read the diff as if reviewing someone else's code
  • I have commented the code, particularly in hard-to-understand areas
  • Verify that all checks and tests have passed

If the target branch is master:

Summary

The backend container runs flask db upgrade && flask init-db $DB_TYPE && flask run on every start. With dev-small/dev-big, init-db inserted the development data without checking whether it was already there. Whenever dds_backend restarted or was recreated while dds_database was kept, it crashed with:

sqlalchemy.exc.IntegrityError: (1062, "Duplicate entry 'someunit' for key 'internal_ref'")

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 in docker-compose.yml (which recreates only the backend). The workaround was to remove all containers and images and rebuild from scratch.

This PR makes init-db check for an existing Unit with the development options. If one is found, it logs Development db already contains data, skipping init-db. and returns. The production option 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 data
  • tests/test_commands.py: test_fill_db_wrapper_devsmall now expects the skip message instead of documenting the duplicate failure

To 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_wrapper
  • docker 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 errors
  • Changed an env var for the backend in docker-compose.yml and ran docker compose up: the recreated backend skips init-db and starts

Reviewer Notes

  • Only the dev-small/dev-big options are affected. production is unchanged.
  • The check is "any Unit exists". Both fill_db() and the dev-big factories 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.

@valyo
valyo requested a review from a team as a code owner November 17, 2026 09:44
@valyo valyo added the skip-changelog For release template label Nov 17, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread dds_web/commands.py Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T09:46:16.859677Z 60d86cb PR opened
ℹ️ 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" or "@codex security review".

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.
@valyo
valyo force-pushed the HMS-2698-init-db-skip-filled-db branch from bf92151 to eb51889 Compare November 17, 2026 09:52
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.88%. Comparing base (da7ac68) to head (89816ca).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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 SevLG left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Changes make sense, comment from codex was resolved. I tested setting up fresh with docker compose and restarting backend:

Screenshot 2026-11-24 at 09 02 27

Changing an env variable for the backend:

Screenshot 2026-11-24 at 09 07 25

And running pytest tests/test_commands.py -k fill_db_wrapper:

Screenshot 2026-11-24 at 12 18 41

All works. Many warnings in the test but I think they're not introduced in this PR.

@valyo
valyo merged commit cc15483 into dev Nov 25, 2026
15 checks passed
@valyo
valyo deleted the HMS-2698-init-db-skip-filled-db branch November 25, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changelog For release template

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants