Skip to content

Commit c7f1f22

Browse files
committed
Handle custom session_class in trim task
Better approach to rails#178
1 parent 78e0047 commit c7f1f22

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ You must implement these methods:
9595
* `save`
9696
* `destroy`
9797

98+
Note that some of the provided [rake tasks](https://github.com/rails/activerecord-session_store/blob/master/lib/tasks/database.rake)
99+
require additional methods to be implemented:
100+
101+
* `db:sessions:clear` depends on `table_name`
102+
* `db:sessions:trim` depends on `where` and `delete_all`
103+
* `db:sessions:upgrade` depends on `secure!`
104+
98105
The example SqlBypass class is a generic SQL session store. You may
99106
use it as a basis for high-performance database-specific stores.
100107

lib/tasks/database.rake

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ namespace 'db:sessions' do
88

99
desc "Clear the sessions table"
1010
task :clear => [:environment, 'db:load_config'] do
11-
ActiveRecord::Base.connection.execute "TRUNCATE TABLE #{ActiveRecord::SessionStore::Session.table_name}"
11+
ActiveRecord::Base.connection.truncate(ActionDispatch::Session::ActiveRecordStore.session_class.table_name)
1212
end
1313

1414
desc "Trim old sessions from the table (default: > 30 days)"
1515
task :trim => [:environment, 'db:load_config'] do
1616
cutoff_period = (ENV['SESSION_DAYS_TRIM_THRESHOLD'] || 30).to_i.days.ago
17-
ActiveRecord::SessionStore::Session.
17+
ActionDispatch::Session::ActiveRecordStore.session_class.
1818
where("updated_at < ?", cutoff_period).
1919
delete_all
2020
end

test/tasks/database_rake_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ def test_upgrade_task
6363
assert Session.find_by_session_id(Rack::Session::SessionId.new("original_session_id").private_id)
6464
assert Session.find_by_session_id("2::secure_session_id")
6565
end
66+
67+
def test_clear_task
68+
Session.create!(data: "obsolete")
69+
70+
Rake.application.invoke_task 'db:sessions:clear'
71+
72+
assert_equal 0, Session.count
73+
end
6674
end
6775
end
6876
end

0 commit comments

Comments
 (0)