mirror of
https://github.com/discourse/discourse.git
synced 2026-08-07 19:55:17 -05:00
DEV: Add muted_users converter and importer steps (#33709)
This adds converter(Discourse-only, for now) and importer steps for muted_users
This commit is contained in:
@@ -49,6 +49,12 @@ schema:
|
||||
exclude:
|
||||
- "created_at"
|
||||
- "id"
|
||||
group_users:
|
||||
primary_key_column_names: [ "group_id", "user_id" ]
|
||||
columns:
|
||||
exclude:
|
||||
- "id"
|
||||
- "first_unread_pm_at"
|
||||
groups:
|
||||
columns:
|
||||
add:
|
||||
@@ -79,12 +85,10 @@ schema:
|
||||
- "publish_read_state"
|
||||
- "title"
|
||||
- "visibility_level"
|
||||
group_users:
|
||||
primary_key_column_names: [ "group_id", "user_id" ]
|
||||
muted_users:
|
||||
columns:
|
||||
exclude:
|
||||
- "id"
|
||||
- "first_unread_pm_at"
|
||||
user_emails:
|
||||
columns:
|
||||
include:
|
||||
@@ -270,7 +274,6 @@ schema:
|
||||
- "message_bus"
|
||||
- "model_accuracies"
|
||||
- "moved_posts"
|
||||
- "muted_users"
|
||||
- "notifications"
|
||||
- "oauth2_user_infos"
|
||||
- "onceoff_logs"
|
||||
|
||||
@@ -132,6 +132,13 @@ CREATE TABLE "groups"
|
||||
visibility_level INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE muted_users
|
||||
(
|
||||
created_at DATETIME,
|
||||
muted_user_id NUMERIC NOT NULL,
|
||||
user_id NUMERIC NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE user_emails
|
||||
(
|
||||
email TEXT NOT NULL,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Migrations::Converters::Discourse
|
||||
class MutedUsers < ::Migrations::Converters::Base::ProgressStep
|
||||
attr_accessor :source_db
|
||||
|
||||
def max_progress
|
||||
@source_db.count <<~SQL
|
||||
SELECT COUNT(*) FROM muted_users
|
||||
SQL
|
||||
end
|
||||
|
||||
def items
|
||||
@source_db.query <<~SQL
|
||||
SELECT * FROM muted_users
|
||||
SQL
|
||||
end
|
||||
|
||||
def process_item(item)
|
||||
IntermediateDB::MutedUser.create(
|
||||
muted_user_id: item[:muted_user_id],
|
||||
user_id: item[:user_id],
|
||||
created_at: item[:created_at],
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# This file is auto-generated from the IntermediateDB schema. To make changes,
|
||||
# update the "config/intermediate_db.yml" configuration file and then run
|
||||
# `bin/cli schema generate` to regenerate this file.
|
||||
|
||||
module Migrations::Database::IntermediateDB
|
||||
module MutedUser
|
||||
SQL = <<~SQL
|
||||
INSERT INTO muted_users (
|
||||
created_at,
|
||||
muted_user_id,
|
||||
user_id
|
||||
)
|
||||
VALUES (
|
||||
?, ?, ?
|
||||
)
|
||||
SQL
|
||||
|
||||
def self.create(created_at: nil, muted_user_id:, user_id:)
|
||||
::Migrations::Database::IntermediateDB.insert(
|
||||
SQL,
|
||||
::Migrations::Database.format_datetime(created_at),
|
||||
muted_user_id,
|
||||
user_id,
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,44 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Migrations::Importer::Steps
|
||||
class MutedUsers < ::Migrations::Importer::CopyStep
|
||||
depends_on :users
|
||||
|
||||
requires_set :existing_muted_users, "SELECT user_id, muted_user_id FROM muted_users"
|
||||
|
||||
column_names %i[muted_user_id user_id created_at]
|
||||
|
||||
total_rows_query <<~SQL, MappingType::USERS
|
||||
SELECT COUNT(*)
|
||||
FROM muted_users
|
||||
JOIN mapped.ids mapped_users
|
||||
ON muted_users.user_id = mapped_users.original_id AND mapped_users.type = ?
|
||||
SQL
|
||||
|
||||
rows_query <<~SQL, MappingType::USERS
|
||||
SELECT muted_users.*,
|
||||
mapped_users.discourse_id AS discourse_user_id,
|
||||
mapped_muted_users.discourse_id AS discourse_muted_user_id
|
||||
FROM muted_users
|
||||
JOIN mapped.ids mapped_users
|
||||
ON muted_users.user_id = mapped_users.original_id AND mapped_users.type = ?
|
||||
JOIN mapped.ids mapped_muted_users
|
||||
ON muted_users.muted_user_id = mapped_muted_users.original_id AND mapped_muted_users.type = ?
|
||||
ORDER BY user_id, muted_user_id
|
||||
SQL
|
||||
|
||||
private
|
||||
|
||||
def transform_row(row)
|
||||
user_id = row[:discourse_user_id]
|
||||
muted_user_id = row[:discourse_muted_user_id]
|
||||
|
||||
return nil unless @existing_muted_users.add?(user_id, muted_user_id)
|
||||
|
||||
row[:user_id] = user_id
|
||||
row[:muted_user_id] = muted_user_id
|
||||
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe ::Migrations::Database::IntermediateDB::MutedUser do
|
||||
it_behaves_like "a database entity"
|
||||
end
|
||||
Reference in New Issue
Block a user