From db917f1e519ad13cc1578ad5dd34b4cab80db313 Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Thu, 12 Feb 2026 13:11:23 -0500 Subject: [PATCH] Deprecate sessid column fallback I'm working on removing queries in my application that should be going through the SchemaCache in production but currently are not. I found that the `sessions` table is being queried during runtime due to the `reset_column_information` when the class is loaded (which clears the SchemaCache for the `sessions` table). This commit deprecates the cause of the schema cache clearing: the `sessid` fallback. This code checks the model's columns and conditionally redefines methods to allow using `sessid` instead of `session_id`. The `session_id` -> `sessid` fallback has been in place for [twenty years][1], I'd be surprised if there's even a single app still using `sessid`. Also of note: many `app.deprecators` implementations check that the `app` responds to `deprecators` (which was added in Rails 7.1), but that's our minimum version so we can skip it. [1]: rails/rails@452442d Co-authored-by: elasticspoon --- lib/active_record/session_store.rb | 7 +++++++ lib/active_record/session_store/railtie.rb | 4 ++++ lib/active_record/session_store/session.rb | 4 ++++ test/helper.rb | 2 ++ test/session_test.rb | 4 +++- 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/active_record/session_store.rb b/lib/active_record/session_store.rb index 4f56c5e..d1a7802 100644 --- a/lib/active_record/session_store.rb +++ b/lib/active_record/session_store.rb @@ -1,6 +1,7 @@ require 'active_record' require 'active_record/session_store/version' require 'action_dispatch/session/active_record_store' +require 'active_support' require 'active_support/core_ext/hash/keys' require 'json' @@ -8,6 +9,12 @@ module ActiveRecord module SessionStore autoload :Session, 'active_record/session_store/session' + class << self + def deprecator + @deprecator ||= ActiveSupport::Deprecation.new("3.0", "ActiveRecord::SessionStore") + end + end + module ClassMethods # :nodoc: mattr_accessor :serializer diff --git a/lib/active_record/session_store/railtie.rb b/lib/active_record/session_store/railtie.rb index 9159cb5..f2b867c 100644 --- a/lib/active_record/session_store/railtie.rb +++ b/lib/active_record/session_store/railtie.rb @@ -4,6 +4,10 @@ module ActiveRecord module SessionStore class Railtie < Rails::Railtie rake_tasks { load File.expand_path("../../../tasks/database.rake", __FILE__) } + + initializer "activerecord-session_store.deprecator" do |app| + app.deprecators[:"activerecord-session_store"] = SessionStore.deprecator + end end end end diff --git a/lib/active_record/session_store/session.rb b/lib/active_record/session_store/session.rb index aec3253..3745766 100644 --- a/lib/active_record/session_store/session.rb +++ b/lib/active_record/session_store/session.rb @@ -38,6 +38,10 @@ def setup_sessid_compatibility! # Reset column info since it may be stale. reset_column_information if columns_hash['sessid'] + SessionStore.deprecator.warn <<~MSG + Using a session ID column other than `session_id` is deprecated without replacement. You should migrate your session table to use `session_id`. + MSG + def self.find_by_session_id(session_id) find_by_sessid(session_id) end diff --git a/test/helper.rb b/test/helper.rb index 4a9ad7c..f51414d 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -7,6 +7,8 @@ require 'active_record/session_store' +ActiveRecord::SessionStore.deprecator.behavior = :raise + ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') SharedTestRoutes = ActionDispatch::Routing::RouteSet.new diff --git a/test/session_test.rb b/test/session_test.rb index e49c76d..8609bd4 100644 --- a/test/session_test.rb +++ b/test/session_test.rb @@ -100,7 +100,9 @@ def self.session_id_column session.sessid = "100" session.save! - found = klass.find_by_session_id("100") + found = assert_deprecated(ActiveRecord::SessionStore.deprecator) do + klass.find_by_session_id("100") + end assert_equal session, found assert_equal session.sessid, found.session_id ensure