2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-29 09:37:37 -04:00
|
|
|
module ShaAttribute
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2022-05-03 20:09:14 -04:00
|
|
|
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
|
2021-11-18 01:10:36 -05:00
|
|
|
|
2018-08-27 08:35:31 -04:00
|
|
|
class_methods do
|
2017-06-29 09:37:37 -04:00
|
|
|
def sha_attribute(name)
|
2017-12-26 01:36:33 -05:00
|
|
|
return if ENV['STATIC_VERIFICATION']
|
2018-05-07 20:14:00 -04:00
|
|
|
|
2022-05-03 20:09:14 -04:00
|
|
|
sha_attribute_fields << name
|
2018-05-07 20:14:00 -04:00
|
|
|
|
|
|
|
attribute(name, Gitlab::Database::ShaAttribute.new)
|
|
|
|
end
|
|
|
|
|
2022-05-03 20:09:14 -04:00
|
|
|
def sha_attribute_fields
|
|
|
|
@sha_attribute_fields ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha256_attribute(name)
|
|
|
|
return if ENV['STATIC_VERIFICATION']
|
|
|
|
|
|
|
|
sha256_attribute_fields << name
|
|
|
|
|
|
|
|
attribute(name, Gitlab::Database::Sha256Attribute.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha256_attribute_fields
|
|
|
|
@sha256_attribute_fields ||= []
|
|
|
|
end
|
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
# 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.
|
2019-09-18 10:02:45 -04:00
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/merge_requests/5502 for more discussion
|
2022-05-03 20:09:14 -04:00
|
|
|
def load_schema!
|
|
|
|
super
|
2017-07-07 05:57:34 -04:00
|
|
|
|
2022-05-03 20:09:14 -04:00
|
|
|
return if Rails.env.production?
|
2017-06-29 09:37:37 -04:00
|
|
|
|
2022-05-03 20:09:14 -04:00
|
|
|
sha_attribute_fields.each do |field|
|
|
|
|
column = columns_hash[field.to_s]
|
2017-06-29 09:37:37 -04:00
|
|
|
|
2022-05-03 20:09:14 -04:00
|
|
|
if column && column.type != :binary
|
|
|
|
raise ShaAttributeTypeMismatchError.new(column.name, column.type)
|
|
|
|
end
|
2018-05-07 20:14:00 -04:00
|
|
|
end
|
2019-02-20 01:37:49 -05:00
|
|
|
|
2022-05-03 20:09:14 -04:00
|
|
|
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
|
2019-02-20 01:37:49 -05:00
|
|
|
end
|
2017-06-29 09:37:37 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
ShaAttribute::ClassMethods.prepend_mod_with('ShaAttribute')
|