diff options
author | Jan Provaznik <jprovaznik@gitlab.com> | 2018-03-06 15:20:20 +0100 |
---|---|---|
committer | Jan Provaznik <jprovaznik@gitlab.com> | 2018-03-06 16:32:37 +0100 |
commit | 222f7634ca9b68a122a1a3feb684ab9a53b7acc1 (patch) | |
tree | 342d18a8dfe605864d725385a6cf7ebd4ae0293f | |
parent | fa31559a37bfe394dedf4078789f1540e01fc5c7 (diff) | |
download | gitlab-ce-jprovazn-migrate-missing-commit-counts.tar.gz |
Assure commit_count is set after migration cleanupjprovazn-migrate-missing-commit-counts
!17513 cleaned up migration which adds commit_count to
MergeRequestDiffs. It's also necessary to assure that there
are not any records with commit_count unset (in case sidekiq
queue was killed for example).
Related to #41698
-rw-r--r-- | db/migrate/20180306131009_assure_commits_count_for_merge_request_diff.rb | 27 | ||||
-rw-r--r-- | db/schema.rb | 2 | ||||
-rw-r--r-- | spec/migrations/assure_commits_count_for_merge_request_diff_spec.rb | 32 |
3 files changed, 60 insertions, 1 deletions
diff --git a/db/migrate/20180306131009_assure_commits_count_for_merge_request_diff.rb b/db/migrate/20180306131009_assure_commits_count_for_merge_request_diff.rb new file mode 100644 index 00000000000..9172146e3f9 --- /dev/null +++ b/db/migrate/20180306131009_assure_commits_count_for_merge_request_diff.rb @@ -0,0 +1,27 @@ +class AssureCommitsCountForMergeRequestDiff < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + class MergeRequestDiff < ActiveRecord::Base + self.table_name = 'merge_request_diffs' + + include ::EachBatch + end + + def up + # Background migration queue is cleaned up in + # db/migrate/20180304204842_clean_commits_count_migration.rb. + MergeRequestDiff.where(commits_count: nil).each_batch(of: 50) do |batch| + range = batch.pluck('MIN(id)', 'MAX(id)').first + + Gitlab::BackgroundMigration::AddMergeRequestDiffCommitsCount.new.perform(*range) + end + end + + def down + # noop + end +end diff --git a/db/schema.rb b/db/schema.rb index 0881a1af945..d4f3a088473 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180305144721) do +ActiveRecord::Schema.define(version: 20180306131009) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/spec/migrations/assure_commits_count_for_merge_request_diff_spec.rb b/spec/migrations/assure_commits_count_for_merge_request_diff_spec.rb new file mode 100644 index 00000000000..ea252b24cc6 --- /dev/null +++ b/spec/migrations/assure_commits_count_for_merge_request_diff_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' +require Rails.root.join('db', 'migrate', '20180306131009_assure_commits_count_for_merge_request_diff.rb') + +describe AssureCommitsCountForMergeRequestDiff, :migration, :sidekiq, :redis do + let(:migration) { spy('migration') } + + before do + allow(Gitlab::BackgroundMigration::AddMergeRequestDiffCommitsCount) + .to receive(:new).and_return(migration) + end + + context 'when there are still unmigrated commit_counts afterwards' do + let(:namespaces) { table('namespaces') } + let(:projects) { table('projects') } + let(:merge_requests) { table('merge_requests') } + let(:diffs) { table('merge_request_diffs') } + + before do + namespace = namespaces.create(name: 'foo', path: 'foo') + project = projects.create!(namespace_id: namespace.id) + merge_request = merge_requests.create!(source_branch: 'x', target_branch: 'y', target_project_id: project.id) + diffs.create!(commits_count: nil, merge_request_id: merge_request.id) + diffs.create!(commits_count: nil, merge_request_id: merge_request.id) + end + + it 'migrates commit_counts sequentially in batches' do + migrate! + + expect(migration).to have_received(:perform).once + end + end +end |