function profile2_update_7104 in Profile 2 7
Same name and namespace in other branches
- 7.2 profile2.install \profile2_update_7104()
Remove duplicate profile records in batches of 50, and add unique key on type + uid.
File
- ./
profile2.install, line 236 - Install, update and uninstall functions for the profile module.
Code
function profile2_update_7104(&$sandbox) {
// Query to get duplicate profiles.
$query = db_select('profile', 'p1');
$query
->distinct();
$query
->fields('p1', array(
'pid',
));
$query
->join('profile', 'p2', 'p1.type = p2.type AND p1.uid = p2.uid AND p1.pid < p2.pid');
// Setup initial batch variables.
if (!isset($sandbox['progress'])) {
// The number of duplicate profiles deleted so far.
$sandbox['progress'] = 0;
// Total number of duplicate profiles that will be deleted.
$sandbox['total'] = $query
->execute()
->rowCount();
}
if ($sandbox['total']) {
// Query the next 50 profiles to be deleted.
$query
->range(0, 50);
$result = $query
->execute();
// Update progress of removing duplicate profiles.
$sandbox['progress'] = $sandbox['progress'] + $result
->rowCount();
// Delete duplicate profiles.
profile2_delete_multiple($result
->fetchCol());
// Update batch status.
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['total'];
}
else {
$sandbox['#finished'] = 1;
}
if ($sandbox['#finished'] >= 1) {
// Add a unique key on type + uid.
db_add_unique_key('profile', 'user_profile_type', array(
'type',
'uid',
));
return t('@total duplicate profiles were removed from the system.', array(
'@total' => $sandbox['progress'],
));
}
}