Skip to content

Instantly share code, notes, and snippets.

@arturo-c
Created September 26, 2011 18:57
Show Gist options
  • Save arturo-c/1243040 to your computer and use it in GitHub Desktop.
Save arturo-c/1243040 to your computer and use it in GitHub Desktop.
/**
* Make sure that we have no duplicate emails on user creation by updating schema for mail field in users table.
*/
function apci_user_update_6003() {
$ret = array();
$unique_mail = array();
$duplicates = db_query('SELECT DISTINCT a.uid, a.mail, a.name FROM users a INNER JOIN users b ON a.mail = b.mail WHERE a.uid <> b.uid ORDER BY a.uid');
while($duplicate = db_fetch_object($duplicates)) {
if(array_key_exists(strtolower($duplicate->mail), $unique_mail)) {
list($mail_1, $mail_2) = split('@', $duplicate->mail);
$unique_mail[strtolower($duplicate->mail)]['count'] +=1;
db_query('UPDATE users SET mail="'.$mail_1.'_'.$unique_mail[strtolower($duplicate->mail)]['count'].'_'.$mail_2.'@allplayers.net" WHERE uid="'.$duplicate->uid.'"');
watchdog('duplicates', 'Successfully changed email of duplicated account: %dup from %oldmail to %newmail',
array('%dup' => $duplicate->name, '%oldmail' => $duplicate->mail, '%newmail' => $mail_1.'_'.$unique_mail[strtolower($duplicate->mail)]['count'].'_'.$mail_2.'@allplayers.net'));
} else{
$unique_mail[strtolower($duplicate->mail)]['count'] = 0;
$unique_mail[strtolower($duplicate->mail)]['uid'] = $duplicate->uid;
}
}
db_drop_index($ret, 'users', 'mail');
db_add_unique_key($ret, 'users', 'mail', array('mail'));
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment