Repository navigation
Fix staged_insert1 object paths and store names to match insert1 - #1741
godara-alt wants to merge 2 commits into
Conversation
dimitri-yatsenko
left a comment
There was a problem hiding this comment.
Thank you for the fix and for the thorough tests.
The change is correct. I checked out the branch and ran it with a regular table and a part table on MySQL and PostgreSQL. Rows written with staged_insert1 now record the same path layout as insert1 (recording/... and session__item/...). They also record the resolved store name (local) instead of "". They still read, and garbage collection still counts them, after stores.default is pointed at another store. I also reverted the change in staged_insert.py, and all five new tests failed. Each half of the fix, table name and store name, has its own failing test. The integration tests here use schema_obj, which runs on MySQL only, so I ran the same checks on PostgreSQL locally. Nothing is needed there.
Two requests before we merge:
Add a part-table case to the integration test. The unit test passes session__item to a stand-in table. That shows table_name is used, but not that a real part table produces master__part. A test with a real part table, compared against a row written with insert1, covers that directly. This one passes on your branch and fails without your change:
def test_staged_insert_part_table_matches_insert1(schema_obj, mock_object_storage):
@schema_obj
class StagedSession(dj.Manual):
definition = """
session_id: int
"""
class Item(dj.Part):
definition = """
-> master
item_id: int
---
data_file: <object@>
"""
StagedSession.insert1({"session_id": 1})
StagedSession.Item.insert1({"session_id": 1, "item_id": 1, "data_file": b"regular contents"})
with StagedSession.Item.staged_insert1 as staged:
staged.rec.update(session_id=1, item_id=2)
with staged.open("data_file", ".dat") as destination:
destination.write(b"staged contents")
regular = (StagedSession.Item & {"item_id": 1}).fetch1("data_file")
staged_obj = (StagedSession.Item & {"item_id": 2}).fetch1("data_file")
# Same layout as insert1: _schema/<database>/staged_session__item/...
assert staged_obj.path.split("/")[:3] == regular.path.split("/")[:3]
assert staged_obj.path.split("/")[2] == StagedSession.Item.table_name == "staged_session__item"
assert staged_obj.store == regular.store == "local"
assert staged_obj.read() == b"staged contents"It could go in the TestStagedInsert class in tests/integration/test_object.py, next to the other staged-insert tests. Your new integration test could move there too. That part is up to you.
PR title. The title is cut off at "regular…". The title is what users see in the project history, so could you change it to something like "Fix staged_insert1 object paths and store names to match insert1"?
One optional cleanup: test_staged_insert_metadata_shape_matches_encode in tests/integration/test_object.py passes table.class_name to ObjectCodec.encode (lines 851 and 894). insert1 passes table.table_name. Switching to table_name there would make that comparison match the regular insert path.
Rows staged by earlier versions. Nothing is needed in this PR for them. I'll cover them in the release notes. Here is what I found, for the record:
- No released version recorded
""as the store name fromstaged_insert1.<object@>without a store name raised on insert until #1575, which is not released yet. So only the class-name paths matter. - Those rows keep reading, because the full path is stored in the row.
- On a case-insensitive file store (macOS, Windows), a table's folder keeps the casing of the row that created it. Say an earlier
staged_insert1createdRecording/. Rows written after this fix recordrecording/..., but their files land in that same folder. Garbage collection lists those files asRecording/...and reports them as orphaned, andcollect(dry_run=False)deletes them. I reproduced this on both backends. The same already happens today toinsert1rows in such tables. It needs a fix in garbage collection, not here. - Users can find such rows like this (shown for a table
Recordingwith an<object@>attributedata):
old = [
row for row in Recording.to_dicts()
if f"{Recording.database}/{Recording.table_name}/" not in row["data"].path
]
Thanks for the detailed review! I’ve added the real part-table integration test, updated the metadata comparison to use table_name, and corrected the PR title. All 12 CI checks are passing now. The changes are ready for another review. |
MilagrosMarin
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround, @godara-alt, and welcome to the project!
I checked the latest commit (82a7052) merged with current master:
- Full test suite: 2059 passed, 17 skipped on both MySQL and PostgreSQL.
- I ran master's version of
staged_insert.pyagainst the new tests and 6 of them failed, so each part of the fix has a test that catches it. - I wrote a row with
insert1and a row withstaged_insert1into a part table and compared them, using both<object@>(default store) and<object@named>on both backends. In all four cases the stored paths share the same_schema/<database>/<master>__<part>/prefix, the recorded store is the same (localornamed), and the stored metadata has the same keys. Onlypathandtimestampdiffer, as they should. The staged objects read back correctly. - The earlier requests are all handled: the part-table integration test, the
table_namechange in the metadata test, and the title.
The branch is 9 commits behind master, but none of those commits touch staged insert and it merges cleanly. The only thing left is CI: no checks have run on this commit in this repository yet, so a maintainer will need to start the workflows before it can merge.
Nice, focused fix: two lines in staged_insert.py, with a test for each.
Summary
Fixes #1701.
staged_insert1 was using the Python class name instead of the SQL table name when generating object storage paths. It also recorded an empty store name for <object@> fields using the default store.
This caused inconsistent object paths, incorrect store references when the default configuration changed, and potential data loss during garbage collection.
Changes
Validation
Verified on commit d6aa1cd against the updated upstream master.
The PR contains only the staged-insert fix and its regression tests, without unrelated changes.