You are here

function hosting_client_update_1 in Hosting 6.2

Same name and namespace in other branches
  1. 5 client/hosting_client.install \hosting_client_update_1()
  2. 7.4 client/hosting_client.install \hosting_client_update_1()
  3. 7.3 client/hosting_client.install \hosting_client_update_1()

Add the unique index to the client table

This will also run through all existing clients and merge / delete the ones that don't belong.

File

client/hosting_client.install, line 97
Install, update and uninstall for the clients module.

Code

function hosting_client_update_1() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret = array();
      $result = db_query("SELECT email, count(distinct nid) as count FROM {hosting_client} GROUP BY email");
      while ($distinct = db_fetch_object($result)) {
        if ($distinct->count > 1) {

          # we have found duplicates.
          $result2 = db_query("SELECT nid FROM {hosting_client} WHERE email = '%s' ORDER BY nid", $distinct->email);
          $first = false;
          while ($client = db_fetch_object($result2)) {
            if (!$first) {

              // this is the key all the others will be assigned to.
              $first = $client->nid;
            }
            else {

              // reset nodes to the first occurrence, and delete the duplicate
              db_query("UPDATE {hosting_site} SET client=%d WHERE client=%d", $first, $client->nid);
              node_delete($client->nid);
            }
          }
        }
      }
      $ret[] = update_sql("CREATE UNIQUE INDEX hosting_client_email_idx ON hosting_client (email)");
      break;
  }
  return $ret;
}