Skip to content

Commit b7ff7d0

Browse files
committed
Make cutoff_age_in_days a named parameter
- Change initialize signature to use keyword argument: cutoff_age_in_days: - Rename internal variable @days_ago to @cutoff_age_in_days for consistency
1 parent 3e7bba2 commit b7ff7d0

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

app/jobs/runtime/events_cleanup.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(cutoff_age_in_days)
99
end
1010

1111
def perform
12-
Database::OldRecordCleanup.new(Event, cutoff_age_in_days).delete
12+
Database::OldRecordCleanup.new(Event, cutoff_age_in_days:).delete
1313
end
1414

1515
def job_name_in_configuration

app/repositories/app_usage_event_repository.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def purge_and_reseed_started_apps!
152152
end
153153

154154
def delete_events_older_than(cutoff_age_in_days)
155-
Database::OldRecordCleanup.new(AppUsageEvent, cutoff_age_in_days, keep_at_least_one_record: true).delete
155+
Database::OldRecordCleanup.new(AppUsageEvent, cutoff_age_in_days: cutoff_age_in_days, keep_at_least_one_record: true).delete
156156
end
157157

158158
private

app/repositories/service_usage_event_repository.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def purge_and_reseed_service_instances!
9292
end
9393

9494
def delete_events_older_than(cutoff_age_in_days)
95-
Database::OldRecordCleanup.new(ServiceUsageEvent, cutoff_age_in_days, keep_at_least_one_record: true).delete
95+
Database::OldRecordCleanup.new(ServiceUsageEvent, cutoff_age_in_days: cutoff_age_in_days, keep_at_least_one_record: true).delete
9696
end
9797
end
9898
end

lib/database/old_record_cleanup.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
module Database
44
class OldRecordCleanup
55
class NoCurrentTimestampError < StandardError; end
6-
attr_reader :model, :days_ago, :keep_at_least_one_record
6+
attr_reader :model, :cutoff_age_in_days, :keep_at_least_one_record
77

8-
def initialize(model, days_ago, keep_at_least_one_record: false)
8+
def initialize(model, cutoff_age_in_days:, keep_at_least_one_record: false)
99
@model = model
10-
@days_ago = days_ago
10+
@cutoff_age_in_days = cutoff_age_in_days
1111
@keep_at_least_one_record = keep_at_least_one_record
1212
end
1313

1414
def delete
15-
cutoff_date = current_timestamp_from_database - days_ago.to_i.days
15+
cutoff_date = current_timestamp_from_database - cutoff_age_in_days.to_i.days
1616

1717
old_records = model.dataset.where(Sequel.lit('created_at < ?', cutoff_date))
1818
if keep_at_least_one_record

spec/unit/lib/database/old_record_cleanup_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
let!(:fresh_event) { VCAP::CloudController::Event.make(created_at: 1.day.ago + 1.minute) }
1010

1111
it 'deletes records older than specified days' do
12-
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::Event, 1)
12+
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::Event, cutoff_age_in_days: 1)
1313

1414
expect do
1515
record_cleanup.delete
@@ -22,7 +22,7 @@
2222

2323
context "when there are no records at all but you're trying to keep at least one" do
2424
it "doesn't keep one because there aren't any to keep" do
25-
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::ServiceUsageEvent, 1, keep_at_least_one_record: true)
25+
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::ServiceUsageEvent, cutoff_age_in_days: 1, keep_at_least_one_record: true)
2626

2727
expect { record_cleanup.delete }.not_to raise_error
2828
expect(VCAP::CloudController::ServiceUsageEvent.count).to eq(0)
@@ -31,12 +31,12 @@
3131

3232
it 'only retrieves the current timestamp from the database once' do
3333
expect(VCAP::CloudController::Event.db).to receive(:fetch).with('SELECT CURRENT_TIMESTAMP as now').once.and_call_original
34-
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::Event, 1)
34+
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::Event, cutoff_age_in_days: 1)
3535
record_cleanup.delete
3636
end
3737

3838
it 'keeps the last row when :keep_at_least_one_record is true even if it is older than the cutoff date' do
39-
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::Event, 0, keep_at_least_one_record: true)
39+
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::Event, cutoff_age_in_days: 0, keep_at_least_one_record: true)
4040

4141
expect do
4242
record_cleanup.delete

0 commit comments

Comments
 (0)