Commit 667a295c authored by Stan Hu's avatar Stan Hu

Fix error 500s creating new projects due to empty weights

GitLab 13.1.0 added the
`application_settings.repository_storages_weighted` column, but it was
possible that the value was not seeded properly due to a stale schema
cache due to the use of ActiveRecord inside the migrations. Admins that
upgraded might see 500 errors when creating projects.

To ensure that the data is seeded properly, we surgically add a
migration that seeds the data only if it is not present. The schema
version was intentionally used to avoid backporting conflicts and to
ensure any background migrations will have the correct data when they
run. The changes were largely taken from
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/35814.

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/225203
parent b3f41861
---
title: Fix error 500s creating new projects due to empty weights
merge_request: 35829
author:
type: fixed
# frozen_string_literal: true
class ReseedRepositoryStoragesWeighted < ActiveRecord::Migration[6.0]
DOWNTIME = false
class ApplicationSetting < ActiveRecord::Base
serialize :repository_storages
self.table_name = 'application_settings'
end
def up
reseed_repository_storages_weighted
end
private
def reseed_repository_storages_weighted
# We need to flush the cache to ensure the newly-added column is loaded
ApplicationSetting.reset_column_information
# There should only be one row here due to
# 20200420162730_remove_additional_application_settings_rows.rb
ApplicationSetting.all.each do |settings|
# Admins may have already tweaked these values, so don't do anything
# if there is data already.
next if settings.repository_storages_weighted.present?
storages = Gitlab.config.repositories.storages.keys.collect do |storage|
weight = settings.repository_storages.include?(storage) ? 100 : 0
[storage.to_sym, weight]
end
settings.repository_storages_weighted = Hash[storages]
settings.save!
end
end
end
...@@ -23396,6 +23396,7 @@ COPY "schema_migrations" (version) FROM STDIN; ...@@ -23396,6 +23396,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200508091106 20200508091106
20200508140959 20200508140959
20200508203901 20200508203901
20200509203901
20200511080113 20200511080113
20200511083541 20200511083541
20200511092246 20200511092246
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20200509203901_reseed_repository_storages_weighted.rb')
RSpec.describe ReseedRepositoryStoragesWeighted do
let(:storages) { { "foo" => {}, "baz" => {} } }
let(:application_settings) do
table(:application_settings).tap do |klass|
klass.class_eval do
serialize :repository_storages
end
end
end
before do
allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
end
let(:repository_storages) { ["foo"] }
let!(:application_setting) { application_settings.create!(repository_storages: repository_storages) }
context 'with empty repository_storages_weighted column' do
it 'populates repository_storages_weighted properly' do
migrate!
expect(application_settings.find(application_setting.id).repository_storages_weighted).to eq({ "foo" => 100, "baz" => 0 })
end
end
context 'with already-populated repository_storages_weighted column' do
let(:existing_weights) { { "foo" => 100, "baz" => 50 } }
it 'does not change repository_storages_weighted properly' do
application_setting.repository_storages_weighted = existing_weights
application_setting.save!
migrate!
expect(application_settings.find(application_setting.id).repository_storages_weighted).to eq(existing_weights)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment