Skip to content

Commit 42703e5

Browse files
committed
Add UNLOGGED tables rake task for test performance
Adds db:test:unlogged task that converts all PostgreSQL tables to UNLOGGED mode for faster test performance. UNLOGGED tables skip WAL logging, making writes faster during test setup. Auto-runs after db:test:prepare when in test environment. This is optional and can be removed if it causes issues.
1 parent a918312 commit 42703e5

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

lib/tasks/test_unlogged.rake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace :db do
2+
namespace :test do
3+
desc "Convert all tables to UNLOGGED for faster test performance"
4+
task unlogged: :environment do
5+
raise "This task only works in test environment" unless Rails.env.test?
6+
7+
tables = ActiveRecord::Base.connection.tables
8+
converted = 0
9+
tables.each do |table|
10+
next if table == "schema_migrations" || table == "ar_internal_metadata"
11+
result = ActiveRecord::Base.connection.execute(
12+
"SELECT relpersistence FROM pg_class WHERE relname = '#{table}'"
13+
)
14+
if result.first && result.first["relpersistence"] != "u"
15+
ActiveRecord::Base.connection.execute("ALTER TABLE #{table} SET UNLOGGED")
16+
converted += 1
17+
end
18+
end
19+
puts "Converted #{converted} tables to UNLOGGED" if converted > 0
20+
end
21+
end
22+
end
23+
24+
# Auto-run after db:test:prepare
25+
Rake::Task["db:test:prepare"].enhance do
26+
Rake::Task["db:test:unlogged"].invoke if Rails.env.test?
27+
end

0 commit comments

Comments
 (0)