Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-11 21:08:10 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-11 21:08:10 +0300
commit175b4fa261259ab0d033482d10bb4159fee8e538 (patch)
treee1f1dba5e41177f11ffded5a505e0e7f692b8df5 /lib/gitlab/database
parent4eea104c69e59f6fa53c7bc15b986c69f29b60c8 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/database')
-rw-r--r--lib/gitlab/database/sha256_attribute.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/gitlab/database/sha256_attribute.rb b/lib/gitlab/database/sha256_attribute.rb
new file mode 100644
index 00000000000..adf3f7fb5a6
--- /dev/null
+++ b/lib/gitlab/database/sha256_attribute.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Database
+ # Class for casting binary data to hexadecimal SHA256 hashes (and vice-versa).
+ #
+ # Using Sha256Attribute allows you to store SHA256 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 Sha256Attribute < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Bytea
+ # Casts binary data to a SHA256 and remove trailing = and newline from encode64
+ def deserialize(value)
+ value = super(value)
+ if value.present?
+ Base64.encode64(value).delete("=").chomp("\n")
+ else
+ nil
+ end
+ end
+
+ # Casts a SHA256 in a proper binary format. which is 32 bytes long
+ def serialize(value)
+ arg = if value.present?
+ Base64.decode64(value)
+ else
+ nil
+ end
+
+ super(arg)
+ end
+ end
+ end
+end