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

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

require 'spec_helper'

require_migration!

RSpec.describe BumpDefaultPartitionIdValueForCiTables, :migration, feature_category: :continuous_integration do
  context 'when on sass' do
    before do
      allow(Gitlab).to receive(:com?).and_return(true)
    end

    it 'changes default values' do
      reversible_migration do |migration|
        migration.before -> {
          expect(default_values).not_to include(101)
        }

        migration.after -> {
          expect(default_values).to match_array([101])
        }
      end
    end

    context 'with tables already changed' do
      before do
        active_record_base.connection.execute(<<~SQL)
          ALTER TABLE ci_builds ALTER COLUMN partition_id SET DEFAULT 101
        SQL
      end

      after do
        schema_migrate_down!
      end

      let(:alter_query) do
        /ALTER TABLE "ci_builds" ALTER COLUMN "partition_id" SET DEFAULT 101/
      end

      it 'skips updating already changed tables' do
        recorder = ActiveRecord::QueryRecorder.new { migrate! }

        expect(recorder.log.any?(alter_query)).to be_falsey
        expect(default_values).to match_array([101])
      end
    end
  end

  context 'when self-managed' do
    before do
      allow(Gitlab).to receive(:com?).and_return(false)
    end

    it 'does not change default values' do
      reversible_migration do |migration|
        migration.before -> {
          expect(default_values).not_to include(101)
        }

        migration.after -> {
          expect(default_values).not_to include(101)
        }
      end
    end
  end

  def default_values
    values = described_class::TABLES.flat_map do |table_name, columns|
      active_record_base
        .connection
        .columns(table_name)
        .select { |column| columns.include?(column.name.to_sym) }
        .map { |column| column.default&.to_i }
    end

    values.uniq
  end
end