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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::Partitioning::List::ConvertTable, feature_category: :database do
include Gitlab::Database::DynamicModelHelpers
include Database::TableSchemaHelpers
include Database::InjectFailureHelpers
include_context 'with a table structure for converting a table to a list partition'
let(:converter) do
described_class.new(
migration_context: migration_context,
table_name: table_name,
partitioning_column: partitioning_column,
parent_table_name: parent_table_name,
zero_partition_value: partitioning_default,
lock_tables: lock_tables
)
end
describe "#prepare_for_partitioning" do
subject(:prepare) { converter.prepare_for_partitioning(async: async) }
let(:async) { false }
it 'adds a check constraint' do
expect { prepare }.to change {
Gitlab::Database::PostgresConstraint
.check_constraints
.by_table_identifier(table_identifier)
.count
}.from(0).to(1)
end
context 'when it fails to add constraint' do
before do
allow(migration_context).to receive(:add_check_constraint)
end
it 'raises UnableToPartition error' do
expect { prepare }
.to raise_error(described_class::UnableToPartition)
.and change {
Gitlab::Database::PostgresConstraint
.check_constraints
.by_table_identifier(table_identifier)
.count
}.by(0)
end
end
context 'when async' do
let(:async) { true }
it 'adds a NOT VALID check constraint' do
expect { prepare }.to change {
Gitlab::Database::PostgresConstraint
.check_constraints
.by_table_identifier(table_identifier)
.count
}.from(0).to(1)
constraint =
Gitlab::Database::PostgresConstraint
.check_constraints
.by_table_identifier(table_identifier)
.last
expect(constraint.definition).to end_with('NOT VALID')
end
it 'adds a PostgresAsyncConstraintValidation record' do
expect { prepare }.to change {
Gitlab::Database::AsyncConstraints::PostgresAsyncConstraintValidation.count
}.by(1)
record = Gitlab::Database::AsyncConstraints::PostgresAsyncConstraintValidation
.where(table_name: table_name).last
expect(record.name).to eq described_class::PARTITIONING_CONSTRAINT_NAME
expect(record).to be_check_constraint
end
context 'when constraint exists but is not valid' do
before do
converter.prepare_for_partitioning(async: true)
end
it 'validates the check constraint' do
expect { prepare }.to change {
Gitlab::Database::PostgresConstraint
.check_constraints
.by_table_identifier(table_identifier).first.constraint_valid?
}.from(false).to(true)
end
context 'when it fails to validate constraint' do
before do
allow(migration_context).to receive(:validate_check_constraint)
end
it 'raises UnableToPartition error' do
expect { prepare }
.to raise_error(described_class::UnableToPartition,
starting_with('Error validating partitioning constraint'))
.and change {
Gitlab::Database::PostgresConstraint
.check_constraints
.by_table_identifier(table_identifier)
.count
}.by(0)
end
end
end
context 'when constraint exists and is valid' do
before do
converter.prepare_for_partitioning(async: false)
end
it 'raises UnableToPartition error' do
expect(Gitlab::AppLogger).to receive(:info).with(starting_with('Nothing to do'))
prepare
end
end
end
end
describe '#revert_preparation_for_partitioning' do
before do
converter.prepare_for_partitioning
end
subject(:revert_prepare) { converter.revert_preparation_for_partitioning }
it 'removes a check constraint' do
expect { revert_prepare }.to change {
Gitlab::Database::PostgresConstraint
.check_constraints
.by_table_identifier("#{connection.current_schema}.#{table_name}")
.count
}.from(1).to(0)
end
end
describe "#partition" do
subject(:partition) { converter.partition }
let(:async) { false }
before do
converter.prepare_for_partitioning(async: async)
end
context 'when the primary key is incorrect' do
before do
connection.execute(<<~SQL)
alter table #{referencing_table_name} drop constraint fk_referencing; -- this depends on the primary key
alter table #{other_referencing_table_name} drop constraint fk_referencing_other; -- this does too
alter table #{table_name} drop constraint #{table_name}_pkey;
alter table #{table_name} add constraint #{table_name}_pkey PRIMARY KEY (id);
SQL
end
it 'throws a reasonable error message' do
expect { partition }.to raise_error(described_class::UnableToPartition, /#{partitioning_column}/)
end
end
context 'when there is not a supporting check constraint' do
before do
connection.execute(<<~SQL)
alter table #{table_name} drop constraint partitioning_constraint;
SQL
end
it 'throws a reasonable error message' do
expect { partition }.to raise_error(described_class::UnableToPartition, /is not ready for partitioning./)
end
end
context 'when supporting check constraint is not valid' do
let(:async) { true }
it 'throws a reasonable error message' do
expect { partition }.to raise_error(described_class::UnableToPartition, /is not ready for partitioning./)
end
end
it 'migrates the table to a partitioned table' do
fks_before = migration_context.foreign_keys(table_name)
partition
expect(Gitlab::Database::PostgresPartition.for_parent_table(parent_table_name).count).to eq(1)
expect(migration_context.foreign_keys(parent_table_name).map(&:options)).to match_array(fks_before.map(&:options))
connection.execute(<<~SQL)
insert into #{table_name} (referenced_id, other_referenced_id) select #{referenced_table_name}.id, #{other_referenced_table_name}.id from #{referenced_table_name}, #{other_referenced_table_name};
SQL
# Create a second partition
connection.execute(<<~SQL)
create table #{table_name}2 partition of #{parent_table_name} FOR VALUES IN (2)
SQL
parent_model.create!(partitioning_column => 2, :referenced_id => 1, :other_referenced_id => 1)
expect(parent_model.pluck(:id)).to match_array([1, 2, 3])
expect { referencing_model.create!(partitioning_column => 1, :ref_id => 1) }.not_to raise_error
end
context 'when the existing table is owned by a different user' do
before do
connection.execute(<<~SQL)
CREATE USER other_user SUPERUSER;
ALTER TABLE #{table_name} OWNER TO other_user;
SQL
end
let(:current_user) { model.connection.select_value('select current_user') }
it 'partitions without error' do
expect { partition }.not_to raise_error
end
end
context 'with locking tables' do
let(:lock_tables) { [table_name] }
it 'locks the table' do
recorder = ActiveRecord::QueryRecorder.new { partition }
expect(recorder.log).to include(/LOCK "_test_table_to_partition" IN ACCESS EXCLUSIVE MODE/)
end
end
context 'when an error occurs during the conversion' do
before do
# Set up the fault that we'd like to inject
fault.call
end
let(:old_fks) do
Gitlab::Database::PostgresForeignKey.by_referenced_table_identifier(table_identifier).not_inherited
end
let(:new_fks) do
Gitlab::Database::PostgresForeignKey.by_referenced_table_identifier(parent_table_identifier).not_inherited
end
context 'when partitioning fails the first time' do
where(:case_name, :fault) do
[
["creating parent table", lazy { fail_sql_matching(/CREATE/i) }],
["adding the first foreign key", lazy { fail_adding_fk(parent_table_name, referenced_table_name) }],
["adding the second foreign key", lazy { fail_adding_fk(parent_table_name, other_referenced_table_name) }],
["attaching table", lazy { fail_sql_matching(/ATTACH/i) }]
]
end
with_them do
it 'recovers from a fault', :aggregate_failures do
expect { converter.partition }.to raise_error(/fault/)
expect(Gitlab::Database::PostgresPartition.for_parent_table(parent_table_name).count).to eq(0)
expect { converter.partition }.not_to raise_error
expect(Gitlab::Database::PostgresPartition.for_parent_table(parent_table_name).count).to eq(1)
end
end
end
end
context 'when table has LFK triggers' do
before do
migration_context.track_record_deletions(table_name)
end
it 'moves the trigger on the parent table', :aggregate_failures do
expect(migration_context.has_loose_foreign_key?(table_name)).to be_truthy
expect { partition }.not_to raise_error
expect(migration_context.has_loose_foreign_key?(table_name)).to be_truthy
expect(migration_context.has_loose_foreign_key?(parent_table_name)).to be_truthy
end
context 'with locking tables' do
let(:lock_tables) { [table_name] }
it 'locks the table before dropping the triggers' do
recorder = ActiveRecord::QueryRecorder.new { partition }
lock_index = recorder.log.find_index do |log|
log.start_with?('LOCK "_test_table_to_partition" IN ACCESS EXCLUSIVE MODE')
end
trigger_index = recorder.log.find_index do |log|
log.start_with?('DROP TRIGGER IF EXISTS _test_table_to_partition_loose_fk_trigger')
end
expect(lock_index).to be_present
expect(trigger_index).to be_present
expect(lock_index).to be < trigger_index
end
end
end
end
describe '#revert_partitioning' do
before do
converter.prepare_for_partitioning
converter.partition
end
subject(:revert_conversion) { converter.revert_partitioning }
it 'detaches the partition' do
expect { revert_conversion }.to change {
Gitlab::Database::PostgresPartition
.for_parent_table(parent_table_name).count
}.from(1).to(0)
end
it 'does not drop the child partition' do
expect { revert_conversion }.not_to change { table_oid(table_name) }
end
it 'removes the parent table' do
expect { revert_conversion }.to change { table_oid(parent_table_name).present? }.from(true).to(false)
end
it 're-adds the check constraint' do
expect { revert_conversion }.to change {
Gitlab::Database::PostgresConstraint
.check_constraints
.by_table_identifier(table_identifier)
.count
}.by(1)
end
it 'moves sequences back to the original table' do
expect { revert_conversion }.to change { converter.send(:sequences_owned_by, table_name).count }.from(0)
.and change { converter.send(:sequences_owned_by, parent_table_name).count }.to(0)
end
context 'when table has LFK triggers' do
before do
migration_context.track_record_deletions(parent_table_name)
migration_context.track_record_deletions(table_name)
end
it 'restores the trigger on the partition', :aggregate_failures do
expect(migration_context.has_loose_foreign_key?(table_name)).to be_truthy
expect(migration_context.has_loose_foreign_key?(parent_table_name)).to be_truthy
expect { revert_conversion }.not_to raise_error
expect(migration_context.has_loose_foreign_key?(table_name)).to be_truthy
end
end
end
end
|