--- modules/og_panels/og_panels.install	2009-07-14 15:17:05.062500000 +0200
+++ modules/og_panels/og_panels_new.install	2009-07-14 15:21:22.156250000 +0200
@@ -100,4 +100,62 @@ function og_panels_update_5001() {
       break;
   }
   return $ret ? $ret : array();
-}
\ No newline at end of file
+}
+
+/**
+ * Update og_panels table and data from Drupal 5 site to Drupal 6.
+ * 
+ * Add the tab_num field, plus add as index, and a primary key
+ * Update all existing records in the table with a tab_num value
+ * To get the tab_num value we just need to select the records in the
+ * correct order, then run through them, resetting the tab_num when we get
+ * to the next nid.
+ */
+function og_panels_update_6000() {
+  $ret = array();
+
+ // Add the tab_num field
+  $new_field = array(
+   'description' => 'Tab number for this node_tab.',
+   'type' => 'int',
+   'unsigned' => TRUE,
+   'not null' => TRUE,
+   'default' => 0,
+  );
+
+// Setup the indexes
+  $keys_new = array(
+    'indexes' => array(
+      'nid' => array('nid'),
+      'page_title' => array('page_title'),
+      'tab_num' => array('tab_num'),
+      'did' => array('did'),
+     ),
+  );
+  // Don't want did to be primary key anymore - will use nid and tab_num
+
+  db_add_field($ret, 'og_panels', 'tab_num', $new_field, $keys_new);
+  
+  // Select all records in the table, order by nid, default_page and weight
+  $result = db_query("SELECT * from {og_panels} ORDER BY nid, default_page DESC, WEIGHT ASC");
+  $old_nid = 0;
+  $tab_num = 0;
+  while ($row = db_fetch_object($result)) {
+   $tab_num ++;
+   $current_nid = $row->nid;
+   $did = $row->did;
+   // Reset the tab_num value to 1 when we move to the next node
+   if ($old_nid != 0 && ($old_nid != $current_nid)) {
+    $tab_num = 1;
+   }
+   $ret[] = update_sql("UPDATE {og_panels} SET tab_num = $tab_num WHERE nid = $current_nid AND did = $did");
+   $old_nid = $current_nid;
+  }
+  
+  // Can only add the new primary key here because there are were no values in
+  // tab_num before, so would cause "duplicate entry for key 1" errors  
+  db_drop_primary_key($ret, 'og_panels');
+  db_add_primary_key($ret, 'og_panels', array('nid', 'tab_num'));
+  
+  return $ret;
+}
