blob: df653c853b9b3785581c2bb836cb9b4af091b5f1 (
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
|
# frozen_string_literal: true
module Ci
module PartitioningTesting
module SchemaHelpers
module_function
def with_routing_tables
previous_table_name = CommitStatus.table_name
CommitStatus.table_name = :p_ci_builds
CommitStatus.descendants.each(&:reset_table_name)
yield
ensure
CommitStatus.table_name = previous_table_name
CommitStatus.descendants.each(&:reset_table_name)
end
def setup(connection: Ci::ApplicationRecord.connection)
each_partitionable_table do |table_name|
create_test_partition("p_#{table_name}", connection: connection)
end
ensure_builds_id_uniquness(connection: connection)
end
def teardown(connection: Ci::ApplicationRecord.connection)
each_partitionable_table do |table_name|
drop_test_partition("p_#{table_name}", connection: connection)
end
end
def each_partitionable_table
::Ci::Partitionable::Testing::PARTITIONABLE_MODELS.each do |klass|
model = klass.safe_constantize
table_name = model.table_name.delete_prefix('p_')
yield(table_name)
model.reset_column_information if model.connected?
end
end
def create_test_partition(table_name, connection:)
return unless table_available?(table_name, connection: connection)
drop_test_partition(table_name, connection: connection)
connection.execute(<<~SQL.squish)
CREATE TABLE #{full_partition_name(table_name)}
PARTITION OF #{table_name}
FOR VALUES IN (#{PartitioningTesting::PartitionIdentifiers.ci_testing_partition_id});
SQL
end
def drop_test_partition(table_name, connection:)
return unless table_available?(table_name, connection: connection)
return unless connection.table_exists?(full_partition_name(table_name))
connection.execute(<<~SQL.squish)
ALTER TABLE #{table_name} DETACH PARTITION #{full_partition_name(table_name)};
DROP TABLE IF EXISTS #{full_partition_name(table_name)};
SQL
end
# This can be removed after https://gitlab.com/gitlab-org/gitlab/-/issues/421173
# is implemented
def ensure_builds_id_uniquness(connection:)
connection.execute(<<~SQL.squish)
CREATE TRIGGER assign_p_ci_builds_id_trigger
BEFORE INSERT ON #{full_partition_name('ci_builds')}
FOR EACH ROW EXECUTE FUNCTION assign_p_ci_builds_id_value();
SQL
end
def table_available?(table_name, connection:)
connection.table_exists?(table_name) &&
connection.column_exists?(table_name, :partition_id)
end
def full_partition_name(table_name)
[
Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA,
'._test_gitlab_',
table_name.delete_prefix('p_'),
'_partition'
].join('')
end
end
end
end
|