diff --git a/db/db.code.version.txt b/db/db.code.version.txt index ee977b5e..078fa0fe 100644 --- a/db/db.code.version.txt +++ b/db/db.code.version.txt @@ -1 +1 @@ -115 +119 diff --git a/db/db.update.116.php b/db/db.update.116.php index 3f987246..f6375b1b 100644 --- a/db/db.update.116.php +++ b/db/db.update.116.php @@ -25,12 +25,86 @@ function db_update_116_post() 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? + + //okay now finally, there's a chance of duplicates from + //committee/volunteer that were in here before, so we need to merge + //them + $q = mysql_query("SELECT * FROM `users` WHERE types LIKE '%committee%'"); + while($r = mysql_fetch_assoc($q)) { + $orig_r = $r; + $qq = mysql_query("SELECT * FROM `users` WHERE + (`username`='{$r['username']}' OR `email`='{$r['email']}') + AND `id`!={$r['id']}"); + if(mysql_num_rows($qq) == 0) continue; + + echo "User id {$r['id']} ({$r['username']} {$r['email']}) has multiple users, merging...\n"; + + /* Now, there should only be two types, because the system is + * only supposed to let committee members and volunteers be + * created, and it has only been in use for one year without + * year stamps., but we'll handle any number */ + + /* However, we will make the committee the record that sticks + * */ + $delete_ids = array(); + $delete_userids = array(); + while($rr = mysql_fetch_assoc($qq)) { + $delete_ids[] = "`id`={$rr['id']}"; + $delete_userids[] = "`users_id`={$rr['id']}"; + $keys = array_keys($rr); + foreach($keys as $k) { + switch($k) { + case 'id': + /* Skip */ + break; + case 'types': + /* Merge types */ + if(strstr($r['types'], $rr['types']) == false) { + $r['types']= $r['types'].','.$rr['types']; + echo " New type: {$r['types']}\n"; + } + break; + default: + /* Save data */ + if(trim($r[$k]) == '' && trim($rr[$k]) != '') { + $r[$k] = $rr[$k]; + } + break; + } + } + } + + /* Construct SQL for a SET clause */ + $set = array(); + $keys = array_keys($r); + foreach($keys as $k) { + if($r[$k] != $orig_r[$k]) { + $set[] = "`$k`='{$r[$k]}'"; + } + } + if(count($set)) { + $query = join(',',$set); + mysql_query("UPDATE `users` SET $query WHERE id={$r['id']}"); + echo "Update query: UPDATE `users` SET $query WHERE id={$r['id']}\n"; + } + + /* Join together the WHERE commands */ + $where_id = "WHERE ".join(" OR ", $delete_ids); + $where_users_id = "WHERE ".join(" OR ", $delete_userids); + + echo "Merged... Deleting duplicate and adjusting volunteer tables...\n"; + /* Delete the dupe */ + mysql_query("DELETE FROM `users` $where_id"); + /* Update volunteer linkage */ + mysql_query("UPDATE `users_volunteer` SET `users_id`={$r['id']} $where_users_id"); + mysql_query("UPDATE `volunteer_positions_signup` SET `users_id`={$r['id']} $where_users_id"); + + echo "done with this user.\n"; + + } + /* 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)) { diff --git a/db/db.update.117.sql b/db/db.update.117.sql index 6a8b88f5..10681791 100644 --- a/db/db.update.117.sql +++ b/db/db.update.117.sql @@ -17,3 +17,5 @@ ALTER TABLE `judges_teams_link` CHANGE `judges_id` `users_id` INT( 11 ) NOT NULL -- The users_id is linked with the year, don't need to store it here too ALTER TABLE `judges_specialaward_sel` DROP `year`; +ALTER TABLE users ADD UNIQUE (username,year); +