2018-08-03 17:22:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-29 13:37:37 +00:00
|
|
|
module ShaAttribute
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-08-27 12:35:31 +00:00
|
|
|
class_methods do
|
2017-06-29 13:37:37 +00:00
|
|
|
def sha_attribute(name)
|
2017-12-26 06:36:33 +00:00
|
|
|
return if ENV['STATIC_VERIFICATION']
|
2018-05-08 00:14:00 +00:00
|
|
|
|
2020-07-30 18:09:39 +00:00
|
|
|
validate_binary_column_exists!(name) if Rails.env.development?
|
2018-05-08 00:14:00 +00:00
|
|
|
|
|
|
|
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-09-18 14:02:45 +00:00
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/merge_requests/5502 for more discussion
|
2018-05-08 00:14:00 +00:00
|
|
|
def validate_binary_column_exists!(name)
|
2019-02-20 06:37:49 +00:00
|
|
|
return unless database_exists?
|
2020-07-30 18:09:39 +00:00
|
|
|
return unless table_exists?
|
2017-07-07 09:57:34 +00:00
|
|
|
|
2017-06-29 13:37:37 +00:00
|
|
|
column = columns.find { |c| c.name == name.to_s }
|
|
|
|
|
2020-07-30 18:09:39 +00:00
|
|
|
return unless column
|
2017-06-29 13:37:37 +00:00
|
|
|
|
2018-05-08 00:14:00 +00:00
|
|
|
unless column.type == :binary
|
2021-05-04 15:10:36 +00:00
|
|
|
raise ArgumentError, "sha_attribute #{name.inspect} is invalid since the column type is not :binary"
|
2018-05-08 00:14:00 +00:00
|
|
|
end
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError => error
|
2018-05-08 00:14:00 +00:00
|
|
|
Gitlab::AppLogger.error "ShaAttribute initialization: #{error.message}"
|
|
|
|
raise
|
2017-06-29 13:37:37 +00:00
|
|
|
end
|
2019-02-20 06:37:49 +00:00
|
|
|
|
|
|
|
def database_exists?
|
2021-07-29 15:09:48 +00:00
|
|
|
Gitlab::Database.main.exists?
|
2019-02-20 06:37:49 +00:00
|
|
|
end
|
2017-06-29 13:37:37 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
ShaAttribute::ClassMethods.prepend_mod_with('ShaAttribute')
|