1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::Admin::Migrations, feature_category: :database do
let(:admin) { create(:admin) }
describe 'POST /admin/migrations/:version/mark' do
let(:database) { :main }
let(:params) { { database: database } }
let(:connection) { ApplicationRecord.connection }
let(:path) { "/admin/migrations/#{version}/mark" }
let(:version) { 1 }
subject(:mark) do
post api(path, admin, admin_mode: true), params: params
end
context 'when the migration exists' do
before do
double = instance_double(
Database::MarkMigrationService,
execute: ServiceResponse.success)
allow(Database::MarkMigrationService)
.to receive(:new)
.with(connection: connection, version: version)
.and_return(double)
end
it_behaves_like "POST request permissions for admin mode"
it 'marks the migration as successful' do
mark
expect(response).to have_gitlab_http_status(:created)
end
end
context 'when the migration does not exist' do
let(:version) { 123 }
it 'returns 404' do
mark
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when the migration was already executed' do
let(:version) { connection.migration_context.current_version }
it 'returns 422' do
mark
expect(response).to have_gitlab_http_status(:unprocessable_entity)
end
end
context 'when multiple database is enabled' do
let(:ci_model) { Ci::ApplicationRecord }
let(:database) { :ci }
before do
skip_if_multiple_databases_not_setup(:ci)
end
it 'uses the correct connection' do
expect(Database::MarkMigrationService)
.to receive(:new)
.with(connection: ci_model.connection, version: version)
.and_call_original
mark
end
context 'when the database name does not exist' do
let(:database) { :wrong_database }
it 'returns bad request', :aggregate_failures do
mark
expect(response).to have_gitlab_http_status(:bad_request)
expect(response.body).to include('database does not have a valid value')
end
end
end
end
end
|