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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::Partitionable::Switch, :aggregate_failures, feature_category: :continuous_integration do
let(:model) do
Class.new(Ci::ApplicationRecord) do
self.primary_key = :id
self.table_name = :_test_ci_jobs_metadata
self.sequence_name = :_test_ci_jobs_metadata_id_seq
def self.name
'TestSwitchJobMetadata'
end
end
end
let(:table_rollout_flag) { :ci_partitioning_use_test_routing_table }
let(:partitioned_model) { model::Partitioned }
let(:jobs_model) do
Class.new(Ci::ApplicationRecord) do
self.primary_key = :id
self.table_name = :_test_ci_jobs
def self.name
'TestSwitchJob'
end
end
end
before do
create_tables(<<~SQL)
CREATE TABLE _test_ci_jobs_metadata(
id serial NOT NULL PRIMARY KEY,
job_id int,
partition_id int NOT NULL DEFAULT 1,
type text,
expanded_environment_name text);
CREATE TABLE _test_p_ci_jobs_metadata (
LIKE _test_ci_jobs_metadata INCLUDING DEFAULTS
) PARTITION BY LIST(partition_id);
ALTER TABLE _test_p_ci_jobs_metadata
ADD CONSTRAINT _test_p_ci_jobs_metadata_id_partition_id
UNIQUE (id, partition_id);
ALTER TABLE _test_p_ci_jobs_metadata
ATTACH PARTITION _test_ci_jobs_metadata FOR VALUES IN (1);
CREATE TABLE _test_ci_jobs(id serial NOT NULL PRIMARY KEY);
SQL
stub_const('Ci::Partitionable::Testing::PARTITIONABLE_MODELS', [model.name])
model.include(Ci::Partitionable)
model.partitionable scope: ->(r) { 1 },
through: { table: :_test_p_ci_jobs_metadata, flag: table_rollout_flag }
model.belongs_to :job, anonymous_class: jobs_model
jobs_model.has_one :metadata,
anonymous_class: model,
foreign_key: :job_id, inverse_of: :job,
dependent: :destroy
allow(Feature::Definition).to receive(:get).and_call_original
allow(Feature::Definition).to receive(:get).with(table_rollout_flag)
.and_return(
Feature::Definition.new("development/#{table_rollout_flag}.yml",
{ type: 'gitlab_com_derisk', name: table_rollout_flag }
)
)
end
# the models defined here are leaked to other tests through
# `ActiveRecord::Base.descendants` and we need to counter the side effects
# from this. We redefine the method so that we don't check the FF existence
# outside of the examples here.
# `ActiveSupport::DescendantsTracker.clear` doesn't work with cached classes.
after do
model.define_singleton_method(:routing_table_enabled?) { false }
end
it { expect(model).not_to be_routing_class }
it { expect(partitioned_model).to be_routing_class }
it { expect(partitioned_model.table_name).to eq('_test_p_ci_jobs_metadata') }
it { expect(partitioned_model.quoted_table_name).to eq('"_test_p_ci_jobs_metadata"') }
it { expect(partitioned_model.arel_table.name).to eq('_test_p_ci_jobs_metadata') }
it { expect(partitioned_model.sequence_name).to eq('_test_ci_jobs_metadata_id_seq') }
context 'when switching the tables' do
before do
stub_feature_flags(table_rollout_flag => false)
end
%i[table_name quoted_table_name arel_table predicate_builder].each do |name|
it "switches #{name} to routing table and rollbacks" do
old_value = model.public_send(name)
routing_value = partitioned_model.public_send(name)
expect(old_value).not_to eq(routing_value)
expect { stub_feature_flags(table_rollout_flag => true) }
.to change(model, name).from(old_value).to(routing_value)
expect { stub_feature_flags(table_rollout_flag => false) }
.to change(model, name).from(routing_value).to(old_value)
end
end
it 'can switch aggregate methods' do
rollout_and_rollback_flag(
-> { expect(sql { model.count }).to all match(/FROM "_test_ci_jobs_metadata"/) },
-> { expect(sql { model.count }).to all match(/FROM "_test_p_ci_jobs_metadata"/) }
)
end
it 'can switch reads' do
rollout_and_rollback_flag(
-> { expect(sql { model.last }).to all match(/FROM "_test_ci_jobs_metadata"/) },
-> { expect(sql { model.last }).to all match(/FROM "_test_p_ci_jobs_metadata"/) }
)
end
it 'can switch inserts' do
rollout_and_rollback_flag(
-> {
expect(sql(filter: /INSERT/) { model.create! })
.to all match(/INSERT INTO "_test_ci_jobs_metadata"/)
},
-> {
expect(sql(filter: /INSERT/) { model.create! })
.to all match(/INSERT INTO "_test_p_ci_jobs_metadata"/)
}
)
end
it 'can switch deletes' do
3.times { model.create! }
rollout_and_rollback_flag(
-> {
expect(sql(filter: /DELETE/) { model.last.destroy! })
.to all match(/DELETE FROM "_test_ci_jobs_metadata"/)
},
-> {
expect(sql(filter: /DELETE/) { model.last.destroy! })
.to all match(/DELETE FROM "_test_p_ci_jobs_metadata"/)
}
)
end
context 'with associations' do
let(:job) { jobs_model.create! }
it 'reads' do
model.create!(job_id: job.id)
rollout_and_rollback_flag(
-> {
expect(sql(filter: /jobs_metadata/) { jobs_model.find(job.id).metadata })
.to all match(/FROM "_test_ci_jobs_metadata"/)
},
-> {
expect(sql(filter: /jobs_metadata/) { jobs_model.find(job.id).metadata })
.to all match(/FROM "_test_p_ci_jobs_metadata"/)
}
)
end
it 'writes' do
rollout_and_rollback_flag(
-> {
expect(sql(filter: [/INSERT/, /jobs_metadata/]) { jobs_model.find(job.id).create_metadata! })
.to all match(/INSERT INTO "_test_ci_jobs_metadata"/)
},
-> {
expect(sql(filter: [/INSERT/, /jobs_metadata/]) { jobs_model.find(job.id).create_metadata! })
.to all match(/INSERT INTO "_test_p_ci_jobs_metadata"/)
}
)
end
it 'deletes' do
3.times do
job = jobs_model.create!
job.create_metadata!
end
rollout_and_rollback_flag(
-> {
expect(sql(filter: [/DELETE/, /jobs_metadata/]) { jobs_model.last.destroy! })
.to all match(/DELETE FROM "_test_ci_jobs_metadata"/)
},
-> {
expect(sql(filter: [/DELETE/, /jobs_metadata/]) { jobs_model.last.destroy! })
.to all match(/DELETE FROM "_test_p_ci_jobs_metadata"/)
}
)
end
it 'can switch joins from jobs' do
rollout_and_rollback_flag(
-> {
expect(sql { jobs_model.joins(:metadata).last })
.to all match(/INNER JOIN "_test_ci_jobs_metadata"/)
},
-> {
expect(sql { jobs_model.joins(:metadata).last })
.to all match(/INNER JOIN "_test_p_ci_jobs_metadata"/)
}
)
end
it 'can switch joins from metadata' do
rollout_and_rollback_flag(
-> {
expect(sql { model.joins(:job).last })
.to all match(/FROM "_test_ci_jobs_metadata" INNER JOIN "_test_ci_jobs"/)
},
-> {
expect(sql { model.joins(:job).last })
.to all match(/FROM "_test_p_ci_jobs_metadata" INNER JOIN "_test_ci_jobs"/)
}
)
end
it 'preloads' do
job = jobs_model.create!
job.create_metadata!
rollout_and_rollback_flag(
-> {
expect(sql(filter: /jobs_metadata/) { jobs_model.preload(:metadata).last })
.to all match(/FROM "_test_ci_jobs_metadata"/)
},
-> {
expect(sql(filter: /jobs_metadata/) { jobs_model.preload(:metadata).last })
.to all match(/FROM "_test_p_ci_jobs_metadata"/)
}
)
end
context 'with nested attributes' do
before do
jobs_model.accepts_nested_attributes_for :metadata
end
it 'writes' do
attrs = { metadata_attributes: { expanded_environment_name: 'test_env_name' } }
rollout_and_rollback_flag(
-> {
expect(sql(filter: [/INSERT/, /jobs_metadata/]) { jobs_model.create!(attrs) })
.to all match(/INSERT INTO "_test_ci_jobs_metadata" .* 'test_env_name'/)
},
-> {
expect(sql(filter: [/INSERT/, /jobs_metadata/]) { jobs_model.create!(attrs) })
.to all match(/INSERT INTO "_test_p_ci_jobs_metadata" .* 'test_env_name'/)
}
)
end
end
end
end
context 'with safe request store', :request_store do
it 'changing the flag to true does not affect the current request' do
stub_feature_flags(table_rollout_flag => false)
expect(model.table_name).to eq('_test_ci_jobs_metadata')
stub_feature_flags(table_rollout_flag => true)
expect(model.table_name).to eq('_test_ci_jobs_metadata')
end
it 'changing the flag to false does not affect the current request' do
stub_feature_flags(table_rollout_flag => true)
expect(model.table_name).to eq('_test_p_ci_jobs_metadata')
stub_feature_flags(table_rollout_flag => false)
expect(model.table_name).to eq('_test_p_ci_jobs_metadata')
end
end
def rollout_and_rollback_flag(old, new)
# Load class and SQL statements cache
old.call
stub_feature_flags(table_rollout_flag => true)
# Test switch
new.call
stub_feature_flags(table_rollout_flag => false)
# Test that it can switch back in the same process
old.call
end
def create_tables(table_sql)
Ci::ApplicationRecord.connection.execute(table_sql)
end
def sql(filter: nil, &block)
ActiveRecord::QueryRecorder.new(&block)
.log
.select { |statement| Array.wrap(filter).all? { |regex| statement.match?(regex) } }
.tap { |result| expect(result).not_to be_empty }
end
end
|