From 0aed2533ac4c672787fa7d010a75e427d946d030 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 26 May 2017 09:04:13 -0400 Subject: [PATCH] Revert unread optimisation, has too many edge cases --- .../scheduled/update_first_topic_unread_at.rb | 9 ---- app/models/topic.rb | 4 -- app/models/topic_user.rb | 13 ------ app/models/user_stat.rb | 24 ----------- db/fixtures/999_topics.rb | 15 ++++++- ...70524182846_add_unread_tracking_columns.rb | 43 +------------------ ...0526125321_drop_unread_tracking_columns.rb | 8 ++++ lib/post_creator.rb | 6 +-- lib/topic_query.rb | 1 - spec/components/post_creator_spec.rb | 4 -- spec/models/topic_user_spec.rb | 30 ------------- spec/models/user_stat_spec.rb | 28 ------------ 12 files changed, 26 insertions(+), 159 deletions(-) delete mode 100644 app/jobs/scheduled/update_first_topic_unread_at.rb create mode 100644 db/migrate/20170526125321_drop_unread_tracking_columns.rb diff --git a/app/jobs/scheduled/update_first_topic_unread_at.rb b/app/jobs/scheduled/update_first_topic_unread_at.rb deleted file mode 100644 index c5b34156334..00000000000 --- a/app/jobs/scheduled/update_first_topic_unread_at.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Jobs - class UpdateFirstTopicUnreadAt < Jobs::Scheduled - every 1.day - - def execute(args) - UserStat.update_first_topic_unread_at! - end - end -end diff --git a/app/models/topic.rb b/app/models/topic.rb index c3fbbf98937..7f5b34b3988 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -541,10 +541,6 @@ SQL def self.reset_highest(topic_id) result = exec_sql "UPDATE topics SET - last_unread_at = ( - SELECT MAX(created_at) FROM posts - WHERE topic_id = :topic_id - ), highest_staff_post_number = ( SELECT COALESCE(MAX(post_number), 0) FROM posts WHERE topic_id = :topic_id AND diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 3272fa8f189..924d012bca8 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -146,19 +146,6 @@ SQL message[:notifications_reason_id] = reason_id if reason_id MessageBus.publish("/topic/#{topic_id}", message, user_ids: [user_id]) - if notification_level && notification_level >= notification_levels[:tracking] - sql = < 1 AND last_read_post_number < CASE WHEN moderator OR admin - THEN t.highest_staff_post_number - ELSE t.highest_post_number - END - AND t.deleted_at IS NULL - GROUP BY u.id - ) X - WHERE X.user_id = us.user_id AND X.first_unread_at <> first_topic_unread_at -SQL - - nil - end - protected def trigger_badges diff --git a/db/fixtures/999_topics.rb b/db/fixtures/999_topics.rb index 9587290a228..abdc372e8a4 100644 --- a/db/fixtures/999_topics.rb +++ b/db/fixtures/999_topics.rb @@ -68,15 +68,28 @@ end # run this later, cause we need to make sure new application controller resilience is in place first +ColumnDropper.drop( + table: 'user_stats', + after_migration: 'DropUnreadTrackingColumns', + columns: %w{ + first_topic_unread_at + }, + on_drop: ->(){ + STDERR.puts "Removing superflous user stats columns!" + ActiveRecord::Base.exec_sql "DROP FUNCTION IF EXISTS first_unread_topic_for(int)" + } +) + ColumnDropper.drop( table: 'topics', - after_migration: 'AddTopicColumnsBack', + after_migration: 'DropUnreadTrackingColumns', columns: %w{ inappropriate_count bookmark_count off_topic_count illegal_count notify_user_count + last_unread_at }, on_drop: ->(){ STDERR.puts "Removing superflous topic columns!" diff --git a/db/migrate/20170524182846_add_unread_tracking_columns.rb b/db/migrate/20170524182846_add_unread_tracking_columns.rb index 3f326140487..28cb3d7d251 100644 --- a/db/migrate/20170524182846_add_unread_tracking_columns.rb +++ b/db/migrate/20170524182846_add_unread_tracking_columns.rb @@ -1,48 +1,9 @@ class AddUnreadTrackingColumns < ActiveRecord::Migration def up - add_column :user_stats, :first_topic_unread_at, :datetime, null: false, default: "epoch" - add_column :topics, :last_unread_at, :datetime, null: false, default: "epoch" - - execute < 1 AND last_read_post_number < CASE WHEN moderator OR admin - THEN topics.highest_staff_post_number - ELSE topics.highest_post_number - END - AND topics.deleted_at IS NULL - ), current_timestamp) -SQL - - add_index :topics, [:last_unread_at] - - # we need this function for performance reasons - execute <= first_unread_topic_for(?)", user_id) .where("COALESCE(tu.notification_level, :regular) >= :tracking", regular: TopicUser.notification_levels[:regular], tracking: TopicUser.notification_levels[:tracking]) end diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 45828924b8a..dcfecfe83fb 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -411,12 +411,9 @@ describe PostCreator do expect(topic.posts_count).to eq(1) expect(topic.highest_staff_post_number).to eq(3) - expect(topic.last_unread_at).to eq(whisper_reply.created_at) - topic.update_columns(highest_staff_post_number:0, highest_post_number:0, posts_count: 0, - last_unread_at: 1.year.ago, last_posted_at: 1.year.ago) Topic.reset_highest(topic.id) @@ -426,7 +423,6 @@ describe PostCreator do expect(topic.posts_count).to eq(1) expect(topic.last_posted_at).to eq(first.created_at) expect(topic.highest_staff_post_number).to eq(3) - expect(topic.last_unread_at).to eq(whisper_reply.created_at) end end diff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rb index 97e41cff75e..bcc481c9c85 100644 --- a/spec/models/topic_user_spec.rb +++ b/spec/models/topic_user_spec.rb @@ -38,36 +38,6 @@ describe TopicUser do end - describe "first unread" do - it "correctly update first unread as needed" do - time1 = 3.days.ago - time2 = 2.days.ago - time3 = 1.days.ago - - topic1 = Fabricate(:topic, last_unread_at: time1) - topic2 = Fabricate(:topic, last_unread_at: time2) - topic3 = Fabricate(:topic, last_unread_at: time3) - - user = Fabricate(:user) - user.user_stat.update_columns(first_topic_unread_at: Time.zone.now) - - TopicUser.change(user.id, topic3, notification_level: tracking) - - user.user_stat.reload - expect(user.user_stat.first_topic_unread_at).to be_within(1.second).of(time3) - - TopicUser.change(user.id, topic2, notification_level: watching) - - user.user_stat.reload - expect(user.user_stat.first_topic_unread_at).to be_within(1.second).of(time2) - - TopicUser.change(user.id, topic1, notification_level: regular) - - user.user_stat.reload - expect(user.user_stat.first_topic_unread_at).to be_within(1.second).of(time2) - end - end - describe '#notification_levels' do context "verify enum sequence" do before do diff --git a/spec/models/user_stat_spec.rb b/spec/models/user_stat_spec.rb index 86968285018..f440d78b210 100644 --- a/spec/models/user_stat_spec.rb +++ b/spec/models/user_stat_spec.rb @@ -10,34 +10,6 @@ describe UserStat do expect(user.user_stat.new_since).to be_present end - context "#update_first_topic_unread_at" do - it "updates date correctly for staff" do - now = Time.zone.now - - admin = Fabricate(:admin) - topic = Fabricate(:topic, - highest_staff_post_number: 7, - highest_post_number: 1, - last_unread_at: now - ) - - UserStat.update_first_topic_unread_at! - - admin.reload - - expect(admin.user_stat.first_topic_unread_at).to_not be_within(5.years).of(now) - - TopicUser.change(admin.id, topic.id, last_read_post_number: 1, - notification_level: NotificationLevels.all[:tracking]) - - UserStat.update_first_topic_unread_at! - - admin.reload - - expect(admin.user_stat.first_topic_unread_at).to be_within(1.second).of(now) - end - end - context '#update_view_counts' do let(:user) { Fabricate(:user) }