Skip to content

Apply updates to unrestricted external tables#1536

Draft
samuelbray32 wants to merge 3 commits intomasterfrom
checksum_fix
Draft

Apply updates to unrestricted external tables#1536
samuelbray32 wants to merge 3 commits intomasterfrom
checksum_fix

Conversation

@samuelbray32
Copy link
Collaborator

@samuelbray32 samuelbray32 commented Feb 19, 2026

Description

Fixes #1535

  • tracks the unrestricted tables for external updates and applies updates to these

Checklist:

  • NA If this PR should be accompanied by a release, I have updated the CITATION.cff
  • NA If this PR edits table definitions, I have included an alter snippet for release notes.
  • NA If this PR makes changes to position, I ran the relevant tests locally.
  • NA If this PR makes user-facing changes, I have added/edited docs/notebooks to reflect the changes
  • I have updated the CHANGELOG.md with PR number and description.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a bug where _resolve_external_table was attempting to update restricted external tables in DataJoint, which raises a DataJointError. The solution introduces tracking of unrestricted table objects separately so updates can be applied to the correct unrestricted tables instead of the restricted query results.

Changes:

  • Modified _resolve_external_table to maintain a parallel list (table_to_update) of unrestricted table objects alongside the existing list of restricted tables (to_updates)
  • Applied database updates to unrestricted tables instead of restricted ones, resolving the DataJointError from issue #1535
  • Minor formatting changes: removed empty line after imports and repositioned noqa comment

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 465 to 527
def _resolve_external_table(
filepath: str, file_name: str, location: str = "analysis"
):
"""Function to resolve database vs. file property discrepancies.

WARNING: This should only be used when editing file metadata. Can violate
data integrity if improperly used.

Parameters
----------
filepath : str
abs path to the file to edit
file_name : str
name of the file to edit
location : str, optional
which external table the file is in, current options are
["analysis", "raw], by default "analysis"
"""
from spyglass.common import LabMember
from spyglass.common.common_nwbfile import AnalysisRegistry
from spyglass.common.common_nwbfile import schema as common_schema

LabMember().check_admin_privilege(
error_message="Please contact database admin to edit database checksums"
)

file_restr = f"filepath LIKE '%{file_name}'"

to_updates = []
table_to_update = []
if location == "analysis": # Update for each custom Analysis external
for external in AnalysisRegistry().get_externals():
restr_external = external & file_restr
if not bool(restr_external):
continue
if len(restr_external) > 1:
raise ValueError(
"Multiple entries found in external table for file: "
+ f"{file_name}, cannot resolve."
)
to_updates.append(restr_external)
table_to_update.append(external)

elif location == "raw":
restr_external = common_schema.external["raw"] & file_restr
to_updates.append(restr_external)
table_to_update.append(common_schema.external["raw"])

if not to_updates:
logger.warning(
f"No entries found in external tables for file: {file_name}"
)
return

update_vals = dict(
size=Path(filepath).stat().st_size,
contents_hash=dj.hash.uuid_from_file(filepath),
)
for to_update in to_updates:
for to_update, table in zip(to_updates, table_to_update):
key = to_update.fetch1()
key.update(update_vals)
to_update.update1(key)
table.update1(key)

Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

The _resolve_external_table function lacks test coverage. Given that this function handles critical database updates and has been the source of a bug (as per issue #1535), adding tests would help prevent regressions. The test file tests/utils/test_dj_helper_fn.py exists but only contains a single deprecation test. Consider adding tests for both the "analysis" and "raw" location branches, including edge cases like empty results, multiple entries, and successful updates.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@copilot open a new pull request to apply changes based on this feedback

Copy link
Contributor

Copilot AI commented Feb 25, 2026

@samuelbray32 I've opened a new pull request, #1543, to work on those changes. Once the pull request is ready, I'll request review from you.

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.

Update error in _resolve_external_table

3 participants