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

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

require 'spec_helper'

RSpec.describe 'gitlab:clickhouse', click_house: :without_migrations, feature_category: :database do
  include ClickHouseTestHelpers

  # We don't need to delete data since we don't modify Postgres data
  self.use_transactional_tests = false

  let(:migrations_base_dir) { 'click_house/migrations' }
  let(:migrations_dirname) { '' }
  let(:migrations_dir) { expand_fixture_path("#{migrations_base_dir}/#{migrations_dirname}") }
  let(:verbose) { nil }

  before(:all) do
    Rake.application.rake_require 'tasks/gitlab/click_house/migration'
  end

  before do
    stub_env('VERBOSE', verbose) if verbose
  end

  describe 'migrate' do
    subject(:migration) { run_rake_task('gitlab:clickhouse:migrate') }

    let(:target_version) { nil }

    around do |example|
      ClickHouse::MigrationSupport::Migrator.migrations_paths = [migrations_dir]

      example.run

      clear_consts(expand_fixture_path(migrations_base_dir))
    end

    before do
      stub_env('VERSION', target_version) if target_version
    end

    describe 'when creating a table' do
      let(:migrations_dirname) { 'plain_table_creation' }

      it 'creates a table' do
        expect { migration }.to change { active_schema_migrations_count }.from(0).to(1)
          .and output.to_stdout

        expect(describe_table('some')).to match({
          id: a_hash_including(type: 'UInt64'),
          date: a_hash_including(type: 'Date')
        })
      end

      context 'when VERBOSE is false' do
        let(:verbose) { 'false' }

        it 'does not write to stdout' do
          expect { migration }.not_to output.to_stdout

          expect(describe_table('some')).to match({
            id: a_hash_including(type: 'UInt64'),
            date: a_hash_including(type: 'Date')
          })
        end
      end
    end

    describe 'when dropping a table' do
      let(:migrations_dirname) { 'drop_table' }
      let(:target_version) { 2 }

      it 'drops table' do
        stub_env('VERSION', 1)
        run_rake_task('gitlab:clickhouse:migrate')

        expect(table_names).to include('some')

        stub_env('VERSION', target_version)
        migration
        expect(table_names).not_to include('some')
      end
    end

    describe 'with VERSION is invalid' do
      let(:migrations_dirname) { 'plain_table_creation' }
      let(:target_version) { 'invalid' }

      it { expect { migration }.to raise_error RuntimeError, 'Invalid format of target version: `VERSION=invalid`' }
    end
  end

  describe 'rollback' do
    subject(:migration) { run_rake_task('gitlab:clickhouse:rollback') }

    let(:schema_migration) { ClickHouse::MigrationSupport::SchemaMigration }

    around do |example|
      ClickHouse::MigrationSupport::Migrator.migrations_paths = [migrations_dir]
      migrate(nil, ClickHouse::MigrationSupport::MigrationContext.new(migrations_dir, schema_migration))

      example.run

      clear_consts(expand_fixture_path(migrations_base_dir))
    end

    context 'when migrating back all the way to 0' do
      let(:target_version) { 0 }

      context 'when down method is present' do
        let(:migrations_dirname) { 'table_creation_with_down_method' }

        it 'removes migration' do
          expect(table_names).to include('some')

          migration
          expect(table_names).not_to include('some')
        end
      end
    end
  end

  %w[gitlab:clickhouse:migrate].each do |task|
    context "when running #{task}" do
      it "does run gitlab:clickhouse:prepare_schema_migration_table before" do
        expect(Rake::Task['gitlab:clickhouse:prepare_schema_migration_table']).to receive(:execute).and_return(true)
        expect(Rake::Task[task]).to receive(:execute).and_return(true)

        Rake::Task['gitlab:clickhouse:prepare_schema_migration_table'].reenable
        run_rake_task(task)
      end
    end
  end
end