Skip to content

Fix staged_insert1 object paths and store names to match insert1 - #1741

Open
godara-alt wants to merge 2 commits into
datajoint:masterfrom
godara-alt:fix/1701-clean-upstream
Open

godara-alt wants to merge 2 commits into
datajoint:masterfrom
godara-alt:fix/1701-clean-upstream

Conversation

@godara-alt

Copy link
Copy Markdown
Contributor

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

  • Use table.table_name instead of the Python class name when generating object storage paths.
  • Resolve and preserve the actual store name when staging objects.
  • Ensure metadata generation and cleanup continue using the originally resolved store.
  • Add regression tests covering regular tables, part tables, explicit stores, and default-store changes.
  • Add an integration test verifying that stored objects remain accessible after the default store changes.

Validation

Verified on commit d6aa1cd against the updated upstream master.

  • Lint and formatting: Passed
  • Linux unit tests: Passed
  • Windows unit tests: Passed on Python 3.10, 3.14, and 3.15
  • MySQL integration tests: All 6 configurations passed
  • Minimum dependency tests: Passed
  • Overall: All 11 test jobs passed

The PR contains only the staged-insert fix and its regression tests, without unrelated changes.

@dimitri-yatsenko dimitri-yatsenko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 from staged_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_insert1 created Recording/. Rows written after this fix record recording/..., but their files land in that same folder. Garbage collection lists those files as Recording/... and reports them as orphaned, and collect(dry_run=False) deletes them. I reproduced this on both backends. The same already happens today to insert1 rows in such tables. It needs a fix in garbage collection, not here.
  • Users can find such rows like this (shown for a table Recording with an <object@> attribute data):
old = [
    row for row in Recording.to_dicts()
    if f"{Recording.database}/{Recording.table_name}/" not in row["data"].path
]
@godara-alt godara-alt changed the title fix: align staged insert object paths and store metadata with regular… Nov 24, 2026
@godara-alt

Copy link
Copy Markdown
Contributor Author

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 from staged_insert1. <object@> without a store name raised on insert until Resolve an empty store name to the default store #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_insert1 created Recording/. Rows written after this fix record recording/..., but their files land in that same folder. Garbage collection lists those files as Recording/... and reports them as orphaned, and collect(dry_run=False) deletes them. I reproduced this on both backends. The same already happens today to insert1 rows in such tables. It needs a fix in garbage collection, not here.
  • Users can find such rows like this (shown for a table Recording with an <object@> attribute data):
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 MilagrosMarin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py against 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 insert1 and a row with staged_insert1 into 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 (local or named), and the stored metadata has the same keys. Only path and timestamp differ, as they should. The staged objects read back correctly.
  • The earlier requests are all handled: the part-table integration test, the table_name change 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants