Skip to content

Add a context manager to enable the locking of DB mappings#618

Merged
soininen merged 1 commit intomasterfrom
db_lock
Mar 20, 2026
Merged

Add a context manager to enable the locking of DB mappings#618
soininen merged 1 commit intomasterfrom
db_lock

Conversation

@soininen
Copy link
Copy Markdown
Contributor

@soininen soininen commented Mar 19, 2026

This PR add lock_db context manager to spine_db_client that can be used in scripts that are executed as part of Spine Toolbox workflows. It locks a database mapping using DB server allowing the script to safely commit data even when executed in parallel.

No associated issue.

Checklist before merging

  • Documentation (also in Toolbox repo) is up-to-date
  • Release notes have been updated
  • Unit tests have been added/updated accordingly
  • Code has been formatted by black & isort
  • Unit tests pass

In Python scripts, you can use DatabaseMapping instances directly
to access a Spine DB. Running such scripts that make commits
to the same DB in parallel will quickly run into issues where
some database mappings get out-of-sync from the DB or
multiple scripts try to commit same data.

This commit implements a way to lock the database accross
multiple processes when we have DB server running. The lock can be
acquired by a new context manager that asks the server for a lock
and blocks if the DB has been already locked.
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 97.14286% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.15%. Comparing base (deb9159) to head (0d7426e).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
spinedb_api/db_mapping_base.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #618      +/-   ##
==========================================
+ Coverage   86.07%   86.15%   +0.07%     
==========================================
  Files          83       83              
  Lines       11447    11475      +28     
  Branches     1635     1636       +1     
==========================================
+ Hits         9853     9886      +33     
+ Misses       1241     1238       -3     
+ Partials      353      351       -2     

☔ View full report in Codecov by Sentry.
📢 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.

@soininen soininen merged commit 82d214b into master Mar 20, 2026
17 checks passed
@soininen soininen deleted the db_lock branch March 20, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant