From ec66bdf651389f4ae2a05b239b29c5ea073bed65 Mon Sep 17 00:00:00 2001 From: Jesse Date: Tue, 21 Jan 2025 09:34:36 -0800 Subject: [PATCH 1/3] caching strategy for reset column method --- lib/hijacker.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/hijacker.rb b/lib/hijacker.rb index 16161da..cc9ee85 100644 --- a/lib/hijacker.rb +++ b/lib/hijacker.rb @@ -81,7 +81,7 @@ def self.connect(target_name, sister_name = nil, options = {}) #reenable_query_caching - reset_column_info_for_active_record + reset_column_info_for_active_record(target_name) run_after_hijack_callback @@ -299,8 +299,11 @@ def self.run_after_hijack_callback # Reset column information to ensure models are up-to-date with the new schema. # This ensures that any changes to the database schema are immediately # in a multi-tenant architecture where different tenants might have - def self.reset_column_info_for_active_record - ::ActiveRecord::Base.descendants.each do |klass| + def self.reset_column_info_for_active_record(target_name) + descendants = Rails.cache.fetch("#{target_name}/hijacker_descendants", expires_in: 12.hours) do + ::ActiveRecord::Base.descendants + end + descendants.each do |klass| klass.reset_column_information end end From 0d221f88e46badab43fcd4ce21d03128eb92210c Mon Sep 17 00:00:00 2001 From: Jesse Date: Tue, 21 Jan 2025 09:34:36 -0800 Subject: [PATCH 2/3] caching strategy for reset column method --- lib/hijacker.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/hijacker.rb b/lib/hijacker.rb index 16161da..cc9ee85 100644 --- a/lib/hijacker.rb +++ b/lib/hijacker.rb @@ -81,7 +81,7 @@ def self.connect(target_name, sister_name = nil, options = {}) #reenable_query_caching - reset_column_info_for_active_record + reset_column_info_for_active_record(target_name) run_after_hijack_callback @@ -299,8 +299,11 @@ def self.run_after_hijack_callback # Reset column information to ensure models are up-to-date with the new schema. # This ensures that any changes to the database schema are immediately # in a multi-tenant architecture where different tenants might have - def self.reset_column_info_for_active_record - ::ActiveRecord::Base.descendants.each do |klass| + def self.reset_column_info_for_active_record(target_name) + descendants = Rails.cache.fetch("#{target_name}/hijacker_descendants", expires_in: 12.hours) do + ::ActiveRecord::Base.descendants + end + descendants.each do |klass| klass.reset_column_information end end From 2a4434777d6ed00d3e0118eefd2b0d544b180c89 Mon Sep 17 00:00:00 2001 From: Jesse Date: Wed, 22 Jan 2025 15:08:16 -0800 Subject: [PATCH 3/3] switching cache for descendants query from memcached to redis --- lib/hijacker.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/hijacker.rb b/lib/hijacker.rb index cc9ee85..4e92857 100644 --- a/lib/hijacker.rb +++ b/lib/hijacker.rb @@ -300,8 +300,11 @@ def self.run_after_hijack_callback # This ensures that any changes to the database schema are immediately # in a multi-tenant architecture where different tenants might have def self.reset_column_info_for_active_record(target_name) - descendants = Rails.cache.fetch("#{target_name}/hijacker_descendants", expires_in: 12.hours) do - ::ActiveRecord::Base.descendants + descendants_key = "#{target_name}/hijacker_descendants" + descendants = $redis.get(descendants_key) + if descendants.blank? + descendants = ::ActiveRecord::Base.descendants + $redis.setex(descendants_key, 12.hours,descendants) end descendants.each do |klass| klass.reset_column_information