DEV: Add support for user_associated_accounts to import script (#26779)

This commit is contained in:
Gerhard Schlager 2024-04-29 19:48:32 +02:00 committed by GitHub
parent a538e2f153
commit e3882a0c48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 0 deletions

View File

@ -493,6 +493,18 @@ class BulkImport::Base
external_card_background_url
]
USER_ASSOCIATED_ACCOUNT_COLUMNS ||= %i[
provider_name
provider_uid
user_id
last_used
info
credentials
extra
created_at
updated_at
]
USER_OPTION_COLUMNS ||= %i[
user_id
mailing_list_mode
@ -815,6 +827,10 @@ class BulkImport::Base
create_records(rows, "single_sign_on_record", USER_SSO_RECORD_COLUMNS, &block)
end
def create_user_associated_accounts(rows, &block)
create_records(rows, "user_associated_account", USER_ASSOCIATED_ACCOUNT_COLUMNS, &block)
end
def create_user_custom_fields(rows, &block)
create_records(rows, "user_custom_field", USER_CUSTOM_FIELD_COLUMNS, &block)
end
@ -1132,6 +1148,16 @@ class BulkImport::Base
sso_record
end
def process_user_associated_account(account)
account[:last_used] ||= NOW
account[:info] ||= "{}"
account[:credentials] ||= "{}"
account[:extra] ||= "{}"
account[:created_at] = NOW
account[:updated_at] = NOW
account
end
def process_group_user(group_user)
group_user[:created_at] = NOW
group_user[:updated_at] = NOW

View File

@ -54,6 +54,7 @@ class BulkImport::Generic < BulkImport::Base
import_user_fields
import_user_field_values
import_single_sign_on_records
import_user_associated_accounts
import_muted_users
import_user_histories
import_user_notes
@ -611,6 +612,34 @@ class BulkImport::Generic < BulkImport::Base
users.close
end
def import_user_associated_accounts
puts "", "Importing user associated accounts..."
accounts = query(<<~SQL)
SELECT a.*, COALESCE(u.last_seen_at, u.created_at) AS last_used_at, u.email, u.username
FROM user_associated_accounts a
JOIN users u ON u.id = a.user_id
ORDER BY a.user_id, a.provider_name
SQL
existing_user_ids = UserAssociatedAccount.pluck(:user_id).to_set
create_user_associated_accounts(accounts) do |row|
user_id = user_id_from_imported_id(row["user_id"])
next if user_id && existing_user_ids.include?(user_id)
{
user_id: user_id,
provider_name: row["provider_name"],
provider_uid: row["provider_uid"],
last_used: to_datetime(row["last_used_at"]),
info: row["info"].presence || { nickname: row["username"], email: row["email"] }.to_json,
}
end
accounts.close
end
def import_topics
puts "", "Importing topics..."