Merge pull request #1472 from biosistemika/mm-sci-2953-system-notifications-data-migration

System Notifications - Migration of existing data
This commit is contained in:
Miha Mencin 2019-02-01 14:02:00 +01:00 committed by GitHub
commit 3538e74b03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,23 +1,73 @@
namespace :notifications do # frozen_string_literal: true
desc 'Creates new system notification for all active users'
task :new_system, [:title, :message] => :environment do |_, args|
include NotificationsHelper
if args.blank? || namespace :notifications do
args.empty? || desc 'Copies system notifications to newly created data structure. IT SHOULD BE RUN ONE TIME ONLY'
args[:title].blank? || task copy_system_notifications: :environment do
args[:message].blank? t0 = Time.now
puts 'One or both of arguments are missing' system_notifications = Notification
return .where(type_of: :system_message)
.where(generator_user_id: nil)
.where.not('title like ?', 'Congratulations%')
system_notifications.each do |system_notification|
new_notification = SystemNotification.create!(
source_id: -1,
title: system_notification.title,
description: system_notification.message,
modal_title: system_notification.title,
modal_body: system_notification.message,
show_on_login: false,
source_created_at: system_notification.created_at,
last_time_changed_at: system_notification.created_at
)
created_at = system_notification.created_at
sql = ' INSERT INTO user_system_notifications
(
user_id,
created_at,
updated_at,
system_notification_id,
seen_at,
read_at
)
VALUES
'
user_notifications = UserNotification
.where(notification_id: system_notification.id)
values_array = user_notifications.map do |user_notification|
user_notification
.slice(:user_id, :created_at, :updated_at)
.merge(system_notification_id: new_notification.id)
.merge(seen_at: created_at, read_at: created_at)
.values
.map { |v| "'#{v}'" }
.join(',')
end
values_sql = values_array
.map { |v| "(#{v})" }
.join(',')
sql += values_sql
ActiveRecord::Base.connection.execute(sql)
end end
title = args[:title] t1 = Time.now
message = args[:message] puts "Task took #{t1 - t0}"
end
puts 'Creating following system notification:' desc 'Removes obsolete system notifications from notifications table.'
puts " *** #{title} ***" task delete_obsolete_system_notifications: :environment do
puts " #{I18n.l(Time.now, format: :full)} | #{message}" system_notifications = Notification
.where(type_of: :system_message)
.where(generator_user_id: nil)
.where.not('title like ?', 'Congratulations%')
UserNotification
.where(notification_id: system_notifications.pluck(:id))
.delete_all
create_system_notification(title, message) system_notifications.delete_all
end end
end end