<commit_before>module DataFixup
  class FixOrphanedAttachments

    def self.run
      @new_roots = Set.new
      Shackles.activate(:slave) do
        scope = Attachment.
          where('root_attachment_id IS NOT NULL AND
            NOT EXISTS (SELECT id
                        FROM attachments ra
                        WHERE ra.id = attachments.root_attachment_id)')
        create_users if scope.exists?
        scope.find_each(start: 0) do |a|
          next if @new_roots.include? a.root_attachment_id
          fix_orphaned_file(a)
          @new_roots << a.root_attachment_id
        end
      end
    end

    def self.create_users
      Shackles.activate(:master) do
        @deleted_user = User.create(name: 'rescued attachments')
        @deleted_user.destroy
        @broken_user = User.create(name: 'broken attachments')
        @broken_user.destroy
      end
    end

    def self.other_namespace(attachment)
      account_id = attachment.namespace.sub('account_', '').to_i
      if account_id.to_s.length > 8
        namespace_account_id = Account.find(account_id).local_id
      else
        namespace_account_id = attachment.shard.global_id_for(account_id)
      end
      "account_#{namespace_account_id}"
    end

    def self.s3_save(rescued_orphan, ns)
      if rescued_orphan.s3object.exists?
        rescued_orphan.save!
        rescued_orphan.namespace = ns
        finalize_attachment(rescued_orphan)
      else
        ns = rescued_orphan.namespace = other_namespace(rescued_orphan)
        if rescued_orphan.s3object.exists?
          rescued_orphan.save!
          rescued_orphan.namespace = ns
          finalize_attachment(rescued_orphan)
        else<commit_msg>don't fail migration if account isn't found<commit_after>module DataFixup
  class FixOrphanedAttachments

    def self.run
      @new_roots = Set.new
      Shackles.activate(:slave) do
        scope = Attachment.
          where('root_attachment_id IS NOT NULL AND
            NOT EXISTS (SELECT id
                        FROM attachments ra
                        WHERE ra.id = attachments.root_attachment_id)')
        create_users if scope.exists?
        scope.find_each(start: 0) do |a|
          next if @new_roots.include? a.root_attachment_id
          fix_orphaned_file(a)
          @new_roots << a.root_attachment_id
        end
      end
    end

    def self.create_users
      Shackles.activate(:master) do
        @deleted_user = User.create(name: 'rescued attachments')
        @deleted_user.destroy
        @broken_user = User.create(name: 'broken attachments')
        @broken_user.destroy
      end
    end

    def self.other_namespace(attachment)
      account_id = attachment.namespace.sub('account_', '').to_i
      if account_id.to_s.length > 8
        namespace_account_id = Account.find_by_id(account_id).try(:local_id)
      else
        namespace_account_id = attachment.shard.global_id_for(account_id)
      end
      "account_#{namespace_account_id}"
    end

    def self.s3_save(rescued_orphan, ns)
      if rescued_orphan.s3object.exists?
        rescued_orphan.save!
        rescued_orphan.namespace = ns
        finalize_attachment(rescued_orphan)
      else
        ns = rescued_orphan.namespace = other_namespace(rescued_orphan)
        if rescued_orphan.s3object.exists?
          rescued_orphan.save!
          rescued_orphan.namespace = ns
          finalize_attachment(rescued_orphan)
        else
{'fraction_non_alphanumeric': 0.07363213038416763, 'fraction_numerical': 0.002910360884749709, 'mean_word_length': 2.346640701071081, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4658031', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1041, 'n_words': 197}