From ab442db40b8f8b1ae485a6f5469df4d0a9c3b8fd Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Tue, 23 Jan 2024 19:57:26 +0100 Subject: [PATCH] Add target user to reports --- app/models/report.rb | 1 + ...240123182422_add_target_user_to_reports.rb | 39 +++++++++++++++++++ db/schema.rb | 10 +++-- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240123182422_add_target_user_to_reports.rb diff --git a/app/models/report.rb b/app/models/report.rb index bf61c377..3cdb28a4 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -1,5 +1,6 @@ class Report < ApplicationRecord belongs_to :user + belongs_to :target_user, class_name: "User", optional: true validates :type, presence: true validates :target_id, presence: true validates :user_id, presence: true diff --git a/db/migrate/20240123182422_add_target_user_to_reports.rb b/db/migrate/20240123182422_add_target_user_to_reports.rb new file mode 100644 index 00000000..1369940a --- /dev/null +++ b/db/migrate/20240123182422_add_target_user_to_reports.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +class AddTargetUserToReports < ActiveRecord::Migration[7.0] + def up + add_reference :reports, :target_user, null: true, foreign_key: false + + execute <<~SQL.squish + UPDATE reports + SET target_user_id = users.id + FROM users + WHERE users.id = reports.target_id AND reports.type = 'Reports::User' + SQL + + execute <<~SQL.squish + UPDATE reports + SET target_user_id = users.id + FROM users, comments + WHERE users.id = comments.user_id AND comments.id = reports.target_id AND reports.type = 'Reports::Comment' + SQL + + execute <<~SQL.squish + UPDATE reports + SET target_user_id = users.id + FROM users, answers + WHERE users.id = answers.user_id AND answers.id = reports.target_id AND reports.type = 'Reports::Answer' + SQL + + execute <<~SQL.squish + UPDATE reports + SET target_user_id = users.id + FROM users, questions + WHERE users.id = questions.user_id AND questions.id = reports.target_id AND reports.type = 'Reports::Question' + SQL + end + + def down + remove_reference :reports, :target_user, null: true, foreign_key: { to_table: :users } + end +end diff --git a/db/schema.rb b/db/schema.rb index e4990d1f..6429ebc6 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[7.0].define(version: 2023_12_20_100445) do +ActiveRecord::Schema[7.0].define(version: 2024_01_23_182422) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,11 +28,11 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_20_100445) do end create_table "anonymous_blocks", force: :cascade do |t| + t.bigint "user_id" t.string "identifier" + t.bigint "question_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.bigint "question_id" - t.bigint "user_id" t.bigint "target_user_id" t.index ["identifier"], name: "index_anonymous_blocks_on_identifier" t.index ["question_id"], name: "index_anonymous_blocks_on_question_id" @@ -95,10 +95,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_20_100445) do end create_table "mute_rules", id: :bigint, default: -> { "gen_timestamp_id('mute_rules'::text)" }, force: :cascade do |t| + t.bigint "user_id" t.string "muted_phrase" t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false - t.bigint "user_id" t.index ["user_id"], name: "index_mute_rules_on_user_id" end @@ -173,6 +173,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_20_100445) do t.datetime "updated_at", precision: nil t.boolean "deleted", default: false t.string "reason" + t.bigint "target_user_id" + t.index ["target_user_id"], name: "index_reports_on_target_user_id" t.index ["type", "target_id"], name: "index_reports_on_type_and_target_id" t.index ["user_id", "created_at"], name: "index_reports_on_user_id_and_created_at" end