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

50 lines
1.5 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
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-07-07 11:18:12 +05:30
return unless database_exists?
2018-10-15 14:42:47 +05:30
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-09-10 17:25:29 +05:30
column = columns.find { |c| c.name == name.to_s }
2018-10-15 14:42:47 +05:30
unless column
warn "WARNING: sha_attribute #{name.inspect} is invalid since the column doesn't exist - you may need to run database migrations"
return
2017-09-10 17:25:29 +05:30
end
2018-10-15 14:42:47 +05:30
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-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
def database_exists?
ApplicationRecord.connection
true
rescue
false
end
2017-09-10 17:25:29 +05:30
end
end