From 18094a17458aab8c9d629b86946b61e0e7bab251 Mon Sep 17 00:00:00 2001 From: Brett Walker Date: Tue, 8 May 2018 00:14:00 +0000 Subject: =?UTF-8?q?Backport:=20Keep=20ShaAttribute=20from=20halting=20star?= =?UTF-8?q?tup=20when=20we=20can=E2=80=99t=20connect=20to=20a=20database?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/concerns/sha_attribute.rb | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'app/models/concerns/sha_attribute.rb') diff --git a/app/models/concerns/sha_attribute.rb b/app/models/concerns/sha_attribute.rb index 703a72c355c..3340dc96e9f 100644 --- a/app/models/concerns/sha_attribute.rb +++ b/app/models/concerns/sha_attribute.rb @@ -4,18 +4,33 @@ module ShaAttribute module ClassMethods def sha_attribute(name) return if ENV['STATIC_VERIFICATION'] - return unless table_exists? + + 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) + 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 column = columns.find { |c| c.name == name.to_s } - # In case the table doesn't exist we won't be able to find the column, - # thus we will only check the type if the column is present. - if column && column.type != :binary - raise ArgumentError, - "sha_attribute #{name.inspect} is invalid since the column type is not :binary" + unless column + raise ArgumentError.new("sha_attribute #{name.inspect} is invalid since the column doesn't exist") end - attribute(name, Gitlab::Database::ShaAttribute.new) + 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 end end end -- cgit v1.2.1 From 42d27f0b43df544bab2ad5bc4e082728d86c7388 Mon Sep 17 00:00:00 2001 From: Brett Walker Date: Thu, 10 May 2018 10:00:32 -0500 Subject: only issue a warning if column doesn't exist --- app/models/concerns/sha_attribute.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'app/models/concerns/sha_attribute.rb') diff --git a/app/models/concerns/sha_attribute.rb b/app/models/concerns/sha_attribute.rb index 3340dc96e9f..3796737427a 100644 --- a/app/models/concerns/sha_attribute.rb +++ b/app/models/concerns/sha_attribute.rb @@ -22,7 +22,8 @@ module ShaAttribute column = columns.find { |c| c.name == name.to_s } unless column - raise ArgumentError.new("sha_attribute #{name.inspect} is invalid since the column doesn't exist") + warn "WARNING: sha_attribute #{name.inspect} is invalid since the column doesn't exist - you may need to run database migrations" + return end unless column.type == :binary -- cgit v1.2.1