mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-30 10:58:31 -05:00
Refactor update_all statements in order to prevent deprecation warnings in Rails 4
This commit is contained in:
parent
5770879472
commit
89b621d31a
18 changed files with 45 additions and 50 deletions
|
@ -8,7 +8,7 @@ class EmailLog < ActiveRecord::Base
|
||||||
|
|
||||||
after_create do
|
after_create do
|
||||||
# Update last_emailed_at if the user_id is present
|
# Update last_emailed_at if the user_id is present
|
||||||
User.update_all("last_emailed_at = CURRENT_TIMESTAMP", id: user_id) if user_id.present?
|
User.where(id: user_id).update_all("last_emailed_at = CURRENT_TIMESTAMP") if user_id.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.count_per_day(sinceDaysAgo = 30)
|
def self.count_per_day(sinceDaysAgo = 30)
|
||||||
|
|
|
@ -11,7 +11,7 @@ class EmailToken < ActiveRecord::Base
|
||||||
|
|
||||||
after_create do
|
after_create do
|
||||||
# Expire the previous tokens
|
# Expire the previous tokens
|
||||||
EmailToken.update_all 'expired = true', ['user_id = ? and id != ?', self.user_id, self.id]
|
EmailToken.where(['user_id = ? and id != ?', self.user_id, self.id]).update_all 'expired = true'
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.token_length
|
def self.token_length
|
||||||
|
@ -43,7 +43,7 @@ class EmailToken < ActiveRecord::Base
|
||||||
|
|
||||||
user = email_token.user
|
user = email_token.user
|
||||||
User.transaction do
|
User.transaction do
|
||||||
row_count = EmailToken.update_all 'confirmed = true', id: email_token.id, expired: false
|
row_count = EmailToken.where(id: email_token.id, expired: false).update_all 'confirmed = true'
|
||||||
if row_count == 1
|
if row_count == 1
|
||||||
# If we are activating the user, send the welcome message
|
# If we are activating the user, send the welcome message
|
||||||
user.send_welcome_message = !user.active?
|
user.send_welcome_message = !user.active?
|
||||||
|
|
|
@ -27,9 +27,8 @@ InviteRedeemer = Struct.new(:invite) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def mark_invite_redeemed
|
def mark_invite_redeemed
|
||||||
Invite.update_all('redeemed_at = CURRENT_TIMESTAMP',
|
Invite.where(['id = ? AND redeemed_at IS NULL AND created_at >= ?',
|
||||||
['id = ? AND redeemed_at IS NULL AND created_at >= ?',
|
invite.id, SiteSetting.invite_expiry_days.days.ago]).update_all('redeemed_at = CURRENT_TIMESTAMP')
|
||||||
invite.id, SiteSetting.invite_expiry_days.days.ago])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_invited_user
|
def get_invited_user
|
||||||
|
@ -62,7 +61,7 @@ InviteRedeemer = Struct.new(:invite) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_welcome_message
|
def send_welcome_message
|
||||||
if Invite.update_all(['user_id = ?', invited_user.id], ['email = ?', invite.email]) == 1
|
if Invite.where(['email = ?', invite.email]).update_all(['user_id = ?', invited_user.id]) == 1
|
||||||
invited_user.send_welcome_message = true
|
invited_user.send_welcome_message = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,7 @@ class Notification < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.mark_posts_read(user, topic_id, post_numbers)
|
def self.mark_posts_read(user, topic_id, post_numbers)
|
||||||
Notification.update_all "read = 't'", user_id: user.id, topic_id: topic_id, post_number: post_numbers, read: false
|
Notification.where(user_id: user.id, topic_id: topic_id, post_number: post_numbers, read: false).update_all "read = 't'"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.interesting_after(min_date)
|
def self.interesting_after(min_date)
|
||||||
|
|
|
@ -366,7 +366,7 @@ class Post < ActiveRecord::Base
|
||||||
return if post.nil?
|
return if post.nil?
|
||||||
post_reply = post.post_replies.new(reply_id: id)
|
post_reply = post.post_replies.new(reply_id: id)
|
||||||
if post_reply.save
|
if post_reply.save
|
||||||
Post.update_all ['reply_count = reply_count + 1'], id: post.id
|
Post.where(id: post.id).update_all ['reply_count = reply_count + 1']
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -63,9 +63,9 @@ class PostAction < ActiveRecord::Base
|
||||||
moderator_id == -1 ? PostActionType.auto_action_flag_types.values : PostActionType.flag_types.values
|
moderator_id == -1 ? PostActionType.auto_action_flag_types.values : PostActionType.flag_types.values
|
||||||
end
|
end
|
||||||
|
|
||||||
PostAction.update_all({ deleted_at: Time.zone.now, deleted_by: moderator_id }, { post_id: post.id, post_action_type_id: actions })
|
PostAction.where({ post_id: post.id, post_action_type_id: actions }).update_all({ deleted_at: Time.zone.now, deleted_by: moderator_id })
|
||||||
f = actions.map{|t| ["#{PostActionType.types[t]}_count", 0]}
|
f = actions.map{|t| ["#{PostActionType.types[t]}_count", 0]}
|
||||||
Post.with_deleted.update_all(Hash[*f.flatten], id: post.id)
|
Post.where(id: post.id).with_deleted.update_all(Hash[*f.flatten])
|
||||||
update_flagged_posts_count
|
update_flagged_posts_count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -224,19 +224,19 @@ class PostAction < ActiveRecord::Base
|
||||||
case post_action_type
|
case post_action_type
|
||||||
when :vote
|
when :vote
|
||||||
# Voting also changes the sort_order
|
# Voting also changes the sort_order
|
||||||
Post.update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)",
|
Post.where(id: post_id).update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)",
|
||||||
delta: delta,
|
delta: delta,
|
||||||
max: Topic.max_sort_order], id: post_id
|
max: Topic.max_sort_order]
|
||||||
when :like
|
when :like
|
||||||
# `like_score` is weighted higher for staff accounts
|
# `like_score` is weighted higher for staff accounts
|
||||||
Post.update_all ["like_count = like_count + :delta, like_score = like_score + :score_delta",
|
Post.where(id: post_id).update_all ["like_count = like_count + :delta, like_score = like_score + :score_delta",
|
||||||
delta: delta,
|
delta: delta,
|
||||||
score_delta: user.staff? ? delta * SiteSetting.staff_like_weight : delta], id: post_id
|
score_delta: user.staff? ? delta * SiteSetting.staff_like_weight : delta]
|
||||||
else
|
else
|
||||||
Post.update_all ["#{column} = #{column} + ?", delta], id: post_id
|
Post.where(id: post_id).update_all ["#{column} = #{column} + ?", delta]
|
||||||
end
|
end
|
||||||
|
|
||||||
Topic.update_all ["#{column} = #{column} + ?", delta], id: post.topic_id
|
Topic.where(id: post.topic_id).update_all ["#{column} = #{column} + ?", delta]
|
||||||
|
|
||||||
|
|
||||||
if PostActionType.notify_flag_type_ids.include?(post_action_type_id)
|
if PostActionType.notify_flag_type_ids.include?(post_action_type_id)
|
||||||
|
@ -271,10 +271,9 @@ class PostAction < ActiveRecord::Base
|
||||||
reason = guess_hide_reason(old_flags)
|
reason = guess_hide_reason(old_flags)
|
||||||
end
|
end
|
||||||
|
|
||||||
Post.update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", reason], id: post.id)
|
Post.where(id: post.id).update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", reason])
|
||||||
Topic.update_all({ visible: false },
|
Topic.where(["id = :topic_id AND NOT EXISTS(SELECT 1 FROM POSTS WHERE topic_id = :topic_id AND NOT hidden)",
|
||||||
["id = :topic_id AND NOT EXISTS(SELECT 1 FROM POSTS WHERE topic_id = :topic_id AND NOT hidden)",
|
topic_id: post.topic_id]).update_all({ visible: false })
|
||||||
topic_id: post.topic_id])
|
|
||||||
|
|
||||||
# inform user
|
# inform user
|
||||||
if post.user
|
if post.user
|
||||||
|
|
|
@ -59,7 +59,7 @@ class PostMover
|
||||||
def move(post, post_number)
|
def move(post, post_number)
|
||||||
@first_post_number_moved ||= post.post_number
|
@first_post_number_moved ||= post.post_number
|
||||||
|
|
||||||
Post.update_all(
|
Post.where(id: post.id, topic_id: original_topic.id).update_all(
|
||||||
[
|
[
|
||||||
['post_number = :post_number',
|
['post_number = :post_number',
|
||||||
'topic_id = :topic_id',
|
'topic_id = :topic_id',
|
||||||
|
@ -67,9 +67,7 @@ class PostMover
|
||||||
].join(', '),
|
].join(', '),
|
||||||
post_number: post_number,
|
post_number: post_number,
|
||||||
topic_id: destination_topic.id
|
topic_id: destination_topic.id
|
||||||
],
|
]
|
||||||
id: post.id,
|
|
||||||
topic_id: original_topic.id
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ class PostTiming < ActiveRecord::Base
|
||||||
args)
|
args)
|
||||||
|
|
||||||
if rows == 0
|
if rows == 0
|
||||||
Post.update_all 'reads = reads + 1', ['topic_id = :topic_id and post_number = :post_number', args]
|
Post.where(['topic_id = :topic_id and post_number = :post_number', args]).update_all 'reads = reads + 1'
|
||||||
exec_sql("INSERT INTO post_timings (topic_id, user_id, post_number, msecs)
|
exec_sql("INSERT INTO post_timings (topic_id, user_id, post_number, msecs)
|
||||||
SELECT :topic_id, :user_id, :post_number, :msecs
|
SELECT :topic_id, :user_id, :post_number, :msecs
|
||||||
WHERE NOT EXISTS(SELECT 1 FROM post_timings
|
WHERE NOT EXISTS(SELECT 1 FROM post_timings
|
||||||
|
|
|
@ -311,14 +311,14 @@ class Topic < ActiveRecord::Base
|
||||||
old_category = category
|
old_category = category
|
||||||
|
|
||||||
if category_id.present? && category_id != cat.id
|
if category_id.present? && category_id != cat.id
|
||||||
Category.update_all 'topic_count = topic_count - 1', ['id = ?', category_id]
|
Category.where(['id = ?', category_id]).update_all 'topic_count = topic_count - 1'
|
||||||
end
|
end
|
||||||
|
|
||||||
self.category_id = cat.id
|
self.category_id = cat.id
|
||||||
save
|
save
|
||||||
|
|
||||||
CategoryFeaturedTopic.feature_topics_for(old_category)
|
CategoryFeaturedTopic.feature_topics_for(old_category)
|
||||||
Category.update_all 'topic_count = topic_count + 1', id: cat.id
|
Category.where(id: cat.id).update_all 'topic_count = topic_count + 1'
|
||||||
CategoryFeaturedTopic.feature_topics_for(cat) unless old_category.try(:id) == cat.try(:id)
|
CategoryFeaturedTopic.feature_topics_for(cat) unless old_category.try(:id) == cat.try(:id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -354,7 +354,7 @@ class Topic < ActiveRecord::Base
|
||||||
if name.blank?
|
if name.blank?
|
||||||
if category_id.present?
|
if category_id.present?
|
||||||
CategoryFeaturedTopic.feature_topics_for(category)
|
CategoryFeaturedTopic.feature_topics_for(category)
|
||||||
Category.update_all 'topic_count = topic_count - 1', id: category_id
|
Category.where(id: category_id).update_all 'topic_count = topic_count - 1'
|
||||||
end
|
end
|
||||||
self.category_id = nil
|
self.category_id = nil
|
||||||
save
|
save
|
||||||
|
|
|
@ -87,7 +87,7 @@ class TopicUser < ActiveRecord::Base
|
||||||
|
|
||||||
attrs_sql = attrs_array.map { |t| "#{t[0]} = ?" }.join(", ")
|
attrs_sql = attrs_array.map { |t| "#{t[0]} = ?" }.join(", ")
|
||||||
vals = attrs_array.map { |t| t[1] }
|
vals = attrs_array.map { |t| t[1] }
|
||||||
rows = TopicUser.update_all([attrs_sql, *vals], topic_id: topic_id, user_id: user_id)
|
rows = TopicUser.where(topic_id: topic_id, user_id: user_id).update_all([attrs_sql, *vals])
|
||||||
|
|
||||||
if rows == 0
|
if rows == 0
|
||||||
now = DateTime.now
|
now = DateTime.now
|
||||||
|
@ -109,7 +109,7 @@ class TopicUser < ActiveRecord::Base
|
||||||
|
|
||||||
def track_visit!(topic,user)
|
def track_visit!(topic,user)
|
||||||
now = DateTime.now
|
now = DateTime.now
|
||||||
rows = TopicUser.update_all({last_visited_at: now}, {topic_id: topic.id, user_id: user.id})
|
rows = TopicUser.where({topic_id: topic.id, user_id: user.id}).update_all({last_visited_at: now})
|
||||||
if rows == 0
|
if rows == 0
|
||||||
TopicUser.create(topic_id: topic.id, user_id: user.id, last_visited_at: now, first_visited_at: now)
|
TopicUser.create(topic_id: topic.id, user_id: user.id, last_visited_at: now, first_visited_at: now)
|
||||||
else
|
else
|
||||||
|
|
|
@ -230,8 +230,7 @@ class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def saw_notification_id(notification_id)
|
def saw_notification_id(notification_id)
|
||||||
User.update_all ["seen_notification_id = ?", notification_id],
|
User.where(["seen_notification_id < ?", notification_id]).update_all ["seen_notification_id = ?", notification_id]
|
||||||
["seen_notification_id < ?", notification_id]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def publish_notifications_state
|
def publish_notifications_state
|
||||||
|
@ -461,7 +460,7 @@ class User < ActiveRecord::Base
|
||||||
if last_seen.present?
|
if last_seen.present?
|
||||||
diff = (Time.now.to_f - last_seen.to_f).round
|
diff = (Time.now.to_f - last_seen.to_f).round
|
||||||
if diff > 0 && diff < MAX_TIME_READ_DIFF
|
if diff > 0 && diff < MAX_TIME_READ_DIFF
|
||||||
User.update_all ["time_read = time_read + ?", diff], id: id, time_read: time_read
|
User.where(id: id, time_read: time_read).update_all ["time_read = time_read + ?", diff]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
$redis.set(last_seen_key, Time.now.to_f)
|
$redis.set(last_seen_key, Time.now.to_f)
|
||||||
|
@ -531,10 +530,10 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
where_conditions = {notifications_reason_id: nil, user_id: id}
|
where_conditions = {notifications_reason_id: nil, user_id: id}
|
||||||
if auto_track_topics_after_msecs < 0
|
if auto_track_topics_after_msecs < 0
|
||||||
TopicUser.update_all({notification_level: TopicUser.notification_levels[:regular]}, where_conditions)
|
TopicUser.where(where_conditions).update_all({notification_level: TopicUser.notification_levels[:regular]})
|
||||||
else
|
else
|
||||||
TopicUser.update_all(["notification_level = CASE WHEN total_msecs_viewed < ? THEN ? ELSE ? END",
|
TopicUser.where(where_conditions).update_all(["notification_level = CASE WHEN total_msecs_viewed < ? THEN ? ELSE ? END",
|
||||||
auto_track_topics_after_msecs, TopicUser.notification_levels[:regular], TopicUser.notification_levels[:tracking]], where_conditions)
|
auto_track_topics_after_msecs, TopicUser.notification_levels[:regular], TopicUser.notification_levels[:tracking]])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -209,9 +209,9 @@ ORDER BY p.created_at desc
|
||||||
|
|
||||||
def self.update_like_count(user_id, action_type, delta)
|
def self.update_like_count(user_id, action_type, delta)
|
||||||
if action_type == LIKE
|
if action_type == LIKE
|
||||||
User.update_all("likes_given = likes_given + #{delta.to_i}", id: user_id)
|
User.where(id: user_id).update_all("likes_given = likes_given + #{delta.to_i}")
|
||||||
elsif action_type == WAS_LIKED
|
elsif action_type == WAS_LIKED
|
||||||
User.update_all("likes_received = likes_received + #{delta.to_i}", id: user_id)
|
User.where(id: user_id).update_all("likes_received = likes_received + #{delta.to_i}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ class View < ActiveRecord::Base
|
||||||
|
|
||||||
# Update the views count in the parent, if it exists.
|
# Update the views count in the parent, if it exists.
|
||||||
if parent.respond_to?(:views)
|
if parent.respond_to?(:views)
|
||||||
parent.class.update_all 'views = views + 1', id: parent.id
|
parent.class.where(id: parent.id).update_all 'views = views + 1'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -52,9 +52,9 @@ class SpamRulesEnforcer
|
||||||
|
|
||||||
def punish_user
|
def punish_user
|
||||||
Post.transaction do
|
Post.transaction do
|
||||||
Post.update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Post.hidden_reasons[:new_user_spam_threshold_reached]], user_id: @user.id)
|
Post.where(user_id: @user.id).update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Post.hidden_reasons[:new_user_spam_threshold_reached]])
|
||||||
topic_ids = Post.where('user_id = ? and post_number = ?', @user.id, 1).pluck(:topic_id)
|
topic_ids = Post.where('user_id = ? and post_number = ?', @user.id, 1).pluck(:topic_id)
|
||||||
Topic.update_all({ visible: false }, id: topic_ids) unless topic_ids.empty?
|
Topic.where(id: topic_ids).update_all({ visible: false }) unless topic_ids.empty?
|
||||||
SystemMessage.create(@user, :too_many_spam_flags)
|
SystemMessage.create(@user, :too_many_spam_flags)
|
||||||
GroupMessage.create(Group[:moderators].name, :user_automatically_blocked, {user: @user, limit_once_per: false})
|
GroupMessage.create(Group[:moderators].name, :user_automatically_blocked, {user: @user, limit_once_per: false})
|
||||||
@user.blocked = true
|
@user.blocked = true
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
class ChangeSupressToSuppress < ActiveRecord::Migration
|
class ChangeSupressToSuppress < ActiveRecord::Migration
|
||||||
def up
|
def up
|
||||||
SiteSetting.update_all({name: "supress_reply_directly_below"}, name: "suppress_reply_directly_below")
|
SiteSetting.where(name: "suppress_reply_directly_below").update_all({name: "supress_reply_directly_below"})
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
SiteSetting.update_all({name: "suppress_reply_directly_below"}, name: "supress_reply_directly_below")
|
SiteSetting.where(name: "supress_reply_directly_below").update_all({name: "suppress_reply_directly_below"})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,7 +30,7 @@ class PostDestroyer
|
||||||
|
|
||||||
# If the poster doesn't have any other posts in the topic, clear their posted flag
|
# If the poster doesn't have any other posts in the topic, clear their posted flag
|
||||||
unless Post.exists?(["topic_id = ? and user_id = ? and id <> ?", @post.topic_id, @post.user_id, @post.id])
|
unless Post.exists?(["topic_id = ? and user_id = ? and id <> ?", @post.topic_id, @post.user_id, @post.id])
|
||||||
TopicUser.update_all 'posted = false', topic_id: @post.topic_id, user_id: @post.user_id
|
TopicUser.where(topic_id: @post.topic_id, user_id: @post.user_id).update_all 'posted = false'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ class PostDestroyer
|
||||||
@post.post_actions.map(&:trash!)
|
@post.post_actions.map(&:trash!)
|
||||||
|
|
||||||
f = PostActionType.types.map{|k,v| ["#{k}_count", 0]}
|
f = PostActionType.types.map{|k,v| ["#{k}_count", 0]}
|
||||||
Post.with_deleted.update_all(Hash[*f.flatten], id: @post.id)
|
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])
|
||||||
|
|
||||||
@post.trash!
|
@post.trash!
|
||||||
|
|
||||||
|
|
|
@ -28,13 +28,13 @@ class SiteSettings::DbProvider
|
||||||
|
|
||||||
return unless table_exists?
|
return unless table_exists?
|
||||||
|
|
||||||
count = @model.update_all({
|
count = @model.where({
|
||||||
|
name: name
|
||||||
|
}).update_all({
|
||||||
name: name,
|
name: name,
|
||||||
value: value,
|
value: value,
|
||||||
data_type: data_type,
|
data_type: data_type,
|
||||||
updated_at: Time.now
|
updated_at: Time.now
|
||||||
}, {
|
|
||||||
name: name
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if count == 0
|
if count == 0
|
||||||
|
|
|
@ -39,7 +39,7 @@ module Trashable
|
||||||
#
|
#
|
||||||
# Fixed in Rails 4
|
# Fixed in Rails 4
|
||||||
#
|
#
|
||||||
self.class.unscoped.update_all({deleted_at: nil}, id: self.id)
|
self.class.unscoped.where(id: self.id).update_all({deleted_at: nil})
|
||||||
raw_write_attribute :deleted_at, nil
|
raw_write_attribute :deleted_at, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue