<commit_before>module Helpers
  module DeleteContentItem
    def self.destroy_content_items_with_links(content_ids)
      content_ids = Array(content_ids)

      content_items = ContentItem.where(content_id: content_ids)
      destroy_supporting_objects(content_items)
      content_items.destroy_all

      destroy_links(content_ids)
    end

    def self.destroy_supporting_objects(content_items)
      content_items = Array(content_items)

      supporting_classes = [
        AccessLimit,
        Linkable,
        Location,
        State,
        Translation,
        Unpublishing,
        UserFacingVersion,
        ChangeNote,
      ]

      supporting_classes.each do |klass|
        klass.where(content_item: content_items).destroy_all
      end

      LockVersion.where(target: content_items).destroy_all
    end

    def self.destroy_links(content_ids)
      content_ids = Array(content_ids)
      LinkSet.where(content_id: content_ids).destroy_all
      Link.where(target_content_id: content_ids).destroy_all
    end
  end
end
<commit_msg>A slightly more resilient delete_content_item helper<commit_after>module Helpers
  module DeleteContentItem
    def self.destroy_content_items_with_links(content_ids)
      content_ids = Array(content_ids)

      content_items = ContentItem.where(content_id: content_ids)
      destroy_supporting_objects(content_items)
      content_items.destroy_all

      destroy_links(content_ids)
    end

    def self.destroy_supporting_objects(content_items)
      content_items = Array(content_items)

      supporting_classes = [
        AccessLimit,
        Location,
        State,
        Translation,
        Unpublishing,
        UserFacingVersion,
        ChangeNote,
      ]

      supporting_classes.each do |klass|
        next unless ActiveRecord::Base.connection.data_source_exists?(klass.table_name)
        klass.where(content_item: content_items).destroy_all
      end

      LockVersion.where(target: content_items).destroy_all
    end

    def self.destroy_links(content_ids)
      content_ids = Array(content_ids)
      LinkSet.where(content_id: content_ids).destroy_all
      Link.where(target_content_id: content_ids).destroy_all
    end
  end
end

{'fraction_non_alphanumeric': 0.06196291270918137, 'fraction_numerical': 0.0, 'mean_word_length': 3.1657250470809792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6875864', 'n_tokens_mistral': 648, 'n_tokens_neox': 639, 'n_words': 103}