From 0149c4f7916823a064366135ad413699855f7110 Mon Sep 17 00:00:00 2001 From: Ryan Krage Date: Fri, 8 Sep 2023 15:43:07 +0000 Subject: [PATCH] add test for sub-partitioning --- spec/safe_statements_spec.rb | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/spec/safe_statements_spec.rb b/spec/safe_statements_spec.rb index bae4e9d..132e81c 100644 --- a/spec/safe_statements_spec.rb +++ b/spec/safe_statements_spec.rb @@ -3523,7 +3523,7 @@ def up end end - it "waits to acquire a lock if the table is partitioned and child tables are blocked" do + it "waits to acquire a lock if the table is partitioned and child table is blocked" do stub_const("PgHaMigrations::LOCK_TIMEOUT_SECONDS", 1) ActiveRecord::Base.connection.drop_table(table_name) @@ -3551,6 +3551,40 @@ def up end end + it "waits to acquire a lock if the table is partitioned and child sub-partition is blocked" do + stub_const("PgHaMigrations::LOCK_TIMEOUT_SECONDS", 1) + + ActiveRecord::Base.connection.drop_table(table_name) + create_range_partitioned_table(table_name, migration_klass) + create_range_partitioned_table("#{table_name}_sub", migration_klass, with_partman: true) + ActiveRecord::Base.connection.execute(<<~SQL) + ALTER TABLE bogus_table + ATTACH PARTITION bogus_table_sub + FOR VALUES FROM ('2020-01-01') TO ('2020-02-01') + SQL + + begin + thread = Thread.new do + migration.safely_acquire_lock_for_table(:bogus_table_sub_default) { sleep 3 } + end + + sleep 1.1 + + expect(PgHaMigrations::BlockingDatabaseTransactions).to receive(:find_blocking_transactions) + .at_least(3) + .times + .and_call_original + + migration.suppress_messages do + migration.safely_acquire_lock_for_table(table_name) do + expect(locks_for_table(table_name, connection: alternate_connection)).not_to be_empty + end + end + ensure + thread.join + end + end + it "fails lock acquisition quickly if Postgres doesn't grant an exclusive lock but then retries" do stub_const("PgHaMigrations::LOCK_TIMEOUT_SECONDS", 1)