summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/digest_column.rb
blob: 22a3bb8f8f39cbf568f9120b4bff6a1d0a8b2546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# frozen_string_literal: true

# rubocop:disable Style/Documentation
module Gitlab
  module BackgroundMigration
    class DigestColumn
      class PersonalAccessToken < ActiveRecord::Base
        self.table_name = 'personal_access_tokens'
      end

      def perform(model, attribute_from, attribute_to, start_id, stop_id)
        model = model.constantize if model.is_a?(String)

        model.transaction do
          relation = model.where(id: start_id..stop_id).where.not(attribute_from => nil).lock

          relation.each do |instance|
            instance.update_columns(attribute_to => Gitlab::CryptoHelper.sha256(instance.read_attribute(attribute_from)),
                                    attribute_from => nil)
          end
        end
      end
    end
  end
end