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

View File

@ -10,23 +10,23 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_08_20_182149) do
ActiveRecord::Schema[7.0].define(version: 2023_08_21_063101) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "pgcrypto"
enable_extension "plpgsql"
create_table "anomaly_notifications", id: :serial, force: :cascade do |t|
t.integer "user_id"
t.string "user_type"
t.integer "exercise_id"
t.integer "exercise_collection_id"
t.string "reason"
t.integer "contributor_id", null: false
t.string "contributor_type", null: false
t.integer "exercise_id", null: false
t.integer "exercise_collection_id", null: false
t.jsonb "reason"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["contributor_type", "contributor_id"], name: "index_anomaly_notifications_on_contributor"
t.index ["exercise_collection_id"], name: "index_anomaly_notifications_on_exercise_collection_id"
t.index ["exercise_id"], name: "index_anomaly_notifications_on_exercise_id"
t.index ["user_type", "user_id"], name: "index_anomaly_notifications_on_user"
end
create_table "authentication_tokens", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
@ -595,6 +595,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_08_20_182149) do
t.index ["user_type", "user_id"], name: "index_user_proxy_exercise_exercises_on_user"
end
add_foreign_key "anomaly_notifications", "exercise_collections"
add_foreign_key "anomaly_notifications", "exercises"
add_foreign_key "authentication_tokens", "study_groups"
add_foreign_key "community_solution_contributions", "community_solution_locks"
add_foreign_key "community_solution_contributions", "community_solutions"