diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-06-29 16:37:37 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-06-29 16:37:37 +0300 |
commit | af1f6844c98bfb4adda1c20dc75b808f031a4256 (patch) | |
tree | 7042096cf2b58ed08067f8c24b667226cbf32cf3 /app/models/concerns/sha_attribute.rb | |
parent | 88c60307bd1f215095834f09a1a5cb18701ac8ad (diff) |
Added code for defining SHA attributes
These attributes are stored in binary in the database, but exposed as
strings. This allows one to query/create data using plain SHA1 hashes as
Strings, while storing them more efficiently as binary.
Diffstat (limited to 'app/models/concerns/sha_attribute.rb')
-rw-r--r-- | app/models/concerns/sha_attribute.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/app/models/concerns/sha_attribute.rb b/app/models/concerns/sha_attribute.rb new file mode 100644 index 00000000000..c28974a3cdf --- /dev/null +++ b/app/models/concerns/sha_attribute.rb @@ -0,0 +1,18 @@ +module ShaAttribute + extend ActiveSupport::Concern + + module ClassMethods + def sha_attribute(name) + column = columns.find { |c| c.name == name.to_s } + + # In case the table doesn't exist we won't be able to find the column, + # thus we will only check the type if the column is present. + if column && column.type != :binary + raise ArgumentError, + "sha_attribute #{name.inspect} is invalid since the column type is not :binary" + end + + attribute(name, Gitlab::Database::ShaAttribute.new) + end + end +end |