Skip to content

A platform for community discussion. Free, open, simple.

FIX: certain edge cases could leave phantom drafts behind (#37130)

Samc395827f237e
diff --git a/app/models/draft.rb b/app/models/draft.rb
index e39a4d2fd08..02572798bb9 100644
--- a/app/models/draft.rb
+++ b/app/models/draft.rb
@@ -222,7 +222,20 @@ class Draft < ActiveRecord::Base
offset = (opts[:offset] || 0).to_i
limit = (opts[:limit] || 30).to_i
- stream = Draft.where(user_id: user_id).order(updated_at: :desc).offset(offset).limit(limit)
+ stream =
+ Draft
+ .where(user_id: user_id)
+ .where(<<~SQL)
+ sequence >= COALESCE(
+ (SELECT sequence FROM draft_sequences
+ WHERE draft_sequences.user_id = drafts.user_id
+ AND draft_sequences.draft_key = drafts.draft_key),
+ 0
+ )
+ SQL
+ .order(updated_at: :desc)
+ .offset(offset)
+ .limit(limit)
# Preload posts and topics to avoid N+1 queries
Draft.preload_data(stream, opts[:user])
diff --git a/app/models/draft_sequence.rb b/app/models/draft_sequence.rb
index 8a3529ce982..c33bd552568 100644
--- a/app/models/draft_sequence.rb
+++ b/app/models/draft_sequence.rb
@@ -20,7 +20,7 @@ class DraftSequence < ActiveRecord::Base
RETURNING sequence
SQL
- Draft.where(user_id: user_id).where(draft_key: key).where("sequence < ?", sequence).destroy_all
+ Draft.where(user_id: user_id, draft_key: key).destroy_all
UserStat.update_draft_count(user_id)
diff --git a/spec/lib/post_creator_spec.rb b/spec/lib/post_creator_spec.rb
index b14a0cd83f4..b6e0de28408 100644
--- a/spec/lib/post_creator_spec.rb
+++ b/spec/lib/post_creator_spec.rb
@@ -2225,4 +2225,43 @@ RSpec.describe PostCreator do
expect { reviewable.perform(admin, :approve_post) }.not_to change(ReviewablePost, :count)
end
end
+
+ describe "draft cleanup" do
+ it "deletes the draft after successful post creation" do
+ topic = Fabricate(:topic)
+ draft_key = topic.draft_key
+ Draft.set(user, draft_key, 0, '{"reply":"test draft"}')
+
+ expect(Draft.find_by(user_id: user.id, draft_key: draft_key)).to be_present
+
+ PostCreator.create(
+ user,
+ topic_id: topic.id,
+ raw: "reply content for testing",
+ advance_draft: true,
+ )
+
+ expect(Draft.find_by(user_id: user.id, draft_key: draft_key)).to be_nil
+ end
+
+ it "deletes the draft even when draft sequence exceeds DraftSequence" do
+ topic = Fabricate(:topic)
+ draft_key = topic.draft_key
+
+ # Simulate edge case: draft sequence is higher than DraftSequence
+ Draft.create!(user: user, draft_key: draft_key, data: '{"reply":"test"}', sequence: 6)
+ DraftSequence.create!(user_id: user.id, draft_key: draft_key, sequence: 5)
+
+ expect(Draft.find_by(user_id: user.id, draft_key: draft_key)).to be_present
+
+ PostCreator.create(
+ user,
+ topic_id: topic.id,
+ raw: "reply content for testing",
+ advance_draft: true,
+ )
+
+ expect(Draft.find_by(user_id: user.id, draft_key: draft_key)).to be_nil
+ end
+ end
end
diff --git a/spec/lib/post_revisor_spec.rb b/spec/lib/post_revisor_spec.rb
index 16d51f705b8..031eb634940 100644
--- a/spec/lib/post_revisor_spec.rb
+++ b/spec/lib/post_revisor_spec.rb
@@ -1850,4 +1850,34 @@ describe PostRevisor do
end
end
end
+
+ describe "draft cleanup" do
+ fab!(:post)
+
+ it "deletes the draft after successful revision" do
+ draft_key = post.topic.draft_key
+ Draft.set(post.user, draft_key, 0, '{"reply":"test draft"}')
+
+ expect(Draft.find_by(user_id: post.user.id, draft_key: draft_key)).to be_present
+
+ post.revise(post.user, raw: "updated content here for the test")
+
+ expect(Draft.find_by(user_id: post.user.id, draft_key: draft_key)).to be_nil
+ end
+
+ it "deletes the draft even when draft sequence exceeds DraftSequence" do
+ draft_key = post.topic.draft_key
+
+ # Simulate edge case: draft sequence is higher than DraftSequence
+ # When DraftSequence.next! runs, it increments to 6, but sequence < 6 doesn't catch sequence 6
+ Draft.create!(user: post.user, draft_key: draft_key, data: '{"reply":"test"}', sequence: 6)
+ DraftSequence.create!(user_id: post.user.id, draft_key: draft_key, sequence: 5)
+
+ expect(Draft.find_by(user_id: post.user.id, draft_key: draft_key)).to be_present
+
+ post.revise(post.user, raw: "updated content here for the test")
+
+ expect(Draft.find_by(user_id: post.user.id, draft_key: draft_key)).to be_nil
+ end
+ end
end
diff --git a/spec/models/draft_spec.rb b/spec/models/draft_spec.rb
index eeae283ec03..18286de9989 100644
--- a/spec/models/draft_spec.rb
+++ b/spec/models/draft_spec.rb
@@ -210,6 +210,17 @@ RSpec.describe Draft do
draft_row = stream.first
expect(draft_row.user.username).to eq(user.username)
end
+
+ it "does not return drafts with stale sequences" do
+ draft_key = "topic_#{public_topic.id}"
+
+ # Create a draft and advance the sequence to make it stale
+ # We insert directly to simulate an edge case where the draft wasn't cleaned up
+ Draft.create!(user: user, draft_key: draft_key, data: '{"reply":"stale draft"}', sequence: 0)
+ DraftSequence.create!(user_id: user.id, draft_key: draft_key, sequence: 5)
+
+ expect(stream).to be_empty
+ end
end
describe "multiple drafts" do