Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20231025025733_swap_columns_for_ci_pipelines_pipeline_id_bigint_for_self_host_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f7afee950b751303cb808e80e9c6aba489c50967 (plain)
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'
require_migration!

RSpec.describe SwapColumnsForCiPipelinesPipelineIdBigintForSelfHost, feature_category: :continuous_integration do
  let(:connection) { active_record_base.connection }

  before do
    connection.execute('ALTER TABLE ci_pipelines ALTER COLUMN auto_canceled_by_id TYPE integer')
    connection.execute('ALTER TABLE ci_pipelines ALTER COLUMN auto_canceled_by_id_convert_to_bigint TYPE bigint')
  end

  it_behaves_like(
    'swap conversion columns',
    table_name: :ci_pipelines,
    from: :auto_canceled_by_id,
    to: :auto_canceled_by_id_convert_to_bigint
  )

  context 'when foreign key names are different' do
    before do
      if connection.foreign_key_exists?(:ci_pipelines, name: :fk_262d4c2d19)
        connection.execute(
          'ALTER TABLE "ci_pipelines" RENAME CONSTRAINT "fk_262d4c2d19" TO "fk_4_auto_canceled_by_id"'
        )
      end

      if connection.foreign_key_exists?(:ci_pipelines, name: :fk_67e4288f3a)
        connection.execute(
          'ALTER TABLE "ci_pipelines" RENAME CONSTRAINT "fk_67e4288f3a" TO "fk_4_auto_canceled_by_id_convert_to_bigint"'
        )
      end
    end

    after do
      if connection.foreign_key_exists?(:ci_pipelines, name: :fk_4_auto_canceled_by_id)
        connection.execute(
          'ALTER TABLE "ci_pipelines" RENAME CONSTRAINT "fk_4_auto_canceled_by_id" TO "fk_262d4c2d19"'
        )
      end

      if connection.foreign_key_exists?(:ci_pipelines, name: :fk_4_auto_canceled_by_id_convert_to_bigint)
        connection.execute(
          'ALTER TABLE "ci_pipelines" RENAME CONSTRAINT "fk_4_auto_canceled_by_id_convert_to_bigint" TO "fk_67e4288f3a"'
        )
      end
    end

    it 'swaps the foreign key properly' do
      disable_migrations_output do
        recorder = ActiveRecord::QueryRecorder.new { migrate! }
        expect(recorder.log).to include(
          /RENAME CONSTRAINT "fk_4_auto_canceled_by_id_convert_to_bigint" TO "temp_name_for_renaming"/
        )
        expect(recorder.log).to include(
          /RENAME CONSTRAINT "fk_4_auto_canceled_by_id" TO "fk_4_auto_canceled_by_id_convert_to_bigint"/
        )
        expect(recorder.log).to include(/RENAME CONSTRAINT "temp_name_for_renaming" TO "fk_4_auto_canceled_by_id"/)
      end
    end
  end

  context 'when foreign key is missing' do
    before do
      if connection.foreign_key_exists?(:ci_pipelines, name: :fk_262d4c2d19)
        connection.remove_foreign_key(:ci_pipelines, name: :fk_262d4c2d19)
      end

      if connection.foreign_key_exists?(:ci_pipelines, name: :fk_4_auto_canceled_by_id)
        connection.remove_foreign_key(:ci_pipelines, name: :fk_4_auto_canceled_by_id)
      end
    end

    after do
      # Need to add the foreign key back or it will fail the other tests
      connection.add_foreign_key(
        :ci_pipelines, :ci_pipelines,
        name: :fk_262d4c2d19, column: :auto_canceled_by_id, on_delete: :nullify
      )
    end

    it 'raises error' do
      disable_migrations_output do
        expect { migrate! }.to raise_error(/Required foreign key for .* is missing./)
      end
    end
  end
end