Commit d5dead4f authored by Dylan Griffith's avatar Dylan Griffith

Add rake task gitlab:elastic:list_pending_migrations

This will help people diagnose if there are pending migrations before
doing upgrades that might cause backwards compatibility issues.
parent 9939a5f9
......@@ -416,6 +416,7 @@ The following are some available Rake tasks:
| [`sudo gitlab-rake gitlab:elastic:projects_not_indexed`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake) | Displays which projects are not indexed. |
| [`sudo gitlab-rake gitlab:elastic:reindex_cluster`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake) | Schedules a zero-downtime cluster reindexing task. This feature should be used with an index that was created after GitLab 13.0. |
| [`sudo gitlab-rake gitlab:elastic:mark_reindex_failed`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)`] | Mark the most recent re-index job as failed. |
| [`sudo gitlab-rake gitlab:elastic:list_pending_migrations`](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/tasks/gitlab/elastic.rake)`] | List pending migrations. Pending migrations include those that have not yet started, have started but not finished and those that are halted. |
### Environment variables
......
......@@ -61,6 +61,12 @@ module Elastic
end
end
def pending_migrations
migrations.select do |migration|
!migration_has_finished?(migration.name_for_key)
end
end
def halted_migrations?
migrations.reverse.any? do |migration|
migration_halted?(migration)
......
---
title: Add rake task to check pending Elasticsearch migrations
merge_request: 54656
author:
type: added
......@@ -149,6 +149,20 @@ namespace :gitlab do
end
end
desc "GitLab | Elasticsearch | List pending migrations"
task list_pending_migrations: :environment do
pending_migrations = ::Elastic::DataMigrationService.pending_migrations
if pending_migrations.any?
puts 'Pending migrations:'
pending_migrations.each do |migration|
puts migration.name
end
else
puts 'There are no pending migrations.'
end
end
def project_id_batches(&blk)
relation = Project.all
......
......@@ -188,4 +188,38 @@ RSpec.describe Elastic::DataMigrationService, :elastic do
expect(subject.halted_migration).to eq(migration)
end
end
describe 'pending_migrations?' do
context 'when there is a pending migration' do
let(:migration) { subject.migrations.first }
before do
migration.save!(completed: false)
end
it 'returns true' do
expect(subject.pending_migrations?).to eq(true)
end
end
context 'when there is no pending migration' do
it 'returns false' do
expect(subject.pending_migrations?).to eq(false)
end
end
end
describe 'pending_migrations' do
let(:pending_migration1) { subject.migrations[1] }
let(:pending_migration2) { subject.migrations[2] }
before do
pending_migration1.save!(completed: false)
pending_migration2.save!(completed: false)
end
it 'returns only pending migrations' do
expect(subject.pending_migrations.map(&:name)).to eq([pending_migration1, pending_migration2].map(&:name))
end
end
end
......@@ -207,4 +207,28 @@ RSpec.describe 'gitlab:elastic namespace rake tasks', :elastic do
end
end
end
describe 'list_pending_migrations' do
subject { run_rake_task('gitlab:elastic:list_pending_migrations') }
context 'when there are pending migrations' do
let(:pending_migration1) { ::Elastic::DataMigrationService.migrations[1] }
let(:pending_migration2) { ::Elastic::DataMigrationService.migrations[2] }
before do
pending_migration1.save!(completed: false)
pending_migration2.save!(completed: false)
end
it 'outputs pending migrations' do
expect { subject }.to output(/#{pending_migration1.name}\n#{pending_migration2.name}/).to_stdout
end
end
context 'when there is no pending migrations' do
it 'outputs message there are no pending migrations' do
expect { subject }.to output(/There are no pending migrations./).to_stdout
end
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