Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions SPRINTLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions dds_web/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
valyo marked this conversation as resolved.
Resolved

if db_type == "dev-small":
from dds_web.development.db_init import fill_db

Expand Down
5 changes: 4 additions & 1 deletion dds_web/development/db_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading