FIX: leaving around useless drafts after posting

This commit is contained in:
Sam Saffron 2015-06-02 13:45:47 +10:00
parent d459cb95dc
commit 0fd1974838
3 changed files with 28 additions and 0 deletions

View File

@ -11,6 +11,7 @@ module Jobs
Post.calculate_avg_time Post.calculate_avg_time
Topic.calculate_avg_time Topic.calculate_avg_time
ScoreCalculator.new.calculate ScoreCalculator.new.calculate
Draft.cleanup!
end end
end end
end end

View File

@ -34,6 +34,15 @@ class Draft < ActiveRecord::Base
find_by(user_id: user, draft_key: key) find_by(user_id: user, draft_key: key)
end end
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 end
# == Schema Information # == Schema Information

View File

@ -34,6 +34,24 @@ describe Draft do
expect(Draft.get(@user, "test", 1)).to eq "hello" expect(Draft.get(@user, "test", 1)).to eq "hello"
end 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 context 'key expiry' do
it 'nukes new topic draft after a topic is created' do it 'nukes new topic draft after a topic is created' do