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

require 'spec_helper'

RSpec.describe Gitlab::Database::Partitioning::ReplaceTable, '#perform' do
  include TableSchemaHelpers

  subject(:replace_table) { described_class.new(original_table, replacement_table, archived_table, 'id').perform }

  let(:original_table) { '_test_original_table' }
  let(:replacement_table) { '_test_replacement_table' }
  let(:archived_table) { '_test_archived_table' }

  let(:original_sequence) { "#{original_table}_id_seq" }

  let(:original_primary_key) { "#{original_table}_pkey" }
  let(:replacement_primary_key) { "#{replacement_table}_pkey" }
  let(:archived_primary_key) { "#{archived_table}_pkey" }

  before do
    connection.execute(<<~SQL)
      CREATE TABLE #{original_table} (
        id serial NOT NULL PRIMARY KEY,
        original_column text NOT NULL,
        created_at timestamptz NOT NULL);

      CREATE TABLE #{replacement_table} (
        id int NOT NULL,
        replacement_column text NOT NULL,
        created_at timestamptz NOT NULL,
        PRIMARY KEY (id, created_at))
        PARTITION BY RANGE (created_at);
    SQL
  end

  it 'replaces the current table, archiving the old' do
    expect_table_to_be_replaced { replace_table }
  end

  it 'transfers the primary key sequence to the replacement table' do
    expect(sequence_owned_by(original_table, 'id')).to eq(original_sequence)
    expect(default_expression_for(original_table, 'id')).to eq("nextval('#{original_sequence}'::regclass)")

    expect(sequence_owned_by(replacement_table, 'id')).to be_nil
    expect(default_expression_for(replacement_table, 'id')).to be_nil

    expect_table_to_be_replaced { replace_table }

    expect(sequence_owned_by(original_table, 'id')).to eq(original_sequence)
    expect(default_expression_for(original_table, 'id')).to eq("nextval('#{original_sequence}'::regclass)")
    expect(sequence_owned_by(archived_table, 'id')).to be_nil
    expect(default_expression_for(archived_table, 'id')).to be_nil
  end

  it 'renames the primary key constraints to match the new table names' do
    expect_primary_keys_after_tables([original_table, replacement_table])

    expect_table_to_be_replaced { replace_table }

    expect_primary_keys_after_tables([original_table, archived_table])
  end

  context 'when the table has partitions' do
    before do
      connection.execute(<<~SQL)
        CREATE TABLE gitlab_partitions_dynamic.#{replacement_table}_202001 PARTITION OF #{replacement_table}
        FOR VALUES FROM ('2020-01-01') TO ('2020-02-01');

        CREATE TABLE gitlab_partitions_dynamic.#{replacement_table}_202002 PARTITION OF #{replacement_table}
        FOR VALUES FROM ('2020-02-01') TO ('2020-03-01');
      SQL
    end

    it 'renames the partitions to match the new table name' do
      expect(partitions_for_parent_table(original_table).count).to eq(0)
      expect(partitions_for_parent_table(replacement_table).count).to eq(2)

      expect_table_to_be_replaced { replace_table }

      expect(partitions_for_parent_table(archived_table).count).to eq(0)

      partitions = partitions_for_parent_table(original_table).all

      expect(partitions.size).to eq(2)

      expect(partitions[0]).to have_attributes(
        identifier: "gitlab_partitions_dynamic.#{original_table}_202001",
        condition: "FOR VALUES FROM ('2020-01-01 00:00:00+00') TO ('2020-02-01 00:00:00+00')")

      expect(partitions[1]).to have_attributes(
        identifier: "gitlab_partitions_dynamic.#{original_table}_202002",
        condition: "FOR VALUES FROM ('2020-02-01 00:00:00+00') TO ('2020-03-01 00:00:00+00')")
    end

    it 'renames the primary key constraints to match the new partition names' do
      original_partitions = ["#{replacement_table}_202001", "#{replacement_table}_202002"]
      expect_primary_keys_after_tables(original_partitions, schema: 'gitlab_partitions_dynamic')

      expect_table_to_be_replaced { replace_table }

      renamed_partitions = ["#{original_table}_202001", "#{original_table}_202002"]
      expect_primary_keys_after_tables(renamed_partitions, schema: 'gitlab_partitions_dynamic')
    end
  end

  def partitions_for_parent_table(table)
    Gitlab::Database::PostgresPartition.for_parent_table(table)
  end

  def expect_table_to_be_replaced(&block)
    super(original_table: original_table, replacement_table: replacement_table, archived_table: archived_table, &block)
  end
end