copied from duplicate issue
MDL-46642 by Mathew Gancarz:
This may not be a real bug if case-sensitivity is on purpose but we've recently started running the /auth/db/cli/sync_users.php script.
With it we get a whole series of
"Error inserting user"
I was able to track the problem down to this. Existing users in Moodle that were created when they first logged in, had entries in mdl_user as such:
Note the different casing of hotmail vs Hotmail in the username vs email.
In our external db, the email and username are both: samplename@Hotmail.com
It looks like the problem is the sync script considers the non-matching casing as a different user, then tries to create the user again and fails.
We are on 2.5.6, but I think it also affects 2.7, judging by the code on github. We are able to work around this for now by changing the casing of the email field in our external db to be all lower case, but it would be better if it the sync_users script was not-case sensitive when the username in moodle appears to always be stored lower case and the login form is also case-insensitive.
The piece of code that would need to be tweaked for 2.7, I think is line 376 of /auth/db/auth.php, where the array_diff does a case sensitive comparison.
$add_users = array_diff($userlist, $usernames);
For 2.5 it is line 370 of the same file.