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

require 'spec_helper'

RSpec.describe Gitlab::Database::CustomStructure do
  let_it_be(:structure) { described_class.new }
  let_it_be(:filepath) { Rails.root.join(described_class::CUSTOM_DUMP_FILE) }
  let_it_be(:file_header) do
    <<~DATA
      -- this file tracks custom GitLab data, such as foreign keys referencing partitioned tables
      -- more details can be found in the issue: https://gitlab.com/gitlab-org/gitlab/-/issues/201872
      SET search_path=public;
    DATA
  end

  let(:io) { StringIO.new }

  before do
    allow(File).to receive(:open).with(filepath, anything).and_yield(io)
  end

  context 'when there are no partitioned_foreign_keys' do
    it 'dumps a valid structure file' do
      structure.dump

      expect(io.string).to eq("#{file_header}\n")
    end
  end

  context 'when there are partitioned_foreign_keys' do
    let!(:first_fk) do
      Gitlab::Database::PartitioningMigrationHelpers::PartitionedForeignKey.create(
        cascade_delete: true, from_table: 'issues', from_column: 'project_id', to_table: 'projects', to_column: 'id')
    end

    let!(:second_fk) do
      Gitlab::Database::PartitioningMigrationHelpers::PartitionedForeignKey.create(
        cascade_delete: false, from_table: 'issues', from_column: 'moved_to_id', to_table: 'issues', to_column: 'id')
    end

    it 'dumps a file with the command to restore the current keys' do
      structure.dump

      expect(io.string).to eq(<<~DATA)
        #{file_header}
        COPY partitioned_foreign_keys (id, cascade_delete, from_table, from_column, to_table, to_column) FROM STDIN;
        #{first_fk.id}\ttrue\tissues\tproject_id\tprojects\tid
        #{second_fk.id}\tfalse\tissues\tmoved_to_id\tissues\tid
        \\.
      DATA

      first_fk.destroy
      io.truncate(0)
      io.rewind

      structure.dump

      expect(io.string).to eq(<<~DATA)
        #{file_header}
        COPY partitioned_foreign_keys (id, cascade_delete, from_table, from_column, to_table, to_column) FROM STDIN;
        #{second_fk.id}\tfalse\tissues\tmoved_to_id\tissues\tid
        \\.
      DATA
    end
  end
end