diff --git a/lib/tasks/data/truncate_data.rb b/lib/tasks/data/truncate_data.rb index 6e789128c8..68b10c0a63 100644 --- a/lib/tasks/data/truncate_data.rb +++ b/lib/tasks/data/truncate_data.rb @@ -1,6 +1,12 @@ # frozen_string_literal: true class TruncateData + # This model lets us operate on the sessions DB table using ActiveRecord's + # methods within the scope of this service. This relies on the AR's + # convention where a Session model maps to a sessions table. + class Session < ActiveRecord::Base + end + def initialize(months_to_keep = nil) @date = (months_to_keep || 3).months.ago end @@ -36,7 +42,7 @@ class TruncateData Spree::ReturnAuthorization.delete_all Spree::StateChange.delete_all("created_at < '#{1.month.ago.to_date}'") Spree::LogEntry.delete_all("created_at < '#{1.month.ago.to_date}'") - sql_delete_from "sessions" + Session.delete_all("created_at < '#{2.weeks.ago.to_date}'") end def truncate_subscriptions diff --git a/spec/lib/tasks/data/truncate_data_spec.rb b/spec/lib/tasks/data/truncate_data_spec.rb index bc79463f48..d5972fa2ea 100644 --- a/spec/lib/tasks/data/truncate_data_spec.rb +++ b/spec/lib/tasks/data/truncate_data_spec.rb @@ -7,6 +7,7 @@ describe TruncateData do allow(Spree::ReturnAuthorization).to receive(:delete_all) allow(Spree::StateChange).to receive(:delete_all) allow(Spree::LogEntry).to receive(:delete_all) + allow(TruncateData::Session).to receive(:delete_all) end context 'when months_to_keep is not specified' do @@ -36,6 +37,14 @@ describe TruncateData do .to have_received(:delete_all) .with("created_at < '#{1.month.ago.to_date}'") end + + it 'deletes sessions older than two weeks' do + TruncateData.new.call + + expect(TruncateData::Session) + .to have_received(:delete_all) + .with("created_at < '#{2.weeks.ago.to_date}'") + end end context 'when months_to_keep is nil' do @@ -65,6 +74,14 @@ describe TruncateData do .to have_received(:delete_all) .with("created_at < '#{1.month.ago.to_date}'") end + + it 'deletes sessions older than two weeks' do + TruncateData.new.call + + expect(TruncateData::Session) + .to have_received(:delete_all) + .with("created_at < '#{2.weeks.ago.to_date}'") + end end context 'when months_to_keep is specified' do @@ -98,6 +115,14 @@ describe TruncateData do .to have_received(:delete_all) .with("created_at < '#{1.month.ago.to_date}'") end + + it 'deletes sessions older than two weeks' do + TruncateData.new.call + + expect(TruncateData::Session) + .to have_received(:delete_all) + .with("created_at < '#{2.weeks.ago.to_date}'") + end end end end