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

swap_todos_note_id_to_bigint_for_self_managed_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 525e4fbcd8d7458e42b30904fc8303e96dbe4d4a (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
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: true

require 'spec_helper'
require_migration!

RSpec.describe SwapTodosNoteIdToBigintForSelfManaged, feature_category: :database do
  describe '#up' do
    context 'when GitLab.com, dev, or test' do
      before do
        # As we call `schema_migrate_down!` before each example, and for this migration
        # `#down` is same as `#up`, we need to ensure we start from the expected state.
        connection = described_class.new.connection
        connection.execute('ALTER TABLE todos ALTER COLUMN note_id TYPE bigint')
        connection.execute('ALTER TABLE todos DROP COLUMN IF EXISTS note_id_convert_to_bigint')
      end

      it 'does not swap the columns' do
        # rubocop: disable RSpec/AnyInstanceOf
        allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(true)
        # rubocop: enable RSpec/AnyInstanceOf

        todos = table(:todos)

        disable_migrations_output do
          reversible_migration do |migration|
            migration.before -> {
              todos.reset_column_information

              expect(todos.columns.find { |c| c.name == 'note_id' }.sql_type).to eq('bigint')
              expect(todos.columns.find { |c| c.name == 'note_id_convert_to_bigint' }).to be nil
            }

            migration.after -> {
              todos.reset_column_information

              expect(todos.columns.find { |c| c.name == 'note_id' }.sql_type).to eq('bigint')
              expect(todos.columns.find { |c| c.name == 'note_id_convert_to_bigint' }).to be nil
            }
          end
        end
      end
    end

    context 'when self-managed instance with the columns already swapped' do
      before do
        # As we call `schema_migrate_down!` before each example, and for this migration
        # `#down` is same as `#up`, we need to ensure we start from the expected state.
        connection = described_class.new.connection
        connection.execute('ALTER TABLE todos ALTER COLUMN note_id TYPE bigint')
        connection.execute('ALTER TABLE todos ADD COLUMN IF NOT EXISTS note_id_convert_to_bigint integer')
      end

      after do
        connection = described_class.new.connection
        connection.execute('ALTER TABLE todos DROP COLUMN IF EXISTS note_id_convert_to_bigint')
      end

      it 'does not swap the columns' do
        # rubocop: disable RSpec/AnyInstanceOf
        allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false)
        # rubocop: enable RSpec/AnyInstanceOf

        todos = table(:todos)

        migrate!

        expect(todos.columns.find { |c| c.name == 'note_id' }.sql_type).to eq('bigint')
        expect(todos.columns.find do |c|
          c.name == 'note_id_convert_to_bigint'
        end.sql_type).to eq('integer')
      end
    end

    context 'when self-managed instance with the `note_id_convert_to_bigint` column already dropped ' do
      before do
        # As we call `schema_migrate_down!` before each example, and for this migration
        # `#down` is same as `#up`, we need to ensure we start from the expected state.
        connection = described_class.new.connection
        connection.execute('ALTER TABLE todos ALTER COLUMN note_id TYPE bigint')
        connection.execute('ALTER TABLE todos DROP COLUMN IF EXISTS note_id_convert_to_bigint')
      end

      it 'does not swap the columns' do
        # rubocop: disable RSpec/AnyInstanceOf
        allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false)
        # rubocop: enable RSpec/AnyInstanceOf

        todos = table(:todos)

        disable_migrations_output do
          reversible_migration do |migration|
            migration.before -> {
              todos.reset_column_information

              expect(todos.columns.find { |c| c.name == 'note_id' }.sql_type).to eq('bigint')
              expect(todos.columns.find { |c| c.name == 'note_id_convert_to_bigint' }).to be nil
            }

            migration.after -> {
              todos.reset_column_information

              expect(todos.columns.find { |c| c.name == 'note_id' }.sql_type).to eq('bigint')
              expect(todos.columns.find { |c| c.name == 'note_id_convert_to_bigint' }).to be nil
            }
          end
        end
      end
    end

    context 'when self-managed instance' do
      before do
        # As we call `schema_migrate_down!` before each example, and for this migration
        # `#down` is same as `#up`, we need to ensure we start from the expected state.
        connection = described_class.new.connection
        connection.execute('ALTER TABLE todos ALTER COLUMN note_id TYPE integer')
        connection.execute('ALTER TABLE todos ADD COLUMN IF NOT EXISTS note_id_convert_to_bigint bigint')
        connection.execute('ALTER TABLE todos ALTER COLUMN note_id_convert_to_bigint TYPE bigint')
        connection.execute('DROP INDEX IF EXISTS index_todos_on_note_id_convert_to_bigint')
        connection.execute('CREATE OR REPLACE FUNCTION trigger_dca935e3a712() RETURNS trigger LANGUAGE plpgsql AS $$
          BEGIN NEW."note_id_convert_to_bigint" := NEW."note_id"; RETURN NEW; END; $$;')
      end

      after do
        connection = described_class.new.connection
        connection.execute('ALTER TABLE todos DROP COLUMN IF EXISTS note_id_convert_to_bigint')
      end

      it 'swaps the columns' do
        # rubocop: disable RSpec/AnyInstanceOf
        allow_any_instance_of(described_class).to receive(:com_or_dev_or_test_but_not_jh?).and_return(false)
        # rubocop: enable RSpec/AnyInstanceOf

        todos = table(:todos)

        disable_migrations_output do
          reversible_migration do |migration|
            migration.before -> {
              todos.reset_column_information

              expect(todos.columns.find { |c| c.name == 'note_id' }.sql_type).to eq('integer')
              expect(todos.columns.find do |c|
                       c.name == 'note_id_convert_to_bigint'
                     end.sql_type).to eq('bigint')
            }

            migration.after -> {
              todos.reset_column_information

              expect(todos.columns.find { |c| c.name == 'note_id' }.sql_type).to eq('bigint')
              expect(todos.columns.find do |c|
                       c.name == 'note_id_convert_to_bigint'
                     end.sql_type).to eq('integer')
            }
          end
        end
      end
    end
  end
end