Skip to content

Commit

Permalink
add test for sub-partitioning
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrage committed Sep 8, 2023
1 parent 5520300 commit 0149c4f
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion spec/safe_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 0149c4f

Please sign in to comment.