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

41 lines
1.2 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
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
2020-10-24 23:57:45 +05:30
validate_binary_column_exists!(name) if Rails.env.development?
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-10-27 15:23:28 +05:30
Gitlab::Database.main.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')