|
9 | 9 | let(:model) do |
10 | 10 | model = double('Model', table_name: 'events') # rubocop:disable RSpec/VerifiedDoubles |
11 | 11 | allow(model).to receive(:try).with(:partitioning_strategy).and_return(partitioning_strategy) |
12 | | - allow(model).to receive(:partitioning_strategy).and_return(partitioning_strategy) |
| 12 | + allow(model).to receive_messages( |
| 13 | + partitioning_strategy: partitioning_strategy, |
| 14 | + foreign_keys_to_drop_on_detach: foreign_keys_to_drop_on_detach |
| 15 | + ) |
13 | 16 | allow(model).to receive(:with_connection).and_yield(connection) |
14 | 17 | model |
15 | 18 | end |
16 | 19 |
|
17 | 20 | let(:partitioning_strategy) { double('Strategy') } |
18 | 21 |
|
| 22 | + let(:foreign_keys_to_drop_on_detach) { [] } |
| 23 | + |
19 | 24 | let(:connection) do |
20 | 25 | connection = double('Connection') |
21 | 26 | allow(connection).to receive(:execute) |
22 | 27 | allow(connection).to receive(:transaction).and_yield |
23 | 28 | allow(connection).to receive(:quote_table_name) { |name| "\"#{name}\"" } |
| 29 | + allow(connection).to receive(:quote_column_name) { |name| "\"#{name}\"" } |
24 | 30 | allow(connection).to receive(:quote) { |value| "'#{value}'" } |
| 31 | + allow(connection).to receive_messages(select_rows: [], select_values: []) |
25 | 32 | connection |
26 | 33 | end |
27 | 34 |
|
|
172 | 179 |
|
173 | 180 | expect(connection).not_to have_received(:execute).with(a_string_matching(/DETACH/)) |
174 | 181 | end |
| 182 | + |
| 183 | + context 'when the model declares foreign keys to drop on detach' do |
| 184 | + let(:foreign_keys_to_drop_on_detach) { %w[fk_events_parent fk_events_other] } |
| 185 | + |
| 186 | + let(:partition) do |
| 187 | + Code0::ZeroTrack::Database::Partitioning::TimePartition.new( |
| 188 | + model, '2023-01-01', '2023-02-01', partition_name: 'events_202301' |
| 189 | + ) |
| 190 | + end |
| 191 | + |
| 192 | + before do |
| 193 | + allow(partitioning_strategy).to receive(:partitions_to_detach).and_return([partition]) |
| 194 | + end |
| 195 | + |
| 196 | + it 'drops the declared foreign keys found on detached partitions' do |
| 197 | + allow(connection).to receive(:select_rows).and_return( |
| 198 | + [['"partitions_dynamic"."events_202301"', 'fk_events_parent']] |
| 199 | + ) |
| 200 | + |
| 201 | + described_class.new(model).detach_partitions! |
| 202 | + |
| 203 | + expect(connection).to have_received(:execute).with( |
| 204 | + a_string_matching( |
| 205 | + /ALTER TABLE "partitions_dynamic"."events_202301" DROP CONSTRAINT "fk_events_parent"/ |
| 206 | + ) |
| 207 | + ) |
| 208 | + end |
| 209 | + |
| 210 | + it 'reconciles every detached partition, not only the one detached in this run' do |
| 211 | + # Two already-detached partitions still carry the inherited foreign key. |
| 212 | + allow(connection).to receive(:select_rows).and_return( |
| 213 | + [ |
| 214 | + ['"partitions_dynamic"."events_202301"', 'fk_events_parent'], |
| 215 | + ['"partitions_dynamic"."events_202212"', 'fk_events_parent'] |
| 216 | + ] |
| 217 | + ) |
| 218 | + |
| 219 | + described_class.new(model).detach_partitions! |
| 220 | + |
| 221 | + expect(connection).to have_received(:execute).with( |
| 222 | + a_string_matching(/"partitions_dynamic"."events_202301" DROP CONSTRAINT "fk_events_parent"/) |
| 223 | + ) |
| 224 | + expect(connection).to have_received(:execute).with( |
| 225 | + a_string_matching(/"partitions_dynamic"."events_202212" DROP CONSTRAINT "fk_events_parent"/) |
| 226 | + ) |
| 227 | + end |
| 228 | + |
| 229 | + it 'scopes the catalog lookup to the declared constraint names and dynamic schema' do |
| 230 | + described_class.new(model).detach_partitions! |
| 231 | + |
| 232 | + expect(connection).to have_received(:select_rows).with( |
| 233 | + a_string_matching(/pg_catalog\.pg_constraint.*contype = 'f'/m) |
| 234 | + ) |
| 235 | + expect(connection).to have_received(:select_rows).with( |
| 236 | + a_string_matching(/'fk_events_parent', 'fk_events_other'/) |
| 237 | + ) |
| 238 | + expect(connection).to have_received(:select_rows).with( |
| 239 | + a_string_matching(/'partitions_dynamic'/) |
| 240 | + ) |
| 241 | + end |
| 242 | + |
| 243 | + it 'does not attempt to drop anything when the catalog reports no matching foreign keys' do |
| 244 | + allow(connection).to receive(:select_rows).and_return([]) |
| 245 | + |
| 246 | + described_class.new(model).detach_partitions! |
| 247 | + |
| 248 | + expect(connection).not_to have_received(:execute).with(a_string_matching(/DROP CONSTRAINT/)) |
| 249 | + end |
| 250 | + |
| 251 | + it 'skips the catalog lookup entirely when no foreign keys are declared' do |
| 252 | + allow(model).to receive(:foreign_keys_to_drop_on_detach).and_return([]) |
| 253 | + |
| 254 | + described_class.new(model).detach_partitions! |
| 255 | + |
| 256 | + expect(connection).not_to have_received(:select_rows) |
| 257 | + end |
| 258 | + |
| 259 | + it 'warns about declared foreign keys missing from the partitioned parent table' do |
| 260 | + allow(connection).to receive(:select_values).and_return(%w[fk_events_parent]) |
| 261 | + |
| 262 | + described_class.new(model).detach_partitions! |
| 263 | + |
| 264 | + expect(rails_logger).to have_received(:warn).with( |
| 265 | + hash_including( |
| 266 | + message: /do not exist on the partitioned table/, |
| 267 | + table_name: 'events', |
| 268 | + constraint_names: %w[fk_events_other] |
| 269 | + ) |
| 270 | + ) |
| 271 | + end |
| 272 | + |
| 273 | + it 'does not warn when all declared foreign keys exist on the partitioned parent table' do |
| 274 | + allow(connection).to receive(:select_values).and_return(%w[fk_events_parent fk_events_other]) |
| 275 | + |
| 276 | + described_class.new(model).detach_partitions! |
| 277 | + |
| 278 | + expect(rails_logger).not_to have_received(:warn) |
| 279 | + end |
| 280 | + end |
175 | 281 | end |
176 | 282 |
|
177 | 283 | describe '#drop_partitions' do |
|
0 commit comments