Index: /Users/jbitner/Sites/myplay/branches/myplay/public_html/sites/all/modules/contrib/activity/activity.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/activity/activity.install,v
retrieving revision 1.1.2.1.2.4
diff -u -r1.1.2.1.2.4 activity.install
--- activity.install	20 Jan 2008 10:15:36 -0000	1.1.2.1.2.4
+++ activity.install	12 Feb 2008 03:19:20 -0000
@@ -78,3 +78,60 @@
   $ret[] = update_sql("ALTER TABLE {activity} ADD COLUMN scope int(11) AFTER timestamp");
   return $ret;
 }
+
+function activity_update_4() {
+  $ret = array();
+  $ret[] = update_sql("ALTER TABLE {activity} CHANGE `tokens` `data` longtext NOT NULL");
+  $ret[] = update_sql("ALTER TABLE {activity} CHANGE `action` `operation` varchar(25) NOT NULL default ''");
+  $ret[] = update_sql("ALTER TABLE {activity} CHANGE `timestamp` `created` int(11) NOT NULL");
+  
+  if (!db_table_exists('activity_targets')) {  
+    $ret[] = update_sql(
+      "CREATE TABLE {activity_targets} (
+        aid int(11) NOT NULL,
+        target_uid int(11) NOT NULL,
+        target_role varchar(50) NOT NULL default '',
+        PRIMARY KEY (aid, target_uid),
+        KEY (target_uid, target_role),
+        KEY (target_role)
+      ) /*!40100 DEFAULT CHARACTER SET UTF8 */
+    ");
+    //since we're inserting the table in an update we need to also insert some values for the
+    //  already existing activity records.
+    // TODO: first make sure there are not a HUGE number of records in activity table
+    $query = db_query("SELECT COUNT(aid) FROM {activity}");
+    if (db_result($query) > 100000) {
+      // let's try to truncate this a bit, shall we?
+      // first let's get rid of any anonymous records
+      $ret[] = update_sql("DELETE FROM {activity} WHERE uid = 0");
+      
+      // now let's go through and only keep 20 records per user   
+      $query = db_query("SELECT aid, uid, data FROM {activity} ORDER BY uid, aid DESC");    
+      while ($result = db_fetch_object($query)) {
+        $activities[$result->uid][] = array('aid' => $result->aid, 'uid' => $result->uid, 'data' => $result->data);
+      }
+      foreach ($activities as $uid => $activity) {
+        foreach ($activity as $count => $data) {
+          if ($count == 19) {            
+            $ret[] = update_sql("DELETE FROM {activity} WHERE aid < ". $data['aid'] ." AND uid = ". $data['uid']);
+          }
+        }
+      }      
+    }
+    // here's where we put in some default target data.
+    // this might not always be true tho as it only assumes two target roles
+    //  and a module might define more but this'll get you some data at least
+    $query = db_query("SELECT aid, uid FROM {activity} ORDER BY uid, aid DESC");
+    while ($result = db_fetch_object($query)) {
+      $ret[] = update_sql("INSERT INTO {activity_targets} (aid, target_uid, target_role)
+                                                  VALUES (". $result->aid .", -1, 'all')");
+      $ret[] = update_sql("INSERT INTO {activity_targets} (aid, target_uid, target_role)
+                                                  VALUES (". $result->aid .", ". $result->uid .", 'author')");
+    }    
+  }
+  
+  // now we get rid of the uid 'cuz robert says so ;)
+  $ret[] = update_sql("ALTER TABLE {activity} DROP `uid`");
+  
+  return $ret;
+}
