=== modified file 'uuid/uuid.admin.inc' --- uuid/uuid.admin.inc 2009-06-26 03:51:19 +0000 +++ uuid/uuid.admin.inc 2009-06-26 04:41:16 +0000 @@ -57,13 +57,32 @@ */ function uuid_sync() { + // UUID() is non-deterministic and breaks with replication. + // To combat this we need to iterate through all items and statically set a uuid. + if (variable_get('uuid_automatic_for_users', FALSE)) { - db_query("INSERT INTO {uuid_users} SELECT u.uid, UUID() FROM {users} AS u WHERE NOT EXISTS (SELECT uid FROM {uuid_users} WHERE uid = u.uid)"); + $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()); + } } - foreach (variable_get('uuid_automatic_for_nodes', array()) as $type) { - db_query("INSERT INTO {uuid_node} SELECT n.nid, UUID() FROM {node} AS n WHERE n.type = '%s' AND NOT EXISTS (SELECT nid FROM {uuid_node} WHERE nid = n.nid)", $type); - db_query("INSERT INTO {uuid_node_revisions} SELECT nr.vid, UUID() FROM {node_revisions} AS nr INNER JOIN {node} n ON nr.nid = n.nid WHERE n.type = '%s' AND NOT EXISTS (SELECT vid FROM {uuid_node_revisions} WHERE vid = nr.vid)", $type); + foreach(variable_get('uuid_automatic_for_nodes', array()) as $type) { + + // Slightly ugly, but it does save some queries for + // all the node types on which we don't want a UUID. + if($type == '0') + continue; + + $nid_result = db_query("SELECT nid FROM {node} WHERE type = '%s' AND nid NOT IN (SELECT nid FROM {uuid_node})", $type); + while($item = db_fetch_object($nid_result)) { + db_query("INSERT INTO {uuid_node} (nid, uuid) VALUES(%d, '%s')", $item->nid, uuid_uuid()); + } + + $vid_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 = '%s' AND nr.vid NOT IN(SELECT vid FROM {uuid_node_revisions})", $type); + while($item = db_fetch_object($vid_result)) { + db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES(%d, '%s')", $item->vid, uuid_uuid()); + } } drupal_set_message(t("UUID tables have been updated.")); === modified file 'uuid/uuid.module' --- uuid/uuid.module 2009-06-26 03:51:19 +0000 +++ uuid/uuid.module 2009-06-26 04:44:21 +0000 @@ -43,12 +43,12 @@ db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, $node->revision_uuid); } else { - db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, UUID())", $node->vid); + db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, uuid_uuid()); } } else if (in_array($node->type, $automatic_types)) { - db_query('INSERT INTO {uuid_node} (nid, uuid) VALUES (%d, UUID())', $node->nid); - db_query('INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, UUID())', $node->vid); + 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()); } break; case 'update': @@ -57,7 +57,7 @@ db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, $node->revision_uuid); } else { - db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, UUID())", $node->vid); + db_query("INSERT INTO {uuid_node_revisions} (vid, uuid) VALUES (%d, '%s')", $node->vid, uuid_uuid()); } } break; @@ -163,6 +163,27 @@ } /** + * Returns a new formatted UUID string. + * Blatantly copied from http://php.net/manual/en/function.uniqid.php#65879 + * + * @return $string + */ +function uuid_uuid() { + // The field names refer to RFC 4122 section 4.1.2 + + return sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x', + mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low" + mt_rand(0, 65535), // 16 bits for "time_mid" + mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version" + bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)), + // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res" + // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d) + // 8 bits for "clk_seq_low" + mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node" + ); +} + +/** * Determines if a UUID is valid. */ function uuid_is_valid($uuid) {