discourse/app/models/draft_sequence.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-05 14:16:51 -05:00
class DraftSequence < ActiveRecord::Base
def self.next!(user, key)
return nil if !user
2013-02-05 14:16:51 -05:00
user_id = user
2017-04-15 00:11:02 -04:00
user_id = user.id unless user.is_a?(Integer)
return 0 if !User.human_user_id?(user_id)
sequence =
DB.query_single(<<~SQL, user_id: user_id, draft_key: key).first
INSERT INTO draft_sequences (user_id, draft_key, sequence)
VALUES (:user_id, :draft_key, 1)
ON CONFLICT (user_id, draft_key) DO
UPDATE
SET sequence = draft_sequences.sequence + 1
WHERE draft_sequences.user_id = :user_id
AND draft_sequences.draft_key = :draft_key
RETURNING sequence
SQL
DB.exec("DELETE FROM drafts WHERE user_id = :user_id AND draft_key = :draft_key AND sequence < :sequence", draft_key: key, user_id: user_id, sequence: sequence)
UserStat.update_draft_count(user_id)
sequence
2013-02-05 14:16:51 -05:00
end
def self.current(user, key)
return nil if !user
2013-02-05 14:16:51 -05:00
user_id = user
2017-04-15 00:11:02 -04:00
user_id = user.id unless user.is_a?(Integer)
2013-02-05 14:16:51 -05:00
return 0 if !User.human_user_id?(user_id)
2013-02-05 14:16:51 -05:00
# perf critical path
r, _ = DB.query_single('select sequence from draft_sequences where user_id = ? and draft_key = ?', user_id, key)
r.to_i
2013-02-05 14:16:51 -05:00
end
end
# == Schema Information
#
# Table name: draft_sequences
#
# id :integer not null, primary key
# user_id :integer not null
2019-01-11 14:29:56 -05:00
# draft_key :string not null
# sequence :bigint not null
#
# Indexes
#
# index_draft_sequences_on_user_id_and_draft_key (user_id,draft_key) UNIQUE
#