Skip to content

Commit 80d8200

Browse files
committed
Fix MySQL 8.2 TRUNCATE failures by correcting connection pool handling
The ReferentialIntegrity helper for MySQL had a bug where db.disconnect was called before setting FOREIGN_KEY_CHECKS = 0. This caused the SET command to execute on a different connection than the subsequent TRUNCATE operations, leading to FK constraint violations. This bug was exposed more consistently in CI with MySQL 8.2 after adding indexes to the usage_events tables. The fix removes the db.disconnect call and properly saves/restores the original FK checks value. Fixes TRUNCATE errors in: - spec/request/service_usage_events_spec.rb - spec/request/app_usage_events_spec.rb - All tests using isolation: :truncation with MySQL
1 parent 05ed08a commit 80d8200

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

spec/support/referential_integrity.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ def without_referential_integrity_postgres
2525
end
2626

2727
def without_referential_integrity_mysql
28-
db.disconnect
28+
# Save the current foreign_key_checks value and disable it
29+
# Do NOT call db.disconnect as it would cause the SET command to execute
30+
# on a different connection than the subsequent TRUNCATE operations
31+
original_value = db.fetch('SELECT @@foreign_key_checks AS fk').first[:fk]
2932
db.run('SET FOREIGN_KEY_CHECKS = 0;')
3033
yield
3134
ensure
32-
db.run('SET FOREIGN_KEY_CHECKS = 1;')
35+
db.run("SET FOREIGN_KEY_CHECKS = #{original_value};")
3336
end
3437
end

0 commit comments

Comments
 (0)