Migrate AnomalyNotification to contributor

* Also, this commit aims to fix the (broken) anomaly notifications.
This commit is contained in:
Sebastian Serth
2023-08-21 09:08:24 +02:00
committed by Sebastian Serth
parent a1941336d9
commit 9d1be1eeff
10 changed files with 142 additions and 65 deletions

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
class RenameUserToContributorInAnomalyNotifications < ActiveRecord::Migration[7.0]
def change
# We need to drop and recreate the index because the name would be too long otherwise.
# Renaming is not possible because the index can have two different names.
change_table :anomaly_notifications do |t|
t.remove_index %i[user_type user_id]
t.rename :user_id, :contributor_id
t.rename :user_type, :contributor_type
end
add_index :anomaly_notifications, %i[contributor_type contributor_id], name: 'index_anomaly_notifications_on_contributor'
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
class AddForeignKeysToAnomalyNotifications < ActiveRecord::Migration[7.0]
def change
up_only do
# We cannot add a foreign key to a table that has rows that violate the constraint.
AnomalyNotification.where.not(exercise_id: Exercise.all.select(:id)).delete_all
end
change_column_null :anomaly_notifications, :contributor_id, false
change_column_null :anomaly_notifications, :contributor_type, false
change_column_null :anomaly_notifications, :exercise_id, false
add_foreign_key :anomaly_notifications, :exercises
change_column_null :anomaly_notifications, :exercise_collection_id, false
add_foreign_key :anomaly_notifications, :exercise_collections
end
class AnomalyNotification < ActiveRecord::Base; end
class Exercise < ActiveRecord::Base; end
class ExerciseCollection < ActiveRecord::Base; end
end

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
class ConvertReasonToJsonInAnomalyNotifications < ActiveRecord::Migration[7.0]
def up
AnomalyNotification.where("reason LIKE '%value:%'").each do |anomaly_notification|
reason = anomaly_notification.reason
reason = reason.gsub('value:', '"value":')
reason = reason.gsub(/"(\d+\.\d+)"/) {|_| Regexp.last_match(1) }
anomaly_notification.update!(reason:)
end
change_column :anomaly_notifications, :reason, :jsonb, using: 'reason::jsonb'
end
def down
change_column :anomaly_notifications, :reason, :string
end
class AnomalyNotification < ActiveRecord::Base; end
end