gitlab-org--gitlab-foss/lib/gitlab/database/sha_attribute.rb
Andreas Brandl 988dc80585
Further remove code branches by database type
We dropped MySQL support and a lot of mysql specific code has been
removed in https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/29608.

This comes in from the other direction and removes any `if postgresql?`
branches.
2019-07-29 12:47:06 +02:00

31 lines
995 B
Ruby

# frozen_string_literal: true
module Gitlab
module Database
# PostgreSQL defines its own class with slightly different
# behaviour from the default Binary type.
BINARY_TYPE = ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Bytea
# 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.
def deserialize(value)
value = super(value)
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
super(arg)
end
end
end
end