From 95f529d313a7cf859da86a680a6b7fa598b02a68 Mon Sep 17 00:00:00 2001
From: Kevin Atkinson <kevina@cs.utah.edu>
Date: Mon, 8 May 2006 21:50:15 +0000
Subject: [PATCH] Refactor "log" table to move some stuff into a new table.

---
 sql/database-create.sql  | 13 +++++++++++--
 sql/database-fill.sql    |  1 -
 sql/database-migrate.txt | 27 +++++++++++++++++++++++++++
 sql/libtblog.sql         | 12 ++++++++----
 tbsetup/libtblog.pm.in   | 10 ++++------
 5 files changed, 50 insertions(+), 13 deletions(-)

diff --git a/sql/database-create.sql b/sql/database-create.sql
index 2d97ebea5e..b86009249e 100644
--- a/sql/database-create.sql
+++ b/sql/database-create.sql
@@ -1225,8 +1225,6 @@ CREATE TABLE location_info (
 CREATE TABLE log (
   seq int(10) unsigned NOT NULL auto_increment,
   stamp int(10) unsigned NOT NULL default '0',
-  exptidx int(11) NOT NULL default '0',
-  uid int(11) default NULL,
   session int(10) unsigned NOT NULL default '0',
   invocation int(10) unsigned NOT NULL default '0',
   parent int(10) unsigned NOT NULL default '0',
@@ -2116,6 +2114,17 @@ CREATE TABLE scripts (
   UNIQUE KEY id (script_name)
 ) TYPE=MyISAM;
 
+--
+-- Table structure for table `session_info`
+--
+
+CREATE TABLE session_info (
+  session int(11) NOT NULL default '0',
+  uid int(11) NOT NULL default '0',
+  exptidx int(11) NOT NULL default '0',
+  PRIMARY KEY  (session)
+) TYPE=MyISAM;
+
 --
 -- Table structure for table `sitevariables`
 --
diff --git a/sql/database-fill.sql b/sql/database-fill.sql
index 6521cf9fca..0c17592931 100644
--- a/sql/database-fill.sql
+++ b/sql/database-fill.sql
@@ -796,7 +796,6 @@ REPLACE INTO table_regex VALUES ('virt_node_motelog','logfileid','text','regex',
 REPLACE INTO table_regex VALUES ('virt_node_motelog','pid','text','redirect','projects:pid',0,0,NULL);
 REPLACE INTO table_regex VALUES ('virt_node_motelog','eid','text','redirect','experiments:eid',0,0,NULL);
 
-
 --
 -- Dumping data for table `testsuite_preentables`
 --
diff --git a/sql/database-migrate.txt b/sql/database-migrate.txt
index de5056fae1..17df9bb3fa 100644
--- a/sql/database-migrate.txt
+++ b/sql/database-migrate.txt
@@ -3311,3 +3311,30 @@ last_net_act,last_cpu_act,last_ext_act);
 
 4.55: Changes to templates.sql. Skip this revision for now.
 
+4.56: Refactor "log" table to move some stuff into a new table.
+
+      CREATE TABLE session_info (
+        session int(11) NOT NULL default '0',
+        uid int(11) NOT NULL default '0',
+        exptidx int(11) NOT NULL default '0',
+        PRIMARY KEY  (session)
+      );
+
+      lock tables log write;
+
+      insert into session_info
+        select session, uid, exptidx from log 
+        where uid != 0 or exptidx != 0
+        group by session;
+
+      [If either of the above queries failed.  Stop now and fix it as
+       the next will destroy the information.]
+
+      alter table log
+        drop column uid,
+        drop column exptidx;
+
+      [Now install the new code, do it before you unlock the table, as
+       the last statement made an incompatible change to the "log" table]
+
+      unlock tables;
diff --git a/sql/libtblog.sql b/sql/libtblog.sql
index 80f955f10b..7a1379f7b9 100644
--- a/sql/libtblog.sql
+++ b/sql/libtblog.sql
@@ -3,8 +3,6 @@
 CREATE TABLE log (
   seq int unsigned NOT NULL auto_increment,
   stamp int unsigned NOT NULL,   -- timestamp
-  exptidx int NOT NULL,          -- unique experment id
-  uid  int default NULL,         -- numeric user id
   session int unsigned NOT NULL, -- session id (1)
   invocation int unsigned NOT NULL, -- invocation id - unique id for a 
                                  --   particular execution of a script (1)
@@ -38,6 +36,13 @@ CREATE TABLE log (
   KEY (session)
 );
 
+CREATE TABLE session_info (
+  session int NOT NULL,         -- session id (1)
+  uid int default NULL,         -- numeric user id
+  exptidx int NOT NULL,         -- unique experment id
+  PRIMARY KEY  (session)
+);
+
 -- (1) These ids are equal to the sequence number of the "entering" log
 --     message
 -- (2) Due to a number of factors every "exiting" message in not 
@@ -47,8 +52,7 @@ CREATE TABLE log (
 --     multiple exiting messages.  (2) If "exec" is used the script
 --     will not have an exiting message.
 
--- NOTE: It may be beneficial to split this table into three:
---   session pidx uid
+-- NOTE: It may be beneficial to factor out "invocation" specific info:
 --   invocation session parent script level 
 --   seq stamp [session] invocation priority inferred cause type relevant mesg
 -- ...
diff --git a/tbsetup/libtblog.pm.in b/tbsetup/libtblog.pm.in
index b13f256f2e..bebcc5b4b7 100644
--- a/tbsetup/libtblog.pm.in
+++ b/tbsetup/libtblog.pm.in
@@ -490,8 +490,8 @@ sub tblog_set_info ( $$$ )
 	DBQuerySingleFatal("select idx from experiments where pid='$pid' and eid='$eid'");
     $ENV{'TBLOG_UID'} = $uid;
     DBQueryFatal
-	sprintf('update log set exptidx=%d,uid=%s where session=%s',
-		$ENV{TBLOG_EXPTIDX}, $ENV{TBLOG_UID}, $ENV{TBLOG_SESSION});
+	sprintf('replace into session_info values(%d,%d,%d)',
+		$ENV{TBLOG_SESSION}, $ENV{TBLOG_EXPTIDX}, $ENV{TBLOG_UID});
 }
 
 =item tblog_set_default_cause CAUSE
@@ -600,10 +600,8 @@ sub dblog_real ( $$@ ) {
 	  unless defined $cause;
 
 	my $query = 
-	  sprintf('insert into log (stamp,exptidx,uid,session,parent,invocation,script,level,sublevel,priority,inferred,cause,type,mesg) '. 
-		  'VALUES (UNIX_TIMESTAMP(now()),%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s,%s,%s)',
-		  $ENV{TBLOG_EXPTIDX}, 
-		  $ENV{TBLOG_UID},
+	  sprintf('insert into log (stamp,session,parent,invocation,script,level,sublevel,priority,inferred,cause,type,mesg) '. 
+		  'VALUES (UNIX_TIMESTAMP(now()),%d,%d,%d,%d,%d,%d,%d,%d,%s,%s,%s)',
 		  $ENV{TBLOG_SESSION}, 
 		  $ENV{TBLOG_PARENT_INVOCATION}, 
 		  $ENV{TBLOG_INVOCATION},
-- 
GitLab