Avoid superfluous transaction when saving drafts
This commit is contained in:
parent
30bedff2c9
commit
5f8851cb44
|
@ -7,12 +7,10 @@ class Draft < ActiveRecord::Base
|
||||||
d = find_draft(user,key)
|
d = find_draft(user,key)
|
||||||
if d
|
if d
|
||||||
return if d.sequence > sequence
|
return if d.sequence > sequence
|
||||||
d.data = data
|
d.update_columns(data: data, sequence: sequence)
|
||||||
d.sequence = sequence
|
|
||||||
else
|
else
|
||||||
d = Draft.new(user_id: user.id, draft_key: key, data: data, sequence: sequence)
|
Draft.create!(user_id: user.id, draft_key: key, data: data, sequence: sequence)
|
||||||
end
|
end
|
||||||
d.save!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get(user, key, sequence)
|
def self.get(user, key, sequence)
|
||||||
|
|
|
@ -11,6 +11,20 @@ class ActiveRecord::Base
|
||||||
exec_sql(*args).cmd_tuples
|
exec_sql(*args).cmd_tuples
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# note: update_attributes still spins up a transaction this can cause contention
|
||||||
|
# this method performs the raw update sidestepping AR
|
||||||
|
def update_columns(hash)
|
||||||
|
sql = "UPDATE #{ self.class.table_name } SET "
|
||||||
|
|
||||||
|
sql << hash.map do |k,v|
|
||||||
|
"#{k} = :#{k}"
|
||||||
|
end.join(",")
|
||||||
|
|
||||||
|
sql << " WHERE id = :id"
|
||||||
|
|
||||||
|
exec_sql(sql, hash.merge(id: self.id))
|
||||||
|
end
|
||||||
|
|
||||||
def exec_sql(*args)
|
def exec_sql(*args)
|
||||||
ActiveRecord::Base.exec_sql(*args)
|
ActiveRecord::Base.exec_sql(*args)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue