From 89b621d31a5cf75f8ba60cfb84623c6eae773ba1 Mon Sep 17 00:00:00 2001 From: Stephan Kaag Date: Mon, 1 Jul 2013 20:45:52 +0200 Subject: [PATCH] Refactor update_all statements in order to prevent deprecation warnings in Rails 4 --- app/models/email_log.rb | 2 +- app/models/email_token.rb | 4 ++-- app/models/invite_redeemer.rb | 7 +++--- app/models/notification.rb | 2 +- app/models/post.rb | 2 +- app/models/post_action.rb | 23 +++++++++---------- app/models/post_mover.rb | 6 ++--- app/models/post_timing.rb | 2 +- app/models/topic.rb | 6 ++--- app/models/topic_user.rb | 4 ++-- app/models/user.rb | 11 ++++----- app/models/user_action.rb | 4 ++-- app/models/view.rb | 2 +- app/services/spam_rules_enforcer.rb | 4 ++-- ...130621042855_change_supress_to_suppress.rb | 4 ++-- lib/post_destroyer.rb | 4 ++-- lib/site_settings/db_provider.rb | 6 ++--- lib/trashable.rb | 2 +- 18 files changed, 45 insertions(+), 50 deletions(-) diff --git a/app/models/email_log.rb b/app/models/email_log.rb index 2efbdaf01..3502c86d7 100644 --- a/app/models/email_log.rb +++ b/app/models/email_log.rb @@ -8,7 +8,7 @@ class EmailLog < ActiveRecord::Base after_create do # 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 def self.count_per_day(sinceDaysAgo = 30) diff --git a/app/models/email_token.rb b/app/models/email_token.rb index ad86416e6..4109ad9d7 100644 --- a/app/models/email_token.rb +++ b/app/models/email_token.rb @@ -11,7 +11,7 @@ class EmailToken < ActiveRecord::Base after_create do # 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 def self.token_length @@ -43,7 +43,7 @@ class EmailToken < ActiveRecord::Base user = email_token.user 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 we are activating the user, send the welcome message user.send_welcome_message = !user.active? diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb index 64533fa73..5e7f51fd4 100644 --- a/app/models/invite_redeemer.rb +++ b/app/models/invite_redeemer.rb @@ -27,9 +27,8 @@ InviteRedeemer = Struct.new(:invite) do end def mark_invite_redeemed - Invite.update_all('redeemed_at = CURRENT_TIMESTAMP', - ['id = ? AND redeemed_at IS NULL AND created_at >= ?', - invite.id, SiteSetting.invite_expiry_days.days.ago]) + Invite.where(['id = ? AND redeemed_at IS NULL AND created_at >= ?', + invite.id, SiteSetting.invite_expiry_days.days.ago]).update_all('redeemed_at = CURRENT_TIMESTAMP') end def get_invited_user @@ -62,7 +61,7 @@ InviteRedeemer = Struct.new(:invite) do end 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 end end diff --git a/app/models/notification.rb b/app/models/notification.rb index f5832fa58..4ea802174 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -32,7 +32,7 @@ class Notification < ActiveRecord::Base end 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 def self.interesting_after(min_date) diff --git a/app/models/post.rb b/app/models/post.rb index 3751b0949..b94b75eac 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -366,7 +366,7 @@ class Post < ActiveRecord::Base return if post.nil? post_reply = post.post_replies.new(reply_id: id) 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 diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 25a9acd46..a83cea166 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -63,9 +63,9 @@ class PostAction < ActiveRecord::Base moderator_id == -1 ? PostActionType.auto_action_flag_types.values : PostActionType.flag_types.values 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]} - 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 end @@ -224,19 +224,19 @@ class PostAction < ActiveRecord::Base case post_action_type when :vote # 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, - max: Topic.max_sort_order], id: post_id + max: Topic.max_sort_order] when :like # `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, - score_delta: user.staff? ? delta * SiteSetting.staff_like_weight : delta], id: post_id + score_delta: user.staff? ? delta * SiteSetting.staff_like_weight : delta] else - Post.update_all ["#{column} = #{column} + ?", delta], id: post_id + Post.where(id: post_id).update_all ["#{column} = #{column} + ?", delta] 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) @@ -271,10 +271,9 @@ class PostAction < ActiveRecord::Base reason = guess_hide_reason(old_flags) end - Post.update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", reason], id: post.id) - Topic.update_all({ visible: false }, - ["id = :topic_id AND NOT EXISTS(SELECT 1 FROM POSTS WHERE topic_id = :topic_id AND NOT hidden)", - topic_id: post.topic_id]) + Post.where(id: post.id).update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", reason]) + Topic.where(["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 }) # inform user if post.user diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb index 11f15de30..795539329 100644 --- a/app/models/post_mover.rb +++ b/app/models/post_mover.rb @@ -59,7 +59,7 @@ class PostMover def move(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', 'topic_id = :topic_id', @@ -67,9 +67,7 @@ class PostMover ].join(', '), post_number: post_number, topic_id: destination_topic.id - ], - id: post.id, - topic_id: original_topic.id + ] ) end diff --git a/app/models/post_timing.rb b/app/models/post_timing.rb index a6571a6b6..aad81a054 100644 --- a/app/models/post_timing.rb +++ b/app/models/post_timing.rb @@ -16,7 +16,7 @@ class PostTiming < ActiveRecord::Base args) 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) SELECT :topic_id, :user_id, :post_number, :msecs WHERE NOT EXISTS(SELECT 1 FROM post_timings diff --git a/app/models/topic.rb b/app/models/topic.rb index 771fc1cb9..2a1c0eddb 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -311,14 +311,14 @@ class Topic < ActiveRecord::Base old_category = category 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 self.category_id = cat.id save 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) end end @@ -354,7 +354,7 @@ class Topic < ActiveRecord::Base if name.blank? if category_id.present? 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 self.category_id = nil save diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 1db00d2d5..008196e89 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -87,7 +87,7 @@ class TopicUser < ActiveRecord::Base attrs_sql = attrs_array.map { |t| "#{t[0]} = ?" }.join(", ") 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 now = DateTime.now @@ -109,7 +109,7 @@ class TopicUser < ActiveRecord::Base def track_visit!(topic,user) 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 TopicUser.create(topic_id: topic.id, user_id: user.id, last_visited_at: now, first_visited_at: now) else diff --git a/app/models/user.rb b/app/models/user.rb index a9a45917e..1ae91f02e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -230,8 +230,7 @@ class User < ActiveRecord::Base end def saw_notification_id(notification_id) - User.update_all ["seen_notification_id = ?", notification_id], - ["seen_notification_id < ?", notification_id] + User.where(["seen_notification_id < ?", notification_id]).update_all ["seen_notification_id = ?", notification_id] end def publish_notifications_state @@ -461,7 +460,7 @@ class User < ActiveRecord::Base if last_seen.present? diff = (Time.now.to_f - last_seen.to_f).round 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 $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} 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 - TopicUser.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) + 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]]) end end diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 9c62f0dd4..fb0516e0a 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -209,9 +209,9 @@ ORDER BY p.created_at desc def self.update_like_count(user_id, action_type, delta) 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 - 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 diff --git a/app/models/view.rb b/app/models/view.rb index efe6423e5..f1da903f4 100644 --- a/app/models/view.rb +++ b/app/models/view.rb @@ -24,7 +24,7 @@ class View < ActiveRecord::Base # Update the views count in the parent, if it exists. 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 diff --git a/app/services/spam_rules_enforcer.rb b/app/services/spam_rules_enforcer.rb index afda275c3..a34e97c79 100644 --- a/app/services/spam_rules_enforcer.rb +++ b/app/services/spam_rules_enforcer.rb @@ -52,9 +52,9 @@ class SpamRulesEnforcer def punish_user 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.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) GroupMessage.create(Group[:moderators].name, :user_automatically_blocked, {user: @user, limit_once_per: false}) @user.blocked = true diff --git a/db/migrate/20130621042855_change_supress_to_suppress.rb b/db/migrate/20130621042855_change_supress_to_suppress.rb index fb917fa57..1e8f497b0 100644 --- a/db/migrate/20130621042855_change_supress_to_suppress.rb +++ b/db/migrate/20130621042855_change_supress_to_suppress.rb @@ -1,9 +1,9 @@ class ChangeSupressToSuppress < ActiveRecord::Migration 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 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 diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index f39ad7caf..e15e501e4 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -30,7 +30,7 @@ class PostDestroyer # 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]) - 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 @@ -40,7 +40,7 @@ class PostDestroyer @post.post_actions.map(&:trash!) 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! diff --git a/lib/site_settings/db_provider.rb b/lib/site_settings/db_provider.rb index bf2a3c368..21a5ea3b0 100644 --- a/lib/site_settings/db_provider.rb +++ b/lib/site_settings/db_provider.rb @@ -28,13 +28,13 @@ class SiteSettings::DbProvider return unless table_exists? - count = @model.update_all({ + count = @model.where({ + name: name + }).update_all({ name: name, value: value, data_type: data_type, updated_at: Time.now - }, { - name: name }) if count == 0 diff --git a/lib/trashable.rb b/lib/trashable.rb index eabc48824..13767b5d2 100644 --- a/lib/trashable.rb +++ b/lib/trashable.rb @@ -39,7 +39,7 @@ module Trashable # # 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 end