diff --git a/app/jobs/scheduled/weekly.rb b/app/jobs/scheduled/weekly.rb index 156cfd74e45..613833ff514 100644 --- a/app/jobs/scheduled/weekly.rb +++ b/app/jobs/scheduled/weekly.rb @@ -11,6 +11,7 @@ module Jobs Post.calculate_avg_time Topic.calculate_avg_time ScoreCalculator.new.calculate + Draft.cleanup! end end end diff --git a/app/models/draft.rb b/app/models/draft.rb index e4ab1480df7..efc098984e4 100644 --- a/app/models/draft.rb +++ b/app/models/draft.rb @@ -34,6 +34,15 @@ class Draft < ActiveRecord::Base find_by(user_id: user, draft_key: key) end end + + def self.cleanup! + exec_sql("DELETE FROM drafts where sequence < ( + SELECT max(s.sequence) from draft_sequences s + WHERE s.draft_key = drafts.draft_key AND + s.user_id = drafts.user_id + )") + end + end # == Schema Information diff --git a/spec/models/draft_spec.rb b/spec/models/draft_spec.rb index 0f5f555c2b8..c910245ecf4 100644 --- a/spec/models/draft_spec.rb +++ b/spec/models/draft_spec.rb @@ -34,6 +34,24 @@ describe Draft do expect(Draft.get(@user, "test", 1)).to eq "hello" end + it 'can cleanup old' do + user = Fabricate(:user) + key = Draft::NEW_TOPIC + seq = DraftSequence.next!(user, key) + + Draft.set(user,key,seq,'draft') + DraftSequence.update_all('sequence = sequence + 1') + + Draft.cleanup! + + expect(Draft.count).to eq 0 + Draft.set(Fabricate(:user), Draft::NEW_TOPIC, seq+1, 'draft') + + Draft.cleanup! + + expect(Draft.count).to eq 1 + end + context 'key expiry' do it 'nukes new topic draft after a topic is created' do