2023-10-11 19:43:20 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MigrateNotificationToNoticed < ActiveRecord::Migration[7.0]
|
2023-10-17 18:02:55 +08:00
|
|
|
class UserNotification < ApplicationRecord
|
|
|
|
belongs_to :notification
|
|
|
|
end
|
|
|
|
|
2023-10-11 19:43:20 +08:00
|
|
|
def up
|
2023-10-19 14:51:59 +08:00
|
|
|
add_column :notifications, :params, :jsonb
|
2023-10-12 17:06:10 +08:00
|
|
|
add_column :notifications, :type, :string
|
2023-10-11 19:43:20 +08:00
|
|
|
add_column :notifications, :read_at, :datetime
|
2023-10-12 17:47:29 +08:00
|
|
|
add_reference :notifications, :recipient, polymorphic: true
|
2023-10-11 19:43:20 +08:00
|
|
|
|
2023-10-18 19:23:55 +08:00
|
|
|
Notification.reset_column_information
|
|
|
|
|
2023-10-11 19:43:20 +08:00
|
|
|
type_mapping = {
|
2023-10-12 17:38:19 +08:00
|
|
|
0 => 'ActivityNotification',
|
|
|
|
1 => 'GeneralNotification',
|
|
|
|
5 => 'DeliveryNotification',
|
|
|
|
7 => 'DeliveryNotification'
|
2023-10-11 19:43:20 +08:00
|
|
|
}
|
|
|
|
|
2023-10-12 17:06:10 +08:00
|
|
|
UserNotification.includes(:notification).find_each do |user_notification|
|
2023-10-11 19:43:20 +08:00
|
|
|
notification = user_notification.notification.dup
|
|
|
|
|
|
|
|
new_type = type_mapping[notification.type_of]
|
2023-10-18 18:47:44 +08:00
|
|
|
new_type ||= 'GeneralNotification'
|
2023-10-11 19:43:20 +08:00
|
|
|
|
|
|
|
params = {
|
|
|
|
title: notification.title,
|
|
|
|
message: notification.message,
|
|
|
|
legacy: true
|
|
|
|
}
|
|
|
|
|
2023-10-12 17:38:19 +08:00
|
|
|
params[:error] = notification.type_of == 7 if new_type == 'DeliveryNotification'
|
2023-10-12 17:47:29 +08:00
|
|
|
notification.update!(
|
2023-10-11 19:43:20 +08:00
|
|
|
params: params,
|
|
|
|
type: new_type,
|
|
|
|
read_at: (user_notification.updated_at if user_notification.checked),
|
|
|
|
recipient_id: user_notification.user_id,
|
|
|
|
recipient_type: 'User',
|
|
|
|
created_at: user_notification.created_at,
|
|
|
|
updated_at: user_notification.updated_at
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-10-12 17:47:29 +08:00
|
|
|
UserNotification.delete_all
|
|
|
|
Notification.where(type: nil).delete_all
|
2023-10-12 17:06:10 +08:00
|
|
|
|
2023-10-13 16:14:16 +08:00
|
|
|
drop_table :user_notifications
|
2023-10-12 17:06:10 +08:00
|
|
|
change_column_null :notifications, :type, false
|
2023-10-19 14:51:59 +08:00
|
|
|
change_column_null :notifications, :params, false
|
|
|
|
change_column_default :notifications, :params, {}
|
2023-10-11 19:43:20 +08:00
|
|
|
|
|
|
|
remove_column :notifications, :type_of
|
|
|
|
remove_column :notifications, :title
|
|
|
|
remove_column :notifications, :message
|
|
|
|
remove_column :notifications, :generator_user_id
|
|
|
|
end
|
|
|
|
end
|