From f1863e53091d3f5d3d73cfa8e4235594c9cf4234 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 26 May 2023 20:39:19 +0200 Subject: [PATCH 1/2] Fix notification created_at not being set for comment notifications --- app/models/subscription.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 66520203..0b05a1b9 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -36,7 +36,7 @@ class Subscription < ApplicationRecord .where.not(user: source.user) .where.not(user_id: muted_by) .map do |s| - { target_id: source.id, target_type: Comment, recipient_id: s.user_id, new: true, type: Notification::Commented } + { target_id: source.id, target_type: Comment, recipient_id: s.user_id, new: true, type: Notification::Commented, created_at: source.created_at, updated_at: source.created_at } end Notification.insert_all!(notifications) unless notifications.empty? # rubocop:disable Rails/SkipsModelValidations From 84d4ac3dc6cf4cc0f789502aa35ec4347de047f5 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 26 May 2023 20:39:53 +0200 Subject: [PATCH 2/2] Backfill missing created_at on comment notifications --- ...ing_created_at_on_commented_notifications.rb | 17 +++++++++++++++++ db/schema.rb | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20230526181715_backfill_missing_created_at_on_commented_notifications.rb diff --git a/db/migrate/20230526181715_backfill_missing_created_at_on_commented_notifications.rb b/db/migrate/20230526181715_backfill_missing_created_at_on_commented_notifications.rb new file mode 100644 index 00000000..906b6859 --- /dev/null +++ b/db/migrate/20230526181715_backfill_missing_created_at_on_commented_notifications.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class BackfillMissingCreatedAtOnCommentedNotifications < ActiveRecord::Migration[6.1] + def up + execute <<~SQUIRREL + UPDATE notifications + SET created_at = comments.created_at, updated_at = comments.created_at + FROM comments + WHERE notifications.target_id = comments.id AND notifications.created_at IS NULL AND notifications.type = 'Notification::Commented' + SQUIRREL + + # clean up notifications for deleted comments + Notification::Commented.where(created_at: nil).destroy_all + end + + def down; end +end diff --git a/db/schema.rb b/db/schema.rb index 39dc6424..c45fb69c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_02_27_174822) do +ActiveRecord::Schema.define(version: 2023_05_26_181715) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"