Bug: Deleted package versions are blocking publishing

Context

In https://gitlab.com/gitlab-org/gitlab/-/issues/348166, the Package group decoupled the database deletion from object storage deletion. So, moving forward, there are two background jobs that run as part of the deletion process. The first is a loop job that cycles through packages that are marked for deletion and deletes them. The second is a parent job that runs every 12 hours and looks for any packages that are marked for deletion and restarts the loop.

Problem to solve

The problem is that someone may delete a package and then publish the same name/version of that package soon after the deletion. However, the validation logic will reference the package that, although marked for deletion, may not have been deleted yet.

This means that users that delete a package and publish a new one will receive an error and may be blocked from publishing another package for a full work day.

Proposed fix

From https://gitlab.com/gitlab-org/gitlab/-/issues/348166#note_828895818

  • Background jobs: when a Packages::MarkPackageFilesForDestructionWorker, enqueue (with capacity) a Packages::CleanupPackageFileWorker.
    • This way, we don't wait for the cron worker to kick in to enqueue Packages::CleanupPackageFileWorker
  • Database indexes: we have 3 database indexes that implement a UNIQUE constraint. Update those so that we don't take pending_destruction packages into account.
  • Custom model validations: those are functions that mainly query the database for package existence (they are used for enforcing naming conventions). Update those functions so that pending_destruction packages are not taken into account.

Related to #348166.

Edited by Tim Rizzi