summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/obsolete_ignored_columns_spec.rb
blob: 8a35d8149ad8447bec9449250c2b655ede2d8f02 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::ObsoleteIgnoredColumns do
  before do
    stub_const('Testing', Module.new)
    stub_const('Testing::MyBase', Class.new(ActiveRecord::Base))
    stub_const('SomeAbstract', Class.new(Testing::MyBase))
    stub_const('Testing::B', Class.new(Testing::MyBase))
    stub_const('Testing::A', Class.new(SomeAbstract))
    stub_const('Testing::C', Class.new(Testing::MyBase))

    # Used a fixed date to prevent tests failing across date boundaries
    stub_const('REMOVE_DATE', Date.new(2019, 12, 16))

    Testing.module_eval do
      Testing::MyBase.class_eval do
      end

      SomeAbstract.class_eval do
        include IgnorableColumns

        self.abstract_class = true

        self.table_name = 'projects'

        ignore_column :unused, remove_after: '2019-01-01', remove_with: '12.0'
      end

      Testing::B.class_eval do
        include IgnorableColumns

        self.table_name = 'issues'

        ignore_column :id, :other, remove_after: '2019-01-01', remove_with: '12.0'
        ignore_column :not_used_but_still_ignored, remove_after: REMOVE_DATE.to_s, remove_with: '12.1'
      end

      Testing::A.class_eval do
        ignore_column :also_unused, remove_after: '2019-02-01', remove_with: '12.1'
        ignore_column :not_used_but_still_ignored, remove_after: REMOVE_DATE.to_s, remove_with: '12.1'
      end

      Testing::C.class_eval do
        self.table_name = 'users'
      end
    end
  end

  subject { described_class.new(Testing::MyBase) }

  describe '#execute' do
    it 'returns a list of class names and columns pairs' do
      travel_to(REMOVE_DATE) do
        expect(subject.execute).to eq([
          ['Testing::A', {
            'unused' => IgnorableColumns::ColumnIgnore.new(Date.parse('2019-01-01'), '12.0'),
            'also_unused' => IgnorableColumns::ColumnIgnore.new(Date.parse('2019-02-01'), '12.1')
          }],
          ['Testing::B', {
            'other' => IgnorableColumns::ColumnIgnore.new(Date.parse('2019-01-01'), '12.0')
          }]
        ])
      end
    end
  end
end