1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
# frozen_string_literal: true
module ShaAttribute
extend ActiveSupport::Concern
class ShaAttributeTypeMismatchError < StandardError
def initialize(column_name, column_type)
@column_name = column_name
@column_type = column_type
end
def message
"sha_attribute :#{@column_name} should be a :binary column but it is :#{@column_type}"
end
end
class Sha256AttributeTypeMismatchError < ShaAttributeTypeMismatchError
def message
"sha256_attribute :#{@column_name} should be a :binary column but it is :#{@column_type}"
end
end
class_methods do
def sha_attribute(name)
return if Gitlab::Environment.static_verification?
sha_attribute_fields << name
attribute(name, Gitlab::Database::ShaAttribute.new)
end
def sha_attribute_fields
@sha_attribute_fields ||= []
end
def sha256_attribute(name)
return if Gitlab::Environment.static_verification?
sha256_attribute_fields << name
attribute(name, Gitlab::Database::Sha256Attribute.new)
end
def sha256_attribute_fields
@sha256_attribute_fields ||= []
end
# This only gets executed in non-production environments as an additional check to ensure
# the column is the correct type. In production it should behave like any other attribute.
# See https://gitlab.com/gitlab-org/gitlab/merge_requests/5502 for more discussion
def load_schema!
super
return if Rails.env.production?
sha_attribute_fields.each do |field|
column = columns_hash[field.to_s]
if column && column.type != :binary
raise ShaAttributeTypeMismatchError.new(column.name, column.type)
end
end
sha256_attribute_fields.each do |field|
column = columns_hash[field.to_s]
if column && column.type != :binary
raise Sha256AttributeTypeMismatchError.new(column.name, column.type)
end
end
end
end
end
ShaAttribute::ClassMethods.prepend_mod_with('ShaAttribute')
|