From eb5188903cc101d0de706eb5d715e908acee0a5d Mon Sep 17 00:00:00 2001 From: Valentin Georgiev Date: Tue, 17 Nov 2026 10:40:41 +0100 Subject: [PATCH 1/3] HMS-2698: Skip init-db when development db is already filled 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. --- dds_web/commands.py | 6 ++++++ tests/test_commands.py | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/dds_web/commands.py b/dds_web/commands.py index cb724d8c0..f19ae25ef 100644 --- a/dds_web/commands.py +++ b/dds_web/commands.py @@ -64,6 +64,12 @@ def fill_db_wrapper(db_type): flask.current_app.logger.info("Initializing development db") assert flask.current_app.config["USE_LOCAL_DB"] + # The backend runs init-db on every start, so the db may already be filled, + # e.g. after the backend container restarts. Filling again would fail on duplicates. + if models.Unit.query.first() is not None: + flask.current_app.logger.info("Development db already contains data, skipping init-db.") + return + if db_type == "dev-small": from dds_web.development.db_init import fill_db diff --git a/tests/test_commands.py b/tests/test_commands.py index 29452a943..c6b080708 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -75,7 +75,8 @@ def test_fill_db_wrapper_devsmall(client, runner, capfd: LogCaptureFixture) -> N _: click.testing.Result = runner.invoke(fill_db_wrapper, ["dev-small"]) _, err = capfd.readouterr() assert "Initializing development db" in err - assert "DB filled" not in err # DB already filled, duplicates. + assert "Development db already contains data, skipping init-db." in err + assert "DB filled" not in err # def test_fill_db_wrapper_devbig(client, runner) -> None: From b870de5dd24be3a32bd13fdcd7f9f0c046447163 Mon Sep 17 00:00:00 2001 From: Valentin Georgiev Date: Tue, 17 Nov 2026 11:41:23 +0100 Subject: [PATCH 2/3] HMS-2698: Fill dev-small db in a single transaction 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. --- README.md | 9 +++++++++ dds_web/commands.py | 2 ++ dds_web/development/db_init.py | 5 ++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 80c0c724d..59673f097 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,15 @@ This builds and starts all containers needed for development: Add `-d` to run in detached mode, and stop the containers with `docker compose down`. +The backend fills the database with development data the first time it starts (`DDS_DB_TYPE`, e.g. `dev-small`). Later starts against the same database skip this, for example after a backend restart or after changing the backend's settings in `docker-compose.yml`. To start over with a fresh database: + +```bash +docker compose down -v +docker compose up +``` + +`-v` also removes the containers' volumes, which is where the database data lives. There is no need to remove images: the cached images are reused, so this takes seconds rather than a full rebuild. + If you are updating from an older setup (for example from before the switch from MinIO to SeaweedFS), remove the old containers before starting: ```bash diff --git a/dds_web/commands.py b/dds_web/commands.py index f19ae25ef..05ddb6e1f 100644 --- a/dds_web/commands.py +++ b/dds_web/commands.py @@ -66,6 +66,8 @@ def fill_db_wrapper(db_type): # The backend runs init-db on every start, so the db may already be filled, # e.g. after the backend container restarts. Filling again would fail on duplicates. + # Both fills commit everything in a single transaction at the end, so a unit only + # exists if the fill completed. if models.Unit.query.first() is not None: flask.current_app.logger.info("Development db already contains data, skipping init-db.") return diff --git a/dds_web/development/db_init.py b/dds_web/development/db_init.py index 2c5ed5ab6..024763e8b 100644 --- a/dds_web/development/db_init.py +++ b/dds_web/development/db_init.py @@ -180,7 +180,10 @@ def fill_db(): # Add unit to database - relationship will add the rest because of foreign key constraints db.session.add(unit_1) - db.session.commit() + # Flush, not commit: everything is committed in one transaction at the end. If the + # fill fails partway, nothing is saved, and init-db (which skips a db that has a unit) + # retries on the next start instead of treating a half-filled db as done. + db.session.flush() unituser_1_token = encrypted_jwt_token( username=unituser_1.username, From 89816ca45c13610515d9a82a6a1676beac06fcea Mon Sep 17 00:00:00 2001 From: Valentin Georgiev Date: Tue, 17 Nov 2026 11:42:44 +0100 Subject: [PATCH 3/3] sprintlog --- SPRINTLOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/SPRINTLOG.md b/SPRINTLOG.md index c8acdbee3..2c1614db2 100644 --- a/SPRINTLOG.md +++ b/SPRINTLOG.md @@ -711,3 +711,4 @@ _Nothing merged during this sprint_ - Bump dnspython from 2.9.0 to 2.9.2 ([#1893]https://github.com/ScilifelabDataCentre/dds_web/pull/1893) - Fix missing host in email links in local dev after Werkzeug 3.1.7+ bump ([#1894]https://github.com/ScilifelabDataCentre/dds_web/pull/1894) +- HMS-2698: Skip init-db when development db is already filled ([#1895]https://github.com/ScilifelabDataCentre/dds_web/pull/1895)