-
Notifications
You must be signed in to change notification settings - Fork 188
/
database.rake
26 lines (23 loc) · 1.08 KB
/
database.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace 'db:sessions' do
desc "Creates a sessions migration for use with ActiveRecord::SessionStore"
task :create => [:environment, 'db:load_config'] do
Rails.application.load_generators
require 'generators/active_record/session_migration_generator'
ActiveRecord::Generators::SessionMigrationGenerator.start [ ENV['MIGRATION'] || 'add_sessions_table' ]
end
desc "Clear the sessions table"
task :clear => [:environment, 'db:load_config'] do
ActiveRecord::Base.connection.execute "TRUNCATE TABLE #{ActiveRecord::SessionStore::Session.table_name}"
end
desc "Trim old sessions from the table (default: > 30 days)"
task :trim => [:environment, 'db:load_config'] do
cutoff_period = (ENV['SESSION_DAYS_TRIM_THRESHOLD'] || 30).to_i.days.ago
ActiveRecord::SessionStore::Session.
where("updated_at < ?", cutoff_period).
delete_all
end
desc "Upgrade current sessions in the database to the secure version"
task :upgrade => [:environment, 'db:load_config'] do
ActionDispatch::Session::ActiveRecordStore.session_class.find_each(&:secure!)
end
end