warehouse icon indicating copy to clipboard operation
warehouse copied to clipboard

refactor(packaging): Use SQLAlchemy event for Filename registry (Fixes #576)

Open Delta17920 opened this issue 1 month ago • 0 comments

Fixes #576.

This PR refactors the logic for adding new filenames to the file_registry (Filename model).

As suggested by the issue, this logic was previously handled inline in the file_upload view, which is not ideal. This PR moves the logic to a SQLAlchemy event listener.

Changes:

  1. Removed the request.db.add(Filename(filename=filename)) call from warehouse/forklift/legacy.py.
  2. Added an @event.listens_for(File, "after_insert") hook to warehouse/packaging/models.py.

This new hook automatically inserts the File.filename into the file_registry table when a new File is created.

To resolve the SAWarning about modifying a session during a flush, the hook uses a direct connection.execute() operation.


Proof / Test Output:

The existing tests in tests/unit/forklift/test_legacy.py still pass, which confirms the filename logging logic was successfully moved.

image

Delta17920 avatar Oct 27 '25 05:10 Delta17920