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

20230908033511_swap_columns_for_ci_pipeline_chat_data_pipeline_id_bigint_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 082bbeb9060899215c6d7c553a6c8744a924ee07 (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
# frozen_string_literal: true

require 'spec_helper'
require_migration!

RSpec.describe SwapColumnsForCiPipelineChatDataPipelineIdBigint, feature_category: :continuous_integration do
  let(:connection) { active_record_base.connection }
  let(:table_ci_pipeline_chat_data) { table(:ci_pipeline_chat_data) }

  before do
    connection.execute('ALTER TABLE ci_pipeline_chat_data ALTER COLUMN pipeline_id TYPE integer')
    connection.execute('ALTER TABLE ci_pipeline_chat_data ALTER COLUMN pipeline_id_convert_to_bigint TYPE bigint')
  end

  it 'swaps columns' do
    disable_migrations_output do
      reversible_migration do |migration|
        migration.before -> {
          expect(column('pipeline_id').sql_type).to eq('integer')
          expect(column('pipeline_id_convert_to_bigint').sql_type).to eq('bigint')
        }

        migration.after -> {
          expect(column('pipeline_id').sql_type).to eq('bigint')
          expect(column('pipeline_id_convert_to_bigint').sql_type).to eq('integer')
        }
      end
    end
  end

  context 'when legacy foreign key exists' do
    before do
      if connection.foreign_key_exists?(
        :ci_pipeline_chat_data, name: :fk_64ebfab6b3)
        connection.remove_foreign_key(:ci_pipeline_chat_data, :ci_pipelines,
          name: :fk_64ebfab6b3)
      end

      connection.add_foreign_key(:ci_pipeline_chat_data, :ci_pipelines, column: :pipeline_id,
        name: :fk_rails_64ebfab6b3)
    end

    it 'renames the legacy foreign key fk_rails_64ebfab6b3' do
      expect(connection.foreign_key_exists?(:ci_pipeline_chat_data, name: :fk_rails_64ebfab6b3)).to be_truthy
      expect(connection.foreign_key_exists?(:ci_pipeline_chat_data, name: :fk_64ebfab6b3)).to be_falsy

      disable_migrations_output do
        reversible_migration do |migration|
          migration.before -> {
            expect(column('pipeline_id').sql_type).to eq('integer')
            expect(column('pipeline_id_convert_to_bigint').sql_type).to eq('bigint')
          }

          migration.after -> {
            expect(column('pipeline_id').sql_type).to eq('bigint')
            expect(column('pipeline_id_convert_to_bigint').sql_type).to eq('integer')

            expect(connection.foreign_key_exists?(:ci_pipeline_chat_data, name: :fk_rails_64ebfab6b3)).to be_falsy
            expect(connection.foreign_key_exists?(:ci_pipeline_chat_data, name: :fk_64ebfab6b3)).to be_truthy
          }
        end
      end
    end
  end

  private

  def column(name)
    table_ci_pipeline_chat_data.reset_column_information
    table_ci_pipeline_chat_data.columns.find { |c| c.name == name.to_s }
  end
end