[FIX] Make BIDSLayout.save idempotent when re-saving to the same path#1260
Open
Leonard013 wants to merge 1 commit into
Open
[FIX] Make BIDSLayout.save idempotent when re-saving to the same path#1260Leonard013 wants to merge 1 commit into
Leonard013 wants to merge 1 commit into
Conversation
Calling ``layout.save(path)`` twice against the same path raised ``sqlite3.OperationalError: table associations already exists``. ``ConnectionManager.save_database`` snapshots the index by replaying ``sqlite3.iterdump()`` into ``sqlite3.connect(database_file)``. ``iterdump`` emits plain ``CREATE TABLE`` statements, so replaying them into a file that already holds the schema (a previous save, e.g. re-running a script) fails. The destination cannot simply be cleared in place: with the default ``replace_connection=True`` the first save re-points the layout at the saved file, so a subsequent save has ``old_db`` (source) and ``database_file`` (destination) aliasing the same path. Dump into a fresh temporary file and atomically ``replace`` it into position instead, which starts from an empty database and stays correct even when source and destination are the same file. Adds a regression test that saves a layout to the same path twice and checks the re-saved database is still complete. Closes bids-standard#1079. This change was developed with the assistance of Claude, an AI coding assistant; all changes were authored, reviewed, and verified by a human contributor. Co-authored-by: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1079.
Why
Calling
layout.save(path)a second time against the same path raises:This bites users who re-run a script: the first run creates
<path>/layout_index.sqlite, and a secondsave()to the same path fails.@effigies reproduced it in the issue thread and noted it wasn't obvious to fix.
Root cause
ConnectionManager.save_database(src/bids/layout/db.py) snapshots the index byreplaying
sqlite3.Connection.iterdump()intosqlite3.connect(database_file).iterdump()emits plainCREATE TABLE ...(noIF NOT EXISTS, noDROP), soreplaying into a
database_filethat already holds the schema (from a priorsave) collides on the first table —
associations.The reason a naive fix (deleting/truncating the destination first) does not
work is subtle, and is likely why this looked unfixable: with the default
replace_connection=True, the firstsave()re-points the layout's connection atthe saved file. A subsequent
save()to the same path therefore hasold_db(source) and
database_file(destination) aliasing the same file — clearing thedestination in place would destroy the source before
iterdump()reads it(confirmed: it produces an empty /
no such table: layout_infodatabase).What
Dump into a fresh temporary file in the same directory, then atomically move it
into place:
CREATE TABLEnever collides.replace(), so re-saving stayscorrect even when source and destination are the same file.
iterdump()is preserved;replace()is a same-directory atomicrename.
Tests
Added
test_save_database_twice_same_pathtosrc/bids/layout/tests/test_db.py:saves a layout to the same path twice (default
replace_connection=True,exercising the same-file path) and asserts the re-saved database is complete and
reloadable.
OperationalError: table associations already exists.pytest src/bids/layout/tests/→ 193 passed, 71 skipped (network/submodulegated), 1 xfailed, 0 failed.
Caveats (flagging honestly)
old_dbstill holds the destination open whenreplace()runs, which can raisePermissionErroron Windows. This is not a regression (the current codealready raises on the second save everywhere), and pybids CI is Linux/macOS only;
first saves and cross-path saves are unaffected. Happy to guard it (close
old_dbbeforereplace()) if you'd like it fully Windows-safe.(
tempfile.mkstemp(dir=...)), atry/finallyto clean the temp on error, and/ora stronger completeness assertion in the test.
No
CHANGELOG.rstedit — this project compiles the changelog from PR titles viarelease-drafter.
Prepared with AI assistance (Claude Code): the reproduction, root-cause analysis,
fix, and test were AI-drafted; the reasoning and verification are documented above.