commit ef32159da41d613a8b535e799c76cda5dd75d788
Author: andrew morton <drewish@zivtech.com>
Date:   Mon Sep 19 18:47:14 2011 -0400

    Make sure that {webform_submissions}.bundle gets set by webform_entity_install().

diff --git a/webform_entity.install b/webform_entity.install
index 57e8e2c..231616f 100644
--- a/webform_entity.install
+++ b/webform_entity.install
@@ -78,32 +78,20 @@ function webform_entity_install() {
     'description' => 'The bundle used by the webform_entity module.',
   );
   db_add_field('webform_submissions', 'bundle', $field);
-  // We need to add our bundle info.
-  $submissions = db_select('webform_submissions', 'ws')
-    ->fields('ws', array('nid', 'sid'))
-    ->execute()
-    ->fetchAllKeyed(1, 0);
-  $nids = array();
-  if ($nids) {
-    foreach ($submissions as $sid => $nid) {
-      $nids[] = $nid;
-    }
-    $node_types = db_select('node', 'n')
-      ->fields('n', array('nid', 'type'))
-      ->condition('nid', $nids, 'IN')
-      ->execute()
-      ->fetchAllKeyed();
-    $type_updates = array();
-    foreach ($node_types as $nid => $type) {
-      $type_updates[$type][] = $nid;
-    }
-    foreach ($type_updates as $type => $nids) {
-      db_update('webform_submissions')
-        ->fields(array('bundle' => $type))
-        ->condition('nid', $nids, 'IN')
-        ->execute();
-    }
-  } 
+  // We need to add our bundle info. Find all the node types of submissions that
+  // don't have bundles and then run an update for each node.
+  $result = db_query('SELECT DISTINCT ws.nid, n.type
+    FROM {webform_submissions} ws
+    INNER JOIN {node} n ON ws.nid = n.nid
+    WHERE ws.bundle IS NULL');
+  foreach($result->fetchAllKeyed() as $nid => $type) {
+    // This could probably be optimized by grouping the nids by node type and
+    // running one query for each.
+    db_update('webform_submissions')
+      ->fields(array('bundle' => $type))
+      ->condition('nid', $nid)
+      ->execute();
+  }
 }
 
 /**
