debian-mirror-gitlab/app/models/concerns/sha_attribute.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2 KiB
Ruby
Raw Permalink Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module ShaAttribute
extend ActiveSupport::Concern
2022-07-16 23:28:13 +05:30
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
2022-01-26 12:08:38 +05:30
2018-11-20 20:47:30 +05:30
class_methods do
2017-09-10 17:25:29 +05:30
def sha_attribute(name)
2023-04-23 21:23:45 +05:30
return if Gitlab::Environment.static_verification?
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
sha_attribute_fields << name
2018-10-15 14:42:47 +05:30
attribute(name, Gitlab::Database::ShaAttribute.new)
end
2022-07-16 23:28:13 +05:30
def sha_attribute_fields
@sha_attribute_fields ||= []
end
def sha256_attribute(name)
2023-04-23 21:23:45 +05:30
return if Gitlab::Environment.static_verification?
2022-07-16 23:28:13 +05:30
sha256_attribute_fields << name
attribute(name, Gitlab::Database::Sha256Attribute.new)
end
def sha256_attribute_fields
@sha256_attribute_fields ||= []
end
2018-10-15 14:42:47 +05:30
# 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-12-04 20:38:33 +05:30
# See https://gitlab.com/gitlab-org/gitlab/merge_requests/5502 for more discussion
2022-07-16 23:28:13 +05:30
def load_schema!
super
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
return if Rails.env.production?
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
sha_attribute_fields.each do |field|
column = columns_hash[field.to_s]
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
if column && column.type != :binary
raise ShaAttributeTypeMismatchError.new(column.name, column.type)
end
2018-10-15 14:42:47 +05:30
end
2019-07-07 11:18:12 +05:30
2022-07-16 23:28:13 +05:30
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-07-07 11:18:12 +05:30
end
2017-09-10 17:25:29 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
ShaAttribute::ClassMethods.prepend_mod_with('ShaAttribute')