Commit e9f3d03d authored by Stan Hu's avatar Stan Hu

Merge branch '9032-geo-pruneorphanedgeoevents-is-running-on-a-secondary' into 'master'

Geo - Ensure DB is writable before pruning Geo orphaned events

Closes #9032

See merge request gitlab-org/gitlab-ee!13983
parents e35843f3 ad16bb58
......@@ -12,6 +12,7 @@ class SchedulePruneOrphanedGeoEvents < ActiveRecord::Migration[4.2]
def up
return unless Gitlab::Database.postgresql?
return if Gitlab::Database.read_only?
BackgroundMigrationWorker.perform_async('PruneOrphanedGeoEvents')
end
......
......@@ -56,9 +56,10 @@ module Gitlab
end
def perform(table_name = EVENT_TABLES.first)
deleted = prune_orphaned_rows(table_name)
return if Gitlab::Database.read_only?
table_name = next_table(table_name) if deleted.zero?
deleted_rows = prune_orphaned_rows(table_name)
table_name = next_table(table_name) if deleted_rows.zero?
BackgroundMigrationWorker.perform_in(RESCHEDULE_DELAY, self.class.name, table_name) if table_name
end
......
......@@ -48,6 +48,12 @@ describe Gitlab::BackgroundMigration::PruneOrphanedGeoEvents, :migration, :postg
tags_affected: 0)
end
it 'does nothing if the database is read-only' do
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
expect { background_migration.perform(event_table_name) }.not_to change { Geo::RepositoryUpdatedEvent.count }
end
it 'takes the first table if no table is specified' do
expect(subject).to receive(:prune_orphaned_rows).with(described_class::EVENT_TABLES.first).and_call_original
......
......@@ -4,8 +4,24 @@ require 'spec_helper'
require Rails.root.join('ee', 'db', 'post_migrate', '20180618193715_schedule_prune_orphaned_geo_events.rb')
describe SchedulePruneOrphanedGeoEvents, :migration, geo: false, schema: 20180615152524 do
describe '#up' do
it 'delegates work to Gitlab::BackgroundMigration::PruneOrphanedGeoEvents', :postgresql do
describe '#up', :postgresql do
it 'does nothing if it is not running on PostgreSQL' do
allow(Gitlab::Database).to receive(:postgresql?).and_return(false)
expect(BackgroundMigrationWorker).not_to receive(:perform_async).with('PruneOrphanedGeoEvents')
migrate!
end
it 'does nothing if the database is read-only' do
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
expect(BackgroundMigrationWorker).not_to receive(:perform_async).with('PruneOrphanedGeoEvents')
migrate!
end
it 'delegates work to Gitlab::BackgroundMigration::PruneOrphanedGeoEvents' do
expect(BackgroundMigrationWorker).to receive(:perform_async).with('PruneOrphanedGeoEvents')
migrate!
......
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