Prevent orphaned job artifacts

In !191506 (merged), we introduced some temporary code that removes orphaned job artifacts.

We should find the root cause of why we have orphaned job artifacts. Maybe it's because we're using deleting_all for CommitStatus.

p_ci_builds (table for CommitStatus) does have a foreign key with cascade delete on p_ci_job_artifacts, but it's not working because of Bug in PostgreSQL causes integrity issues relat... (#508672). However, we shouldn't rely solely on the FK to delete the artifacts, as this will result in orphaned records in the file storage.

We can consider using fast destroy here to delete the job artifacts related to CommitStatus if there are any.

Refer #499012 (comment 2509435194)

See the such records on Kibana: https://log.gprd.gitlab.net/app/r/s/fx5Lb

Technical Proposal

A very small change

    def destroy_ci_records!
     ...

      all_pipelines.find_each(batch_size: BATCH_SIZE) do |pipeline| # rubocop: disable CodeReuse/ActiveRecord
        ::Ci::DestroyPipelineService.new(project, current_user).unsafe_execute(pipeline)
      end

-     delete_commit_statuses
      destroy_orphaned_ci_job_artifacts!
+     delete_commit_statuses
    end

With a fuller explanation below.

Verifying the fix

Check the application logs linked to in this issue and make sure that we don't have any more orphaned artifacts that could end up stranding files in ObjectStorage.

We have 30 days of retention in Kibana now, so if we don't see this occur for a whole month we can probably close it out as resolved.

Edited by 🤖 GitLab Bot 🤖