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

20230801150214_retry_cleanup_bigint_conversion_for_events_for_gitlab_com_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0eac9f28fcd75586b28bd726c9f11356c4ae7b9b (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true

require 'spec_helper'

require_migration!

RSpec.describe RetryCleanupBigintConversionForEventsForGitlabCom, :migration, feature_category: :database do
  let(:migration) { described_class.new }
  let(:connection) { migration.connection }
  let(:column_name) { 'target_id_convert_to_bigint' }
  let(:events_table) { table(:events) }

  before do
    allow(migration).to receive(:should_run?).and_return(should_run?)
  end

  shared_examples 'skips the up migration' do
    it "doesn't calls cleanup_conversion_of_integer_to_bigint method" do
      disable_migrations_output do
        expect(migration).not_to receive(:cleanup_conversion_of_integer_to_bigint)

        migration.up
      end
    end
  end

  shared_examples 'skips the down migration' do
    it "doesn't calls restore_conversion_of_integer_to_bigint method" do
      disable_migrations_output do
        expect(migration).not_to receive(:restore_conversion_of_integer_to_bigint)

        migration.down
      end
    end
  end

  describe '#up' do
    context 'when column still exists' do
      before do
        # Ensures the correct state of db before the test
        connection.execute('ALTER TABLE events ADD COLUMN IF NOT EXISTS target_id_convert_to_bigint integer')
        connection.execute('CREATE OR REPLACE FUNCTION trigger_cd1aeb22b34a() RETURNS trigger LANGUAGE plpgsql AS $$
          BEGIN NEW."target_id_convert_to_bigint" := NEW."target_id"; RETURN NEW; END; $$;')
        connection.execute('DROP TRIGGER IF EXISTS trigger_cd1aeb22b34a ON events')
        connection.execute('CREATE TRIGGER trigger_cd1aeb22b34a BEFORE INSERT OR UPDATE ON events FOR EACH ROW EXECUTE
          FUNCTION trigger_cd1aeb22b34a()')
      end

      context 'when is GitLab.com, dev, or test' do
        let(:should_run?) { true }

        it 'drop the temporary columns' do
          disable_migrations_output do
            reversible_migration do |migration|
              migration.before -> {
                events_table.reset_column_information
                expect(events_table.columns.find { |c| c.name == 'target_id_convert_to_bigint' }).not_to be_nil
              }

              migration.after -> {
                events_table.reset_column_information
                expect(events_table.columns.find { |c| c.name == 'target_id_convert_to_bigint' }).to be_nil
              }
            end
          end
        end
      end

      context 'when is a self-managed instance' do
        let(:should_run?) { false }

        it_behaves_like 'skips the up migration'
      end
    end

    context 'when column not exists' do
      before do
        connection.execute('ALTER TABLE events DROP COLUMN IF EXISTS target_id_convert_to_bigint')
      end

      context 'when is GitLab.com, dev, or test' do
        let(:should_run?) { true }

        it_behaves_like 'skips the up migration'
      end

      context 'when is a self-managed instance' do
        let(:should_run?) { false }

        it_behaves_like 'skips the up migration'
      end
    end
  end

  describe '#down' do
    context 'when column still exists' do
      before do
        # Ensures the correct state of db before the test
        connection.execute('ALTER TABLE events ADD COLUMN IF NOT EXISTS target_id_convert_to_bigint integer')
        connection.execute('CREATE OR REPLACE FUNCTION trigger_cd1aeb22b34a() RETURNS trigger LANGUAGE plpgsql AS $$
          BEGIN NEW."target_id_convert_to_bigint" := NEW."target_id"; RETURN NEW; END; $$;')
        connection.execute('DROP TRIGGER IF EXISTS trigger_cd1aeb22b34a ON events')
        connection.execute('CREATE TRIGGER trigger_cd1aeb22b34a BEFORE INSERT OR UPDATE ON events FOR EACH ROW EXECUTE
          FUNCTION trigger_cd1aeb22b34a()')
      end

      context 'when is GitLab.com, dev, or test' do
        let(:should_run?) { true }

        it_behaves_like 'skips the down migration'
      end

      context 'when is a self-managed instance' do
        let(:should_run?) { false }

        it_behaves_like 'skips the down migration'
      end
    end

    context 'when column not exists' do
      before do
        connection.execute('ALTER TABLE events DROP COLUMN IF EXISTS target_id_convert_to_bigint')
      end

      context 'when is GitLab.com, dev, or test' do
        let(:should_run?) { true }

        it 'restore the temporary columns' do
          disable_migrations_output do
            migration.down

            column = events_table.columns.find { |c| c.name == 'target_id_convert_to_bigint' }

            expect(column).not_to be_nil
            expect(column.sql_type).to eq('integer')
          end
        end
      end

      context 'when is a self-managed instance' do
        let(:should_run?) { false }

        it_behaves_like 'skips the down migration'
      end
    end
  end
end