Skip to content

Commit db677c4

Browse files
committed
Fix MySQL test cleanup by using DELETE instead of TRUNCATE
The original approach of disabling foreign key checks before TRUNCATE was unreliable due to MySQL's session-based FOREIGN_KEY_CHECKS setting and Sequel's connection pooling. Each db.run() call can get a different connection from the pool, so SET FOREIGN_KEY_CHECKS=0 executed on one connection doesn't affect TRUNCATE statements executed on other connections. Solution: Use DELETE FROM instead of TRUNCATE for MySQL test cleanup. While slower, DELETE respects foreign key constraints by deleting in the correct order, and doesn't require disabling FK checks. PostgreSQL continues to use TRUNCATE with CASCADE, which handles foreign keys correctly in a single statement. This fixes the 208 test failures seen in CI with MySQL 8.2.
1 parent c27eaf2 commit db677c4

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

spec/support/table_truncator.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ def self.isolated_tables(db)
99
end
1010

1111
def truncate_tables
12-
referential_integrity = ReferentialIntegrity.new(db)
13-
referential_integrity.without do
14-
case db.database_type
15-
when :postgres
12+
case db.database_type
13+
when :postgres
14+
referential_integrity = ReferentialIntegrity.new(db)
15+
referential_integrity.without do
1616
tables.each do |table|
1717
db.run("TRUNCATE TABLE #{table} RESTART IDENTITY CASCADE;")
1818
end
19-
when :mysql
20-
tables.each do |table|
21-
db.run("TRUNCATE TABLE #{table};")
22-
end
19+
end
20+
when :mysql
21+
# Use DELETE instead of TRUNCATE for MySQL to avoid FK constraint issues
22+
# with connection pooling. TRUNCATE requires SET FOREIGN_KEY_CHECKS=0,
23+
# which is session-specific and doesn't work reliably across pooled connections.
24+
tables.each do |table|
25+
db.run("DELETE FROM #{table};")
2326
end
2427
end
2528
end

0 commit comments

Comments
 (0)