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

87 lines
2.6 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
require 'spec_helper'
2021-09-30 23:02:18 +05:30
RSpec.describe Gitlab::Database::SchemaMigrations::Migrations do
let(:connection) { ApplicationRecord.connection }
let(:context) { Gitlab::Database::SchemaMigrations::Context.new(connection) }
let(:migrations) { described_class.new(context) }
describe '#touch_all' do
2020-10-24 23:57:45 +05:30
let(:version1) { '20200123' }
let(:version2) { '20200410' }
let(:version3) { '20200602' }
let(:version4) { '20200809' }
2021-09-30 23:02:18 +05:30
2020-10-24 23:57:45 +05:30
let(:relative_schema_directory) { 'db/schema_migrations' }
it 'creates a file containing a checksum for each version with a matching migration' do
Dir.mktmpdir do |tmpdir|
schema_directory = Pathname.new(tmpdir).join(relative_schema_directory)
FileUtils.mkdir_p(schema_directory)
old_version_filepath = schema_directory.join('20200101')
FileUtils.touch(old_version_filepath)
expect(File.exist?(old_version_filepath)).to be(true)
2021-09-30 23:02:18 +05:30
allow(context).to receive(:schema_directory).and_return(schema_directory)
allow(context).to receive(:versions_to_create).and_return([version1, version2])
2020-10-24 23:57:45 +05:30
2021-09-30 23:02:18 +05:30
migrations.touch_all
2020-10-24 23:57:45 +05:30
expect(File.exist?(old_version_filepath)).to be(false)
2021-09-30 23:02:18 +05:30
2020-10-24 23:57:45 +05:30
[version1, version2].each do |version|
version_filepath = schema_directory.join(version)
expect(File.exist?(version_filepath)).to be(true)
hashed_value = Digest::SHA256.hexdigest(version)
expect(File.read(version_filepath)).to eq(hashed_value)
end
[version3, version4].each do |version|
version_filepath = schema_directory.join(version)
expect(File.exist?(version_filepath)).to be(false)
end
end
end
end
2021-09-30 23:02:18 +05:30
describe '#load_all' do
2020-10-24 23:57:45 +05:30
before do
2021-09-30 23:02:18 +05:30
allow(migrations).to receive(:version_filenames).and_return(filenames)
2020-10-24 23:57:45 +05:30
end
context 'when there are no version files' do
let(:filenames) { [] }
it 'does nothing' do
expect(connection).not_to receive(:quote_string)
expect(connection).not_to receive(:execute)
2021-09-30 23:02:18 +05:30
migrations.load_all
2020-10-24 23:57:45 +05:30
end
end
context 'when there are version files' do
let(:filenames) { %w[123 456 789] }
it 'inserts the missing versions into schema_migrations' do
filenames.each do |filename|
expect(connection).to receive(:quote_string).with(filename).and_return(filename)
end
expect(connection).to receive(:execute).with(<<~SQL)
INSERT INTO schema_migrations (version)
VALUES ('123'),('456'),('789')
ON CONFLICT DO NOTHING
SQL
2021-09-30 23:02:18 +05:30
migrations.load_all
2020-10-24 23:57:45 +05:30
end
end
end
end