Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions warehouse/forklift/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,11 +1530,6 @@ def file_upload(request):
f"Invalid attestations supplied during upload: {e}",
)

# TODO: This should be handled by some sort of database trigger or a
# SQLAlchemy hook or the like instead of doing it inline in this
# view.
request.db.add(Filename(filename=filename))

# Store the information about the file in the database.
file_ = File(
release=release,
Expand Down
18 changes: 18 additions & 0 deletions warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Text,
UniqueConstraint,
cast,
event,
func,
or_,
orm,
Expand Down Expand Up @@ -1207,3 +1208,20 @@ class AlternateRepository(db.Model):
name: Mapped[str]
url: Mapped[str]
description: Mapped[str]


@event.listens_for(File, "after_insert")
def add_filename_to_registry(mapper, connection, target):
"""
Log the new filename to the Filename (file_registry) table.

This event listener is triggered *after* a new `File` object is
successfully inserted into the database.

We use a direct connection-level insert (`connection.execute()`)
instead of `session.add(Filename(...))` to avoid an `SAWarning`.
Modifying the session *during* the flush process (which is when
this hook runs) is not a supported operation. This method
bypasses the session's unit-of-work tracking and is safe here.
"""
connection.execute(Filename.__table__.insert(), {"filename": target.filename})