diff --git a/changelogs/unreleased/jc-add-seed-to-repository-storages-weighted.yml b/changelogs/unreleased/jc-add-seed-to-repository-storages-weighted.yml new file mode 100644 index 0000000000000000000000000000000000000000..627697f510c10043e1185dcd15c0c95049c07f9b --- /dev/null +++ b/changelogs/unreleased/jc-add-seed-to-repository-storages-weighted.yml @@ -0,0 +1,5 @@ +--- +title: Fix existing repository_storages_weighted migrations +merge_request: 35814 +author: +type: fixed diff --git a/db/migrate/20200508203901_add_repository_storages_weighted_to_application_settings.rb b/db/migrate/20200508203901_add_repository_storages_weighted_to_application_settings.rb index b9d4f65989af2b2fdf91b0b7ff0d330129bbd3d5..fecaed9a7a0433f9dd6abb4d7a9005d8b65dd436 100644 --- a/db/migrate/20200508203901_add_repository_storages_weighted_to_application_settings.rb +++ b/db/migrate/20200508203901_add_repository_storages_weighted_to_application_settings.rb @@ -3,11 +3,37 @@ class AddRepositoryStoragesWeightedToApplicationSettings < ActiveRecord::Migration[6.0] DOWNTIME = false + class ApplicationSetting < ActiveRecord::Base + serialize :repository_storages + self.table_name = 'application_settings' + end + def up add_column :application_settings, :repository_storages_weighted, :jsonb, default: {}, null: false + + seed_repository_storages_weighted end def down remove_column :application_settings, :repository_storages_weighted end + + private + + def seed_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| + 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 diff --git a/db/post_migrate/20200526000407_seed_repository_storages_weighted.rb b/db/post_migrate/20200526000407_seed_repository_storages_weighted.rb index e5a0acb9cd8b987338a3df6f226eee32cf25262e..979f16e75edcdf7215dbc27c88aef71286c45398 100644 --- a/db/post_migrate/20200526000407_seed_repository_storages_weighted.rb +++ b/db/post_migrate/20200526000407_seed_repository_storages_weighted.rb @@ -1,16 +1,23 @@ # frozen_string_literal: true class SeedRepositoryStoragesWeighted < ActiveRecord::Migration[6.0] + DOWNTIME = false + class ApplicationSetting < ActiveRecord::Base serialize :repository_storages self.table_name = 'application_settings' end def up + # 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| storages = Gitlab.config.repositories.storages.keys.collect do |storage| weight = settings.repository_storages.include?(storage) ? 100 : 0 - [storage, weight] + [storage.to_sym, weight] end settings.repository_storages_weighted = Hash[storages] diff --git a/spec/migrations/add_repository_storages_weighted_to_application_settings_spec.rb b/spec/migrations/add_repository_storages_weighted_to_application_settings_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..6c6c63d8614274cc54ed22a77196aaed84b4a662 --- /dev/null +++ b/spec/migrations/add_repository_storages_weighted_to_application_settings_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'spec_helper' +require Rails.root.join('db', 'migrate', '20200508203901_add_repository_storages_weighted_to_application_settings.rb') + +RSpec.describe AddRepositoryStoragesWeightedToApplicationSettings, :migration 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(:application_setting) { application_settings.create! } + let(:repository_storages) { ["foo"] } + + it 'populates repository_storages_weighted properly' do + application_setting.repository_storages = repository_storages + application_setting.save! + + migrate! + + expect(application_settings.find(application_setting.id).repository_storages_weighted).to eq({ "foo" => 100, "baz" => 0 }) + end +end