gitlab-org--gitlab-foss/lib/gitlab/database/sha_attribute.rb

37 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Gitlab
module Database
BINARY_TYPE =
if Gitlab::Database.postgresql?
# PostgreSQL defines its own class with slightly different
# behaviour from the default Binary type.
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Bytea
else
2018-12-15 04:06:56 -05:00
ActiveModel::Type::Binary
end
# Class for casting binary data to hexadecimal SHA1 hashes (and vice-versa).
#
# Using ShaAttribute allows you to store SHA1 values as binary while still
# using them as if they were stored as string values. This gives you the
# ease of use of string values, but without the storage overhead.
class ShaAttribute < BINARY_TYPE
PACK_FORMAT = 'H*'.freeze
# Casts binary data to a SHA1 in hexadecimal.
2018-12-15 04:06:56 -05:00
def deserialize(value)
value = super(value)
2019-03-13 09:42:43 -04:00
value ? value.unpack1(PACK_FORMAT) : nil
end
# Casts a SHA1 in hexadecimal to the proper binary format.
def serialize(value)
arg = value ? [value].pack(PACK_FORMAT) : nil
2018-12-15 04:06:56 -05:00
super(arg)
end
end
end
end