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

44 lines
1.3 KiB
Ruby
Raw 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-01-26 12:08:38 +05:30
# Needed for the database method
include DatabaseReflection
2018-11-20 20:47:30 +05:30
class_methods do
2017-09-10 17:25:29 +05:30
def sha_attribute(name)
2018-03-17 18:26:18 +05:30
return if ENV['STATIC_VERIFICATION']
2018-10-15 14:42:47 +05:30
2022-01-26 12:08:38 +05:30
validate_binary_column_exists!(name) if Rails.env.development? || Rails.env.test?
2018-10-15 14:42:47 +05:30
attribute(name, Gitlab::Database::ShaAttribute.new)
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.
2019-12-04 20:38:33 +05:30
# See https://gitlab.com/gitlab-org/gitlab/merge_requests/5502 for more discussion
2018-10-15 14:42:47 +05:30
def validate_binary_column_exists!(name)
2019-07-07 11:18:12 +05:30
return unless database_exists?
2020-10-24 23:57:45 +05:30
return unless table_exists?
2017-09-10 17:25:29 +05:30
column = columns.find { |c| c.name == name.to_s }
2020-10-24 23:57:45 +05:30
return unless column
2017-09-10 17:25:29 +05:30
2018-10-15 14:42:47 +05:30
unless column.type == :binary
2021-06-08 01:23:25 +05:30
raise ArgumentError, "sha_attribute #{name.inspect} is invalid since the column type is not :binary"
2018-10-15 14:42:47 +05:30
end
2021-06-08 01:23:25 +05:30
rescue StandardError => error
2018-10-15 14:42:47 +05:30
Gitlab::AppLogger.error "ShaAttribute initialization: #{error.message}"
raise
2017-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
def database_exists?
2021-12-11 22:18:48 +05:30
database.exists?
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')