Class: Gitlab::BackgroundMigration::MigrateFingerprintSha256WithinKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/background_migration/migrate_fingerprint_sha256_within_keys.rb

Overview

This class is responsible to update all sha256 fingerprints within the keys table

Defined Under Namespace

Classes: Key

Constant Summary collapse

TEMP_TABLE =
'tmp_fingerprint_sha256_migration'

Instance Method Summary collapse

Instance Method Details

#perform(start_id, stop_id) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gitlab/background_migration/migrate_fingerprint_sha256_within_keys.rb', line 17

def perform(start_id, stop_id)
  ActiveRecord::Base.transaction do
    execute(<<~SQL)
      CREATE TEMPORARY TABLE #{TEMP_TABLE}
        (id bigint primary key, fingerprint_sha256 bytea not null)
        ON COMMIT DROP
    SQL

    fingerprints = []
    Key.where(id: start_id..stop_id, fingerprint_sha256: nil).find_each do |regular_key|
      if fingerprint = generate_ssh_public_key(regular_key.key)
        bytea = ActiveRecord::Base.connection.escape_bytea(Base64.decode64(fingerprint))

        fingerprints << {
          id: regular_key.id,
          fingerprint_sha256: bytea
        }
      end
    end

    Gitlab::Database.bulk_insert(TEMP_TABLE, fingerprints) # rubocop:disable Gitlab/BulkInsert

    execute("ANALYZE #{TEMP_TABLE}")

    execute(<<~SQL)
      UPDATE keys
        SET fingerprint_sha256 = t.fingerprint_sha256
        FROM #{TEMP_TABLE} t
        WHERE keys.id = t.id
    SQL
  end
end