Generating UUIDs in PHP (for Drupal)

I have begun my quest to convert Drupal over to using UUIDs instead of IDs so that merging/branching drupal databases will be easier.

The first thing to figure out is how best to generate UUIDs in PHP. The naive pure-php approaches that use rand() have quickly proven to be non-viable (generating 10 of them hasn’t completed after several minutes), at least on my test virtual machine, presumably on account of running out of randomness.

The OSSP UUID module for PHP5 is much better, generating 60k “type 4” (completely random) uuids per second. However, the memory usage grows the more UUIDs you generate, so that could be an issue. So despite the fact that this module implies a C module that has to be available to PHP, I’m going to use it.

I will try to keep my implementation open to creating UUIDs in the DB, as PostgreSQL uses the same library to create UUIDs, so no sense in reinventing the wheel. MySQL UUIDs are broken in any sort of replicated environment (in that you can get the same UUID repeatedly from a given SELECT UUID();) so I must have a language-level version available.

So now that I’ve elected to settle on the OSSP UUID module, I can take the next steps towards making it work in Drupal.

One thought on “Generating UUIDs in PHP (for Drupal)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s