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/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) diff --git a/dds_web/commands.py b/dds_web/commands.py index cb724d8c0..05ddb6e1f 100644 --- a/dds_web/commands.py +++ b/dds_web/commands.py @@ -64,6 +64,14 @@ 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. + # 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 + if db_type == "dev-small": from dds_web.development.db_init import fill_db 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, 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: