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"