summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/migrations
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-02-20 13:49:51 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-02-20 13:49:51 +0000
commit71786ddc8e28fbd3cb3fcc4b3ff15e5962a1c82e (patch)
tree6a2d93ef3fb2d353bb7739e4b57e6541f51cdd71 /spec/lib/gitlab/database/migrations
parenta7253423e3403b8c08f8a161e5937e1488f5f407 (diff)
downloadgitlab-ce-71786ddc8e28fbd3cb3fcc4b3ff15e5962a1c82e.tar.gz
Add latest changes from gitlab-org/gitlab@15-9-stable-eev15.9.0-rc42
Diffstat (limited to 'spec/lib/gitlab/database/migrations')
-rw-r--r--spec/lib/gitlab/database/migrations/batched_migration_last_id_spec.rb2
-rw-r--r--spec/lib/gitlab/database/migrations/instrumentation_spec.rb11
-rw-r--r--spec/lib/gitlab/database/migrations/observers/batch_details_spec.rb42
-rw-r--r--spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb52
-rw-r--r--spec/lib/gitlab/database/migrations/timeout_helpers_spec.rb2
5 files changed, 79 insertions, 30 deletions
diff --git a/spec/lib/gitlab/database/migrations/batched_migration_last_id_spec.rb b/spec/lib/gitlab/database/migrations/batched_migration_last_id_spec.rb
index 97b432406eb..1365adb8993 100644
--- a/spec/lib/gitlab/database/migrations/batched_migration_last_id_spec.rb
+++ b/spec/lib/gitlab/database/migrations/batched_migration_last_id_spec.rb
@@ -2,7 +2,7 @@
require 'spec_helper'
-RSpec.describe Gitlab::Database::Migrations::BatchedMigrationLastId, feature_category: :pipeline_insights do
+RSpec.describe Gitlab::Database::Migrations::BatchedMigrationLastId, feature_category: :database do
subject(:test_sampling) { described_class.new(connection, base_dir) }
let(:base_dir) { Pathname.new(Dir.mktmpdir) }
diff --git a/spec/lib/gitlab/database/migrations/instrumentation_spec.rb b/spec/lib/gitlab/database/migrations/instrumentation_spec.rb
index b0bdbf5c371..4f347034c0b 100644
--- a/spec/lib/gitlab/database/migrations/instrumentation_spec.rb
+++ b/spec/lib/gitlab/database/migrations/instrumentation_spec.rb
@@ -18,6 +18,7 @@ RSpec.describe Gitlab::Database::Migrations::Instrumentation do
let(:migration_name) { 'test' }
let(:migration_version) { '12345' }
let(:migration_meta) { { 'max_batch_size' => 1, 'total_tuple_count' => 10, 'interval' => 60 } }
+ let(:expected_json_keys) { %w[version name walltime success total_database_size_change query_statistics] }
it 'executes the given block' do
expect do |b|
@@ -81,7 +82,10 @@ RSpec.describe Gitlab::Database::Migrations::Instrumentation do
expect(subject.success).to be_truthy
expect(subject.version).to eq(migration_version)
expect(subject.name).to eq(migration_name)
- expect(subject.meta).to eq(migration_meta)
+ end
+
+ it 'transforms observation to expected json' do
+ expect(Gitlab::Json.parse(subject.to_json).keys).to contain_exactly(*expected_json_keys)
end
end
@@ -114,7 +118,10 @@ RSpec.describe Gitlab::Database::Migrations::Instrumentation do
expect(subject['success']).to be_falsey
expect(subject['version']).to eq(migration_version)
expect(subject['name']).to eq(migration_name)
- expect(subject['meta']).to include(migration_meta)
+ end
+
+ it 'transforms observation to expected json' do
+ expect(Gitlab::Json.parse(subject.to_json).keys).to contain_exactly(*expected_json_keys)
end
end
end
diff --git a/spec/lib/gitlab/database/migrations/observers/batch_details_spec.rb b/spec/lib/gitlab/database/migrations/observers/batch_details_spec.rb
new file mode 100644
index 00000000000..5b3c23736ed
--- /dev/null
+++ b/spec/lib/gitlab/database/migrations/observers/batch_details_spec.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Database::Migrations::Observers::BatchDetails, feature_category: :database do
+ subject(:observe) { described_class.new(observation, path, connection) }
+
+ let(:connection) { ActiveRecord::Migration.connection }
+ let(:observation) { Gitlab::Database::Migrations::Observation.new(meta: meta) }
+ let(:path) { Dir.mktmpdir }
+ let(:file_name) { 'batch-details.json' }
+ let(:file_path) { Pathname.new(path).join(file_name) }
+ let(:json_file) { Gitlab::Json.parse(File.read(file_path)) }
+ let(:job_meta) do
+ { "min_value" => 1, "max_value" => 19, "batch_size" => 20, "sub_batch_size" => 5, "pause_ms" => 100 }
+ end
+
+ where(:meta, :expected_keys) do
+ [
+ [lazy { { job_meta: job_meta } }, %w[time_spent min_value max_value batch_size sub_batch_size pause_ms]],
+ [nil, %w[time_spent]],
+ [{ job_meta: nil }, %w[time_spent]]
+ ]
+ end
+
+ with_them do
+ before do
+ observe.before
+ observe.after
+ end
+
+ after do
+ FileUtils.remove_entry(path)
+ end
+
+ it 'records expected information to file' do
+ observe.record
+
+ expect(json_file.keys).to match_array(expected_keys)
+ end
+ end
+end
diff --git a/spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb b/spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb
index 0b048617ce1..57c5011590c 100644
--- a/spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb
+++ b/spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb
@@ -2,7 +2,7 @@
require 'spec_helper'
-RSpec.describe Gitlab::Database::Migrations::TestBatchedBackgroundRunner, :freeze_time do
+RSpec.describe Gitlab::Database::Migrations::TestBatchedBackgroundRunner, :freeze_time, feature_category: :database do
include Gitlab::Database::MigrationHelpers
include Database::MigrationTestingHelpers
@@ -45,18 +45,15 @@ RSpec.describe Gitlab::Database::Migrations::TestBatchedBackgroundRunner, :freez
end
with_them do
- let(:result_dir) { Dir.mktmpdir }
+ let(:result_dir) { Pathname.new(Dir.mktmpdir) }
+ let(:connection) { base_model.connection }
+ let(:table_name) { "_test_column_copying" }
+ let(:from_id) { 0 }
after do
FileUtils.rm_rf(result_dir)
end
- let(:connection) { base_model.connection }
-
- let(:table_name) { "_test_column_copying" }
-
- let(:from_id) { 0 }
-
before do
connection.execute(<<~SQL)
CREATE TABLE #{table_name} (
@@ -70,26 +67,15 @@ RSpec.describe Gitlab::Database::Migrations::TestBatchedBackgroundRunner, :freez
context 'running a real background migration' do
let(:interval) { 5.minutes }
- let(:meta) { { "max_batch_size" => nil, "total_tuple_count" => nil, "interval" => interval } }
-
- let(:params) do
- {
- version: nil,
- connection: connection,
- meta: {
- interval: 300,
- max_batch_size: nil,
- total_tuple_count: nil
- }
- }
- end
+ let(:params) { { version: nil, connection: connection } }
+ let(:migration_name) { 'CopyColumnUsingBackgroundMigrationJob' }
+ let(:migration_file_path) { result_dir.join('CopyColumnUsingBackgroundMigrationJob', 'details.json') }
+ let(:json_file) { Gitlab::Json.parse(File.read(migration_file_path)) }
+ let(:expected_file_keys) { %w[interval total_tuple_count max_batch_size] }
before do
- queue_migration('CopyColumnUsingBackgroundMigrationJob',
- table_name, :id,
- :id, :data,
- batch_size: 100,
- job_interval: interval) # job_interval is skipped when testing
+ # job_interval is skipped when testing
+ queue_migration(migration_name, table_name, :id, :id, :data, batch_size: 100, job_interval: interval)
end
subject(:sample_migration) do
@@ -113,6 +99,20 @@ RSpec.describe Gitlab::Database::Migrations::TestBatchedBackgroundRunner, :freez
subject
end
+
+ it 'uses the filtering clause from the migration' do
+ expect_next_instance_of(Gitlab::BackgroundMigration::BatchingStrategies::PrimaryKeyBatchingStrategy) do |s|
+ expect(s).to receive(:filter_batch).at_least(:once).and_call_original
+ end
+
+ subject
+ end
+
+ it 'exports migration details to a file' do
+ subject
+
+ expect(json_file.keys).to match_array(expected_file_keys)
+ end
end
context 'with jobs to run' do
diff --git a/spec/lib/gitlab/database/migrations/timeout_helpers_spec.rb b/spec/lib/gitlab/database/migrations/timeout_helpers_spec.rb
index d35211af680..ee63ea7174b 100644
--- a/spec/lib/gitlab/database/migrations/timeout_helpers_spec.rb
+++ b/spec/lib/gitlab/database/migrations/timeout_helpers_spec.rb
@@ -2,7 +2,7 @@
require 'spec_helper'
-RSpec.describe Gitlab::Database::Migrations::TimeoutHelpers do
+RSpec.describe Gitlab::Database::Migrations::TimeoutHelpers, feature_category: :database do
let(:model) do
ActiveRecord::Migration.new.extend(described_class)
end