You are here

function ldapauth_update_6005 in LDAP integration 6

File

./ldapauth.install, line 296
ldapauth module installation and upgrade code.

Code

function ldapauth_update_6005() {
  $ret = array();
  db_add_field($ret, 'ldapauth', 'machine_name', array(
    'type' => 'varchar',
    'length' => 255,
  ));

  //Create machine names for existing servers!
  $mnames = array();
  $result = db_query('SELECT sid, name from {ldapauth}');
  while ($server = db_fetch_object($result)) {
    $machine_name = drupal_strtolower($server->name);
    $machine_name = preg_replace('/\\s+/', '_', $machine_name);
    $machine_name = preg_replace('/[^a-z0-9_]/', '', $machine_name);
    if (empty($machine_name)) {
      $machine_name = "server_" . $server->sid;
    }
    for ($i = 1; isset($mname[$machine_name]); $i++) {

      // Must be unique
      $machine_name .= $i;
    }
    $mname[$machine_name] = $machine_name;
    $ret[] = update_sql("UPDATE {ldapauth} SET machine_name = '{$machine_name}' WHERE sid = {$server->sid}");
  }

  // in not null after existing rows have a value.
  db_change_field($ret, 'ldapauth', 'machine_name', 'machine_name', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
  ));
  db_add_unique_key($ret, 'ldapauth', 'machine_name', array(
    'machine_name',
  ));
  return $ret;
}