--- ldapgroups.install	2011-01-10 16:20:15.000000000 -0500
+++ ldapgroups.install	2011-01-11 10:19:29.000000000 -0500
@@ -116,3 +116,78 @@
   return $ret;
 }
 
+function _ldapgroups_migrate_or_create_field(&$ret, $table, $old_field,
+  $new_field, $field_spec) {
+
+  if (db_column_exists($table, $old_field)) {
+    db_change_field($ret, $table, $old_field, $new_field, $field_spec);
+
+    // Serialize existing multi-row values
+    $result = db_query("SELECT sid, %s FROM {ldapauth}", $new_field);
+    while ($row = db_fetch_array($result)) {
+      if (strpos($row[$new_field], "\n")) {
+        $row[$new_field] = serialize(explode("\n", trim($row[$new_field])));
+
+        /* The somewhat strange code below is because we can't use update_sql
+         * due to the fact we're storing serialized data.  For more info, see:
+         *
+         * http://api.drupal.org/api/drupal/includes--database.inc/function/update_sql/6#comment-354
+         */
+        $query = "UPDATE {ldapauth} SET $new_field = '%s' WHERE sid = %d";
+        db_query($query, $row[$new_field], $row['sid']);
+        $msg = sprintf($query, $row[$new_field], $row['sid']);
+        $ret[] = array('success' => true, 'query' => $msg);
+      }
+    }
+  } else if (!db_column_exists($table, $new_field)) {
+    db_add_field($ret, $table, $old_field, $new_field, $field_spec);
+  } else {
+    drupal_set_message("ldapgroups update 6002: $new_field already exists");
+  }
+
+  return;
+}
+
+/* The Drupal 5 version of this module used different column names for some
+ * of the fields and multi-line fields were not serialized.  This update
+ * migrates the settings from any old fields into the new format.
+ */
+function ldapgroups_update_6002() {
+  $ret = array();
+  _ldapgroups_migrate_or_create_field($ret, 'ldapauth', 'ldap_groups_in_dn', 'ldapgroups_in_dn', array(
+    'type' => 'int',
+    'size' => 'tiny',
+    'not null' => TRUE,
+    'default' => '0',
+  ));
+  _ldapgroups_migrate_or_create_field($ret, 'ldapauth', 'ldap_group_dn_attribute', 'ldapgroups_dn_attribute', array(
+    'type' => 'varchar',
+    'length' => 255,
+  ));
+  _ldapgroups_migrate_or_create_field($ret, 'ldapauth', 'ldap_group_attr', 'ldapgroups_attr', array(
+    'type' => 'varchar',
+    'length' => 255,
+  ));
+  _ldapgroups_migrate_or_create_field($ret, 'ldapauth', 'ldap_groups_in_attr', 'ldapgroups_in_attr', array(
+    'type' => 'int',
+    'size' => 'tiny',
+    'not null' => TRUE,
+    'default' => '0',
+  ));
+  _ldapgroups_migrate_or_create_field($ret, 'ldapauth', 'ldap_groups_as_entries', 'ldapgroups_as_entries', array(
+    'type' => 'int',
+    'size' => 'tiny',
+    'not null' => TRUE,
+    'default' => '0',
+  ));
+  _ldapgroups_migrate_or_create_field($ret, 'ldapauth', 'ldap_group_entries', 'ldapgroups_entries', array(
+    'type' => 'text',
+  ));
+  _ldapgroups_migrate_or_create_field($ret, 'ldapauth', 'ldap_group_entries_attribute', 'ldapgroups_entries_attribute',  array(
+    'type' => 'varchar',
+    'length' => 255,
+  ));
+
+  return $ret;
+}
+

