Add target user to reports

This commit is contained in:
Andreas Nedbal 2024-01-23 19:57:26 +01:00 committed by Andreas Nedbal
parent 6e370608b3
commit ab442db40b
3 changed files with 46 additions and 4 deletions

View File

@ -1,5 +1,6 @@
class Report < ApplicationRecord class Report < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :target_user, class_name: "User", optional: true
validates :type, presence: true validates :type, presence: true
validates :target_id, presence: true validates :target_id, presence: true
validates :user_id, presence: true validates :user_id, presence: true

View File

@ -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

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -28,11 +28,11 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_20_100445) do
end end
create_table "anonymous_blocks", force: :cascade do |t| create_table "anonymous_blocks", force: :cascade do |t|
t.bigint "user_id"
t.string "identifier" t.string "identifier"
t.bigint "question_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.bigint "question_id"
t.bigint "user_id"
t.bigint "target_user_id" t.bigint "target_user_id"
t.index ["identifier"], name: "index_anonymous_blocks_on_identifier" t.index ["identifier"], name: "index_anonymous_blocks_on_identifier"
t.index ["question_id"], name: "index_anonymous_blocks_on_question_id" 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 end
create_table "mute_rules", id: :bigint, default: -> { "gen_timestamp_id('mute_rules'::text)" }, force: :cascade do |t| 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.string "muted_phrase"
t.datetime "created_at", precision: nil, null: false t.datetime "created_at", precision: nil, null: false
t.datetime "updated_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" t.index ["user_id"], name: "index_mute_rules_on_user_id"
end end
@ -173,6 +173,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_12_20_100445) do
t.datetime "updated_at", precision: nil t.datetime "updated_at", precision: nil
t.boolean "deleted", default: false t.boolean "deleted", default: false
t.string "reason" 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 ["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" t.index ["user_id", "created_at"], name: "index_reports_on_user_id_and_created_at"
end end