debian-mirror-gitlab/spec/lib/gitlab/database/schema_migrations/context_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.7 KiB
Ruby
Raw Normal View History

2021-09-30 23:02:18 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::SchemaMigrations::Context do
2021-10-27 15:23:28 +05:30
let(:connection_class) { ActiveRecord::Base }
let(:connection) { connection_class.connection }
2021-09-30 23:02:18 +05:30
let(:context) { described_class.new(connection) }
describe '#schema_directory' do
it 'returns db/schema_migrations' do
2021-11-11 11:23:49 +05:30
expect(context.schema_directory).to eq(File.join(Rails.root, described_class.default_schema_migrations_path))
2021-09-30 23:02:18 +05:30
end
2021-10-27 15:23:28 +05:30
context 'CI database' do
2021-12-11 22:18:48 +05:30
let(:connection_class) { Ci::ApplicationRecord }
2021-09-30 23:02:18 +05:30
it 'returns a directory path that is database specific' do
2023-04-23 21:23:45 +05:30
skip_if_multiple_databases_not_setup(:ci)
2021-09-30 23:02:18 +05:30
2021-11-11 11:23:49 +05:30
expect(context.schema_directory).to eq(File.join(Rails.root, described_class.default_schema_migrations_path))
2021-10-27 15:23:28 +05:30
end
end
2021-11-18 22:05:49 +05:30
context 'multiple databases', :reestablished_active_record_base do
2021-10-27 15:23:28 +05:30
before do
connection_class.establish_connection(
ActiveRecord::Base
.connection_pool
.db_config
.configuration_hash
.merge(configuration_overrides)
)
end
context 'when `schema_migrations_path` is configured as string' do
let(:configuration_overrides) do
{ "schema_migrations_path" => "db/ci_schema_migrations" }
end
it 'returns a configured directory path that' do
2023-04-23 21:23:45 +05:30
skip_if_multiple_databases_not_setup(:ci)
2021-10-27 15:23:28 +05:30
expect(context.schema_directory).to eq(File.join(Rails.root, 'db/ci_schema_migrations'))
end
end
context 'when `schema_migrations_path` is configured as symbol' do
let(:configuration_overrides) do
{ schema_migrations_path: "db/ci_schema_migrations" }
end
it 'returns a configured directory path that' do
2023-04-23 21:23:45 +05:30
skip_if_multiple_databases_not_setup(:ci)
2021-10-27 15:23:28 +05:30
expect(context.schema_directory).to eq(File.join(Rails.root, 'db/ci_schema_migrations'))
end
2021-09-30 23:02:18 +05:30
end
end
end
describe '#versions_to_create' do
before do
allow(connection).to receive_message_chain(:schema_migration, :all_versions).and_return(migrated_versions)
migrations_struct = Struct.new(:version)
migrations = file_versions.map { |version| migrations_struct.new(version) }
allow(connection).to receive_message_chain(:migration_context, :migrations).and_return(migrations)
end
let(:version1) { '20200123' }
let(:version2) { '20200410' }
let(:version3) { '20200602' }
let(:version4) { '20200809' }
let(:migrated_versions) { file_versions }
let(:file_versions) { [version1, version2, version3, version4] }
context 'migrated versions is the same as migration file versions' do
it 'returns migrated versions' do
expect(context.versions_to_create).to eq(migrated_versions)
end
end
context 'migrated versions is subset of migration file versions' do
let(:migrated_versions) { [version1, version2] }
it 'returns migrated versions' do
expect(context.versions_to_create).to eq(migrated_versions)
end
end
context 'migrated versions is superset of migration file versions' do
let(:migrated_versions) { file_versions + ['20210809'] }
it 'returns file versions' do
expect(context.versions_to_create).to eq(file_versions)
end
end
context 'migrated versions has slightly different versions to migration file versions' do
let(:migrated_versions) { [version1, version2, version3, version4, '20210101'] }
let(:file_versions) { [version1, version2, version3, version4, '20210102'] }
it 'returns the common set' do
expect(context.versions_to_create).to eq([version1, version2, version3, version4])
end
end
end
end