When importing anonymous users from phpBB I get the following error:
Error on record: {:email=>“anonymous_no_email_Надежда”, :username=>“111104”, :name=>“”, :created_at=>Mon, 10 Nov 2014 15:09:15 UTC +00:00, :active=>true, :trust_level=>0, :approved=>true, :approved_by_id=>-1, :approved_at=>2016-07-11 10:40:21 +0000, :import_mode=>true, :last_emailed_at=>2016-07-11 10:40:21 +0000}
/var/www/discourse/vendor/bundle/ruby/2.3.0/gems/rack-mini-profiler-0.10.1/lib/patches/db/pg.rb:90:in `exec’: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint “index_users_on_email” (ActiveRecord::RecordNotUnique)
DETAIL: Key (lower(email::text))=(anonymous_no_email_надежда) already exists.
: INSERT INTO “users” (“email”, “username”, “name”, “created_at”, “active”, “trust_level”, “approved”, “approved_by_id”, “approved_at”, “last_emailed_at”, “username_lower”, “updated_at”) VALUES (‘anonymous_no_email_Надежда’, ‘111104’, ‘’, ‘2014-11-10 15:09:15.000000’, ‘t’, 0, ‘t’, -1, ‘2016-07-11 10:40:21.494781’, ‘2016-07-11 10:40:21.599263’, ‘111104’, ‘2016-07-11 10:40:21.622126’) RETURNING “id”
It seems that we have guest posts under the names “НАДЕЖДА” and “Надежда” - it’s the same name, the first one merely being all-caps. As the importer doesn’t treat anonymous user names as case-insensitive, it tries to create two accounts here (probably not wrong). Trouble is, the generated email address violates uniqueness requirements which treat email addresses as case-insensitive. Looking at the source code, this issue doesn’t seem to be limited to Cyrillic, it would be just the same with Latin.
I’m running the import script from Discourse 1.5.3, master seems to have the same issue however. Given that treating account names as case-sensitive is desired, maybe the simplest solution is adding a hash of the username to the generated “email address”? This patch seems to solve the issue:
diff --git a/script/import_scripts/phpbb3/importers/user_importer.rb b/script/import_scripts/phpbb3/importers/user_importer.rb
index ca50197..3f91a52 100644
--- a/script/import_scripts/phpbb3/importers/user_importer.rb
+++ b/script/import_scripts/phpbb3/importers/user_importer.rb
@@ -47,11 +47,13 @@ module ImportScripts::PhpBB3
end
def map_anonymous_user(row)
+ require 'digest/md5'
username = row[:post_username]
+ md5 = Digest::MD5.hexdigest(username)
{
id: username,
- email: "anonymous_no_email_#{username}",
+ email: "anonymous_no_email_#{username}_#{md5}",
username: username,
name: '',
created_at: Time.zone.at(row[:first_post_time]),