Commit 3bb5a9ee authored by Małgorzata Ksionek's avatar Małgorzata Ksionek

Add migration to fix not consistent data

And add specs to the migration
parent 07bcc6be
# frozen_string_literal: true
class EnsureNamespaceSettingsCreation < ActiveRecord::Migration[5.2]
DOWNTIME = false
BATCH_SIZE = 10000
class Namespace < ActiveRecord::Base
include EachBatch
self.table_name = 'namespaces'
end
def up
ensure_data_migration
end
def down
# no-op
end
private
def ensure_data_migration
Namespace.each_batch(of: BATCH_SIZE) do |query|
missing_count = query.where("NOT EXISTS (SELECT 1 FROM namespace_settings WHERE namespace_settings.namespace_id=namespaces.id)").limit(1).size
if missing_count > 0
min, max = query.pluck("MIN(id), MAX(id)").flatten
# we expect low record count so inline execution is fine.
Gitlab::BackgroundMigration::BackfillNamespaceSettings.new.perform(min, max)
end
end
end
end
e17da7eebb6d054a711368369d2b4fa684e96344f845bb7c6b3c89a9b4c4e067
\ No newline at end of file
......@@ -22513,7 +22513,7 @@ ALTER INDEX product_analytics_events_experimental_pkey ATTACH PARTITION gitlab_p
ALTER INDEX product_analytics_events_experimental_pkey ATTACH PARTITION gitlab_partitions_static.product_analytics_events_experimental_63_pkey;
CREATE TRIGGER table_sync_trigger_ee39a25f9d AFTER INSERT OR DELETE OR UPDATE ON audit_events FOR EACH ROW EXECUTE PROCEDURE table_sync_function_2be879775d();
CREATE TRIGGER table_sync_trigger_ee39a25f9d AFTER INSERT OR DELETE OR UPDATE ON audit_events FOR EACH ROW EXECUTE FUNCTION table_sync_function_2be879775d();
ALTER TABLE ONLY chat_names
ADD CONSTRAINT fk_00797a2bf9 FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE;
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '/Users/gosia/gitlab-development-kit/gitlab/db/post_migrate/20201104124300_ensure_namespace_settings_creation.rb')
RSpec.describe EnsureNamespaceSettingsCreation do
context 'when there are namespaces without namespace settings' do
let(:namespaces) { table(:namespaces) }
let(:namespace_settings) { table(:namespace_settings) }
let!(:namespace) { namespaces.create!(name: 'gitlab', path: 'gitlab-org') }
it 'migrates namespaces without namespace_settings' do
expect { migrate! }.to change { namespace_settings.count }.from(0).to(1)
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