Skip to content

Commit aab1d83

Browse files
committed
Fix leaky test:
- Fix rails#51347 - The tests in this suite are leaky which results in other tests from other suite from sometimes failing. The problem is due to modifying some database tables while a prepared statement on Postgreql already exists. This causes Postgres to report an error "cached plan must not change result type". The order of reproduction is as follow: - We have more than one connection in the pool (Let's say Conn1 and Conn2 for the sake of the example) - A first test runs a query, Conn1 is used, we prepare a statement. - The leaky test modify some table on Conn2. - A third test runs a query, Conn1 is used, the query was prepared. Postgresql reports the "cached plan must not change result type". This commis fixes that by clearing the statement cache on each connection.
1 parent 00cc4ff commit aab1d83

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

activerecord/test/cases/migration_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ def setup
6161
Thing.lease_connection.drop_table(table) rescue nil
6262
end
6363
Thing.reset_column_information
64+
clear_statement_cache(Thing)
6465

6566
%w(reminders people_reminders prefix_reminders_suffix).each do |table|
6667
Reminder.lease_connection.drop_table(table) rescue nil
6768
end
6869
Reminder.reset_table_name
6970
Reminder.reset_column_information
71+
clear_statement_cache(Reminder)
7072

7173
%w(last_name key bio age height wealth birthday favorite_day
7274
moment_of_truth male administrator funny).each do |column|
@@ -76,6 +78,7 @@ def setup
7678
Person.lease_connection.remove_column("people", "middle_name") rescue nil
7779
Person.lease_connection.add_column("people", "first_name", :string)
7880
Person.reset_column_information
81+
clear_statement_cache(Person)
7982

8083
ActiveRecord::Migration.verbose = @verbose_was
8184
end
@@ -1131,6 +1134,12 @@ def self.base_class; self; end
11311134
}
11321135
end
11331136

1137+
def clear_statement_cache(model)
1138+
model.connection_handler.each_connection_pool do |pool|
1139+
pool.connections.each(&:clear_cache!)
1140+
end
1141+
end
1142+
11341143
def with_another_process_holding_lock(lock_id)
11351144
thread_lock = Concurrent::CountDownLatch.new
11361145
test_terminated = Concurrent::CountDownLatch.new

0 commit comments

Comments
 (0)