Common subdirectories: uuid_old/translations and uuid/translations
diff -up uuid_old/uuid.admin.inc uuid/uuid.admin.inc
--- uuid_old/uuid.admin.inc	2010-09-08 17:07:14.000000000 +0100
+++ uuid/uuid.admin.inc	2010-09-18 11:31:08.000000000 +0100
@@ -102,7 +102,7 @@ function uuid_sync() {
   if (variable_get('uuid_automatic_for_users', FALSE)) {
     $result = db_query("SELECT uid FROM {users} WHERE uid NOT IN (SELECT uid FROM {uuid_users})");
     while($item = db_fetch_object($result)) {
-      db_query("INSERT INTO {uuid_users} (uid, uuid) VALUES(%d, '%s')", $item->uid, uuid_uuid());
+      _uuid_insert('uuid_users', 'uid', $item->uid);
     }
   }
 
@@ -110,7 +110,7 @@ function uuid_sync() {
   if (variable_get('uuid_automatic_for_comments', FALSE)) {
     $result = db_query("SELECT cid FROM {comments} WHERE cid NOT IN (SELECT cid FROM {uuid_comments})");
     while($item = db_fetch_object($result)) {
-      db_query("INSERT INTO {uuid_comments} (cid, uuid) VALUES(%d, '%s')", $item->cid, uuid_uuid());
+      _uuid_insert('uuid_comments', 'cid', $item->cid);
     }
   }
 
@@ -124,13 +124,13 @@ function uuid_sync() {
     // Nodes.
     $result = db_query("SELECT nid FROM {node} WHERE type IN (" . $placeholders . ") AND nid NOT IN (SELECT nid FROM {uuid_node})", $types);
     while ($item = db_fetch_object($result)) {
-      db_query("INSERT INTO {uuid_node} (nid, uuid) VALUES(%d, '%s')", $item->nid, uuid_uuid());
+      _uuid_insert('uuid_node', 'nid', $item->nid);
     }
 
     // Revisions.
     $result = db_query("SELECT nr.vid AS vid FROM {node_revisions} AS nr INNER JOIN {node} n ON nr.nid = n.nid WHERE n.type IN (" . $placeholders . ") AND nr.vid NOT IN (SELECT vid FROM {uuid_node_revisions})", $types);
     while ($item = db_fetch_object($result)) {
-      db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES(%d, '%s')", $item->vid, uuid_uuid());
+      _uuid_insert('uuid_node_revisions', 'vid', $item->vid);
     }
   }
 
@@ -144,13 +144,13 @@ function uuid_sync() {
     // Vocabularies.
     $result = db_query("SELECT v.vid FROM {vocabulary} AS v WHERE v.vid IN (" . $placeholders . ") AND NOT EXISTS (SELECT vid FROM {uuid_vocabulary} WHERE vid = v.vid)", $vids);
     while ($item = db_fetch_object($result)) {
-      db_query("INSERT INTO {uuid_vocabulary} (vid, uuid) VALUES(%d, '%s')", $item->vid, uuid_uuid());
+      _uuid_insert('uuid_vocabulary', 'vid', $item->vid);
     }
 
     // Terms.
     $result = db_query("SELECT td.tid FROM {term_data} AS td WHERE td.vid IN (" . $placeholders . ") AND NOT EXISTS (SELECT tid FROM {uuid_term_data} WHERE tid = td.tid)", $vids);
     while ($item = db_fetch_object($result)) {
-      db_query("INSERT INTO {uuid_term_data} (tid, uuid) VALUES(%d, '%s')", $item->tid, uuid_uuid());
+      _uuid_insert('uuid_term_data', 'tid', $item->tid);
     }
   }
 
diff -up uuid_old/uuid.module uuid/uuid.module
--- uuid_old/uuid.module	2010-09-08 17:07:14.000000000 +0100
+++ uuid/uuid.module	2010-09-18 11:42:21.000000000 +0100
@@ -42,27 +42,27 @@ function uuid_nodeapi(&$node, $op, $teas
       // node is created, node_save(), intentionally with the $node->uuid
       // attribute.
       if (isset($node->uuid) && uuid_is_valid($node->uuid)) {
-        db_query("INSERT INTO {uuid_node} (nid, uuid) VALUES (%d, '%s')", $node->nid, $node->uuid);
+        _uuid_insert('uuid_node', 'nid', $node->nid, $node->uuid);
 
         if (isset($node->revision_uuid) && uuid_is_valid($node->revision_uuid)) {
-          db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, $node->revision_uuid);
+          _uuid_insert('uuid_node_revisions', 'vid', $node->vid, $node->revision_uuid);
         }
         else {
-          db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, uuid_uuid());
+          _uuid_insert('uuid_node_revisions', 'vid', $node->vid);
         }
       }
       else if (in_array($node->type, $automatic_types)) {
-        db_query("INSERT INTO {uuid_node} (nid, uuid) VALUES (%d, '%s')", $node->nid, uuid_uuid());
-        db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, uuid_uuid());
+        _uuid_insert('uuid_node', 'nid', $node->nid);
+        _uuid_insert('uuid_node_revisions', 'vid', $node->vid);
       }
       break;
     case 'update':
       if (isset($node->revision)) {
         if (isset($node->revision_uuid) && uuid_is_valid($node->revision_uuid)) {
-          db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, $node->revision_uuid);
+          _uuid_insert('uuid_node_revisions', 'vid', $node->vid, $node->revision_uuid);
         }
         else {
-          db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, uuid_uuid());
+          _uuid_insert('uuid_node_revisions', 'vid', $node->vid);
         }
       }
       break;
@@ -132,10 +132,10 @@ function uuid_user($op, &$edit, &$user, 
       break;
     case 'insert':
       if (isset($user->uuid) && uuid_is_valid($user->uuid)) {
-        db_query("INSERT INTO {uuid_users} (uid, uuid) VALUES (%d, '%s')", $user->uid, $user->uuid);
+        _uuid_insert('uuid_users', 'uid', $user->uid, $user->uuid);
       }
       else if (variable_get('uuid_automatic_for_users', FALSE)) {
-        db_query("INSERT INTO {uuid_users} (uid, uuid) VALUES (%d, '%s')", $user->uid, uuid_uuid());
+        _uuid_insert('uuid_users', 'uid', $user->uid);
       }
       break;
     case 'update':
@@ -143,7 +143,7 @@ function uuid_user($op, &$edit, &$user, 
         $exists = db_result(db_query('SELECT 1 FROM {uuid_users} WHERE uid = %d', $user->uid));
 
         if (!$exists) {
-          db_query("INSERT INTO {uuid_users} (uid, uuid) VALUES (%d, '%s')", $user->uid, $user->uuid);
+          _uuid_insert('uuid_users', 'uid', $user->uid, $user->uuid);
         }
       }
       break;
@@ -192,8 +192,7 @@ function uuid_taxonomy($op, $type, $arra
         if (empty($array['uuid']) || !uuid_is_valid($array['uuid'])) {
           $array['uuid'] = uuid_uuid();
         }
-        db_query("INSERT INTO {" . $types[$type]['table'] . "} ($field, uuid) VALUES (%d, '%s')",
-          $array[$types[$type]['field']], $array['uuid']);
+        _uuid_insert($types[$type]['table'], $field, $array[$types[$type]['field']], $array['uuid']);
         break;
       case 'delete':
         db_query("DELETE FROM {". $types[$type]['table'] ."} WHERE $field = %d", $array[$types[$type]['field']]);
@@ -211,10 +210,10 @@ function uuid_comment(&$a1, $op) {
     // is added.
     case 'insert':
       if (!empty($a1['uuid']) && uuid_is_valid($a1['uuid'])) {
-        db_query("INSERT INTO {uuid_comments} (cid, uuid) VALUES (%d, '%s')", $a1['cid'], $a1['uuid']);
+        _uuid_insert('uuid_comments', 'cid', $a1['cid'], $a1['uuid']);
       }
       else if (variable_get('uuid_automatic_for_comments', FALSE)) {
-        db_query("INSERT INTO {uuid_comments} (cid, uuid) VALUES (%d, '%s')", $a1['cid'], uuid_uuid());
+        _uuid_insert('uuid_comments', 'cid', $a1['cid']);
       }
       break;
       
@@ -223,7 +222,7 @@ function uuid_comment(&$a1, $op) {
         $exists = db_result(db_query('SELECT 1 FROM {uuid_comments} WHERE cid = %d', $a1['cid']));
 
         if (!$exists && variable_get('uuid_automatic_for_comments', FALSE)) {
-          db_query("INSERT INTO {uuid_comments} (cid, uuid) VALUES (%d, '%s')", $a1['cid'], uuid_uuid());
+          _uuid_insert('uuid_comments', 'cid', $a1['cid']);
         }
       }
       break;
@@ -279,3 +278,18 @@ function uuid_uuid() {
     mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)
   );
 }
+
+/**
+ * Inserts an UUID into the selected table.
+ * Makes sure an alternative UUID is generated if the given one is not unique.
+ */
+function _uuid_insert($table, $ref_field, $ref_value, $uuid = NULL) {
+  $success = FALSE;
+  while (!$success) {
+    if (!$uuid) {
+      $uuid = uuid_uuid();
+    }
+    $success = @db_query("INSERT INTO {" . $table . "} ($ref_field, uuid) VALUES (%d, '%s')", $ref_value, $uuid);
+    $uuid = NULL;
+  }
+} 
