Skip to content

Commit

Permalink
Update data retention periods
Browse files Browse the repository at this point in the history
Sessions and cart data are removed if older than 2 months, instead of 6.
  • Loading branch information
Matt-Yorkley committed Jan 27, 2021
1 parent 0f060dd commit ab2f26c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
11 changes: 6 additions & 5 deletions lib/tasks/data/remove_transient_data.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

class RemoveTransientData
RETENTION_PERIOD = 6.months.ago.to_date
MEDIUM_RETENTION = 6.months.ago.to_date
SHORT_RETENTION = 2.months.ago.to_date

# 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
Expand All @@ -12,9 +13,9 @@ class Session < ActiveRecord::Base
def call
Rails.logger.info("#{self.class.name}: processing")

Spree::StateChange.where("created_at < ?", RETENTION_PERIOD).delete_all
Spree::LogEntry.where("created_at < ?", RETENTION_PERIOD).delete_all
Session.where("updated_at < ?", RETENTION_PERIOD).delete_all
Spree::StateChange.where("created_at < ?", MEDIUM_RETENTION).delete_all
Spree::LogEntry.where("created_at < ?", MEDIUM_RETENTION).delete_all
Session.where("updated_at < ?", SHORT_RETENTION).delete_all

clear_old_cart_data!
clear_line_item_option_values!
Expand All @@ -23,7 +24,7 @@ def call
private

def clear_old_cart_data!
old_carts = Spree::Order.where("state = 'cart' AND updated_at < ?", RETENTION_PERIOD)
old_carts = Spree::Order.where("state = 'cart' AND updated_at < ?", SHORT_RETENTION)
old_cart_line_items = Spree::LineItem.where(order_id: old_carts)
old_cart_adjustments = Spree::Adjustment.where(order_id: old_carts)

Expand Down
11 changes: 6 additions & 5 deletions spec/lib/tasks/data/remove_transient_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

describe RemoveTransientData do
describe '#call' do
let(:retention_period) { RemoveTransientData::RETENTION_PERIOD }
let(:medium_retention) { RemoveTransientData::MEDIUM_RETENTION }
let(:short_retention) { RemoveTransientData::SHORT_RETENTION }

before do
allow(Spree::StateChange).to receive(:delete_all)
Expand All @@ -15,21 +16,21 @@
end

it 'deletes state changes older than rentention_period' do
Spree::StateChange.create(created_at: retention_period - 1.day)
Spree::StateChange.create(created_at: medium_retention - 1.day)

RemoveTransientData.new.call
expect(Spree::StateChange.all).to be_empty
end

it 'deletes log entries older than retention_period' do
Spree::LogEntry.create(created_at: retention_period - 1.day)
Spree::LogEntry.create(created_at: medium_retention - 1.day)

expect { RemoveTransientData.new.call }
.to change(Spree::LogEntry, :count).by(-1)
end

it 'deletes sessions older than retention_period' do
RemoveTransientData::Session.create(session_id: 1, updated_at: retention_period - 1.day)
RemoveTransientData::Session.create(session_id: 1, updated_at: short_retention - 1.day)

RemoveTransientData.new.call

Expand All @@ -44,7 +45,7 @@
let!(:line_item) { create(:line_item, order: cart, variant: variant) }
let!(:adjustment) { create(:adjustment, order: cart) }

let!(:old_cart) { create(:order, state: 'cart', updated_at: retention_period - 1.day) }
let!(:old_cart) { create(:order, state: 'cart', updated_at: short_retention - 1.day) }
let!(:old_line_item) { create(:line_item, order: old_cart, variant: variant) }
let!(:old_adjustment) { create(:adjustment, order: old_cart) }

Expand Down

0 comments on commit ab2f26c

Please sign in to comment.