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
|
|
|
|
|
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
|
|
|
|
|
|
|
validate_binary_column_exists!(name) unless Rails.env.production?
|
|
|
|
|
|
|
|
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.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/5502 for more discussion
|
|
|
|
def validate_binary_column_exists!(name)
|
2019-02-20 01:37:49 -05:00
|
|
|
return unless database_exists?
|
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
unless table_exists?
|
|
|
|
warn "WARNING: sha_attribute #{name.inspect} is invalid since the table doesn't exist - you may need to run database migrations"
|
|
|
|
return
|
|
|
|
end
|
2017-07-07 05:57:34 -04:00
|
|
|
|
2017-06-29 09:37:37 -04:00
|
|
|
column = columns.find { |c| c.name == name.to_s }
|
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
unless column
|
2018-05-10 11:00:32 -04:00
|
|
|
warn "WARNING: sha_attribute #{name.inspect} is invalid since the column doesn't exist - you may need to run database migrations"
|
|
|
|
return
|
2017-06-29 09:37:37 -04:00
|
|
|
end
|
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
unless column.type == :binary
|
|
|
|
raise ArgumentError.new("sha_attribute #{name.inspect} is invalid since the column type is not :binary")
|
|
|
|
end
|
|
|
|
rescue => error
|
|
|
|
Gitlab::AppLogger.error "ShaAttribute initialization: #{error.message}"
|
|
|
|
raise
|
2017-06-29 09:37:37 -04:00
|
|
|
end
|
2019-02-20 01:37:49 -05:00
|
|
|
|
|
|
|
def database_exists?
|
2019-03-28 09:17:42 -04:00
|
|
|
ApplicationRecord.connection
|
2019-02-20 01:37:49 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
2017-06-29 09:37:37 -04:00
|
|
|
end
|
|
|
|
end
|