warehouse
warehouse copied to clipboard
refactor(packaging): Use SQLAlchemy event for Filename registry (Fixes #576)
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:
- Removed the
request.db.add(Filename(filename=filename))call fromwarehouse/forklift/legacy.py. - Added an
@event.listens_for(File, "after_insert")hook towarehouse/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.