Simplify messages_count trigger install and harden ensure!

Require all three SQLite triggers before treating the counter as installed,
drop the schema.rb / dump-rewrite install paths in favor of rake ensure after
schema load, isolate destructive trigger tests, and cover foreign room moves
plus fixture baseline counts.

Co-authored-by: Thomas Klemm <github@tklemm.eu>
This commit is contained in:
Cursor Agent
2026-10-07 19:46:57 +00:00
parent 1f5858098b
commit 9fd1563c9b
8 changed files with 73 additions and 94 deletions
+3 -25
View File
@@ -1,38 +1,16 @@
# schema.rb cannot dump SQLite triggers. Reinstall after loads, and re-append
# Room::MessagesCount.install! after dumps so the call is not lost.
# schema.rb cannot dump SQLite triggers — reinstall after schema loads.
namespace :room_messages_count do
task ensure: :environment do
ActiveRecord::Base.connection_pool.with_connection do |connection|
next unless connection.adapter_name.match?(/sqlite/i)
Room::MessagesCount.ensure!(connection)
end
end
task append_schema_install: :environment do
schema = Rails.root.join("db/schema.rb")
contents = schema.read
marker = "Room::MessagesCount.install!"
next if contents.include?(marker)
contents.sub!(/\nend\n?\z/, <<~RUBY)
# SQLite triggers are not dumped by schema.rb; keep rooms.messages_count honest after schema:load.
#{marker}
end
RUBY
schema.write(contents)
end
end
{
"db:schema:load" => "room_messages_count:ensure",
"db:test:load_schema" => "room_messages_count:ensure",
"db:schema:dump" => "room_messages_count:append_schema_install"
}.each do |task_name, enhancement|
%w[db:schema:load db:test:load_schema].each do |task_name|
next unless Rake::Task.task_defined?(task_name)
Rake::Task[task_name].enhance do
Rake::Task[enhancement].invoke
Rake::Task["room_messages_count:ensure"].invoke
end
end