Merge judge records into existing user records

Start to cleanup users table to be able to add UNIQUE(username,year)
This commit is contained in:
james 2008-10-22 16:44:03 +00:00
parent f981858df9
commit 3c1a4d29cc

View File

@ -7,6 +7,30 @@ function db_update_116_post()
$q = mysql_query("UPDATE `users` SET year={$config['FAIRYEAR']} WHERE year=0");
echo mysql_error();
/* Fix users without a username */
mysql_query("UPDATE `users` SET `username`=`email` WHERE `username`=''");
/*randomize usernames for any user that doesnt have a username at this point */
$q=mysql_query("SELECT id FROM `users` WHERE username=''");
//this is ripped from user.inc.php's generate passsword function.
//yes there's a chance of collisions, but i think highly unlikely enough that we
//dont need to worry about it.
$available="ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
$len=strlen($available) - 1;
while($r=mysql_fetch_object($q)) {
$username="";
for($x=0;$x<16;$x++)
$username.=$available{rand(0,$len)};
mysql_query("UPDATE users SET username='$username' WHERE id='$r->id'");
}
//okay now finally, there's a chance of duplicates from committee/volunteer that were in here before, so we need to merge them
//FIXME: dave add the merge code :)
//after done, we should be able to do "ALTER TABLE users ADD UNIQUE (username,year)"
//this can go into update 117.sql i guess?
/* Create volunteer database entries for any that don't exist */
$q = mysql_query("SELECT * FROM users WHERE types LIKE '%volunteer%'");
while($i = mysql_fetch_object($q)) {
@ -78,15 +102,39 @@ function db_update_116_post()
'deleted' => $j->deleted,
'deleteddatetime' => $j->deleteddatetime );
/* Insert the judge */
$fields = '`'.join('`,`', array_keys($u)).'`';
$vals = "'".join("','", array_values($u))."'";
$q = mysql_query("INSERT INTO users ($fields) VALUES ($vals)");
$id = mysql_insert_id();
$updateexclude=array("id","uid","types","username","password","passwordset","oldpassword","year","created","lastlogin","firstaid","cpr","deleted","deleteddatetime");
if($map[$j->id]['uid'] == '') {
$map[$j->id]['uid'] = $id;
$q = mysql_query("UPDATE users SET `uid`='$id' WHERE id='$id'");
//check if a user already exists with this username
$uq=mysql_query("SELECT * FROM users WHERE (username='".mysql_real_escape_string($j->email)."' OR email='".mysql_real_escape_string($j->email)."') AND year='$j->year'");
if($j->email && $ur=mysql_fetch_object($uq)) {
$id=$ur->id;
echo "Using existing users.id=$id for judges.id=$j->id because email address/year ($j->email/$j->year) matches\n";
$sqlset="";
foreach($u AS $f=>$v) {
if(!$ur->$f && $j->$f && !in_array($f,$updateexclude)) {
$sqlset.="`$f`='".mysql_real_escape_string($j->$f).", ";
}
}
$sql="UPDATE users SET $sqlset `types`='{$ur->types},judge',`username`='".mysql_real_escape_string($j->email)."' WHERE id='$id'";
mysql_query($sql);
echo mysql_error();
echo " Updated user record with judge info, but only merged:\n";
echo " ($sqlset)\n";
}
else
{
/* Insert the judge */
$fields = '`'.join('`,`', array_keys($u)).'`';
$vals = "'".join("','", array_values($u))."'";
$q = mysql_query("INSERT INTO users ($fields) VALUES ($vals)");
$id = mysql_insert_id();
if($map[$j->id]['uid'] == '') {
$map[$j->id]['uid'] = $id;
$q = mysql_query("UPDATE users SET `uid`='$id' WHERE id='$id'");
}
}
$uj = array( 'users_id' => "$id",