Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 11976)
+++ trunk/ippTools/src/stacktool.c	(revision 11983)
@@ -39,5 +39,5 @@
 static bool sumscfileMode(pxConfig *config);
 
-static bool setp6RunState(pxConfig *config, const char *p4_id, const char *state);
+static bool setp6RunState(pxConfig *config, const char *p6_id, const char *state);
 
 # define MODECASE(caseName, func) \
@@ -104,4 +104,24 @@
     }
 
+    psString skycell_id = psMetadataLookupStr(&status, config->args, "-skycell_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -skycell_id");
+        return false;
+    }
+    if (!skycell_id) {
+        psError(PS_ERR_UNKNOWN, true, "-skycell_id is required");
+        return false;
+    }
+
+    psString tess_id = psMetadataLookupStr(&status, config->args, "-tess_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -tess_id");
+        return false;
+    }
+    if (!tess_id) {
+        psError(PS_ERR_UNKNOWN, true, "-tess_id is required");
+        return false;
+    }
+
     psTime *registered = NULL;
     {
@@ -124,5 +144,7 @@
             workdir,
             NULL,       // dvodb
-            registered
+            registered,
+            skycell_id,
+            tess_id
     );
     psFree(registered);
@@ -223,23 +245,4 @@
     }
 
-    psString skycell_id = psMetadataLookupStr(&status, config->args, "-skycell_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -skycell_id");
-        return false;
-    }
-    if (!skycell_id) {
-        psError(PS_ERR_UNKNOWN, true, "-skycell_id is required");
-        return false;
-    }
-
-    psString tess_id = psMetadataLookupStr(&status, config->args, "-tess_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -tess_id");
-        return false;
-    }
-    if (!tess_id) {
-        psError(PS_ERR_UNKNOWN, true, "-tess_id is required");
-        return false;
-    }
 
     // XXX need to validate the p4_id here
@@ -248,7 +251,5 @@
     if (!p6InputScfileInsert(config->dbh, 
             (psS32)atoi(p6_id),
-            (psS32)atoi(p4_id),
-            skycell_id,
-            tess_id
+            (psS32)atoi(p4_id)
         )) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -279,7 +280,7 @@
         "   USING(p6_id)\n"
         " JOIN p4Scfile\n"
-        "   ON p6InputScfile.p4_id = p4Scfile.p4_id\n"
-        "   AND p6InputScfile.skycell_id = p4Scfile.skycell_id\n"
-        "   AND p6InputScfile.tess_id = p4Scfile.tess_id\n"
+        "   ON  p6InputScfile.p4_id = p4Scfile.p4_id\n"
+        "   AND p6Run.skycell_id    = p4Scfile.skycell_id\n"
+        "   AND p6Run.tess_id       = p4Scfile.tess_id\n"
         " WHERE\n"
         "   p6Run.state = 'run'\n" 
@@ -454,24 +455,4 @@
     }
 
-    psString skycell_id = psMetadataLookupStr(&status, config->args, "-skycell_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -skycell_id");
-        return false;
-    }
-    if (!skycell_id) {
-        psError(PS_ERR_UNKNOWN, true, "-skycell_id is required");
-        return false;
-    }
-
-    psString tess_id = psMetadataLookupStr(&status, config->args, "-tess_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -tess_id");
-        return false;
-    }
-    if (!tess_id) {
-        psError(PS_ERR_UNKNOWN, true, "-tess_id is required");
-        return false;
-    }
-
     psString uri = psMetadataLookupStr(&status, config->args, "-uri");
     if (!status) {
@@ -494,4 +475,9 @@
     if (!status) {
         psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg_mean_stdev");
+        return false;
+    }
+
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
         return false;
     }
@@ -502,10 +488,25 @@
     if (!p6SumScfileInsert(config->dbh, 
             (psS32)atoi(p6_id),
-            skycell_id,
-            tess_id,
             uri,
             bg,
             bg_mean_stdev
         )) {
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    if (setp6RunState(config, p6_id, "stop")) {
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
+        psError(PS_ERR_UNKNOWN, false, "failed to change p6Run's state");
+        return false;
+    }
+
+    // point of no return
+    if (!psDBCommit(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         return false;
