Index: trunk/ippTools/src/magicdstool.c
===================================================================
--- trunk/ippTools/src/magicdstool.c	(revision 30668)
+++ trunk/ippTools/src/magicdstool.c	(revision 30671)
@@ -709,6 +709,9 @@
 
 
+// update the magicked column for the underlying component
+// Note: Must be called inside a transaction
+
 static bool
-setMagicked(pxConfig *config, psS64 magic_ds_id, psString component)
+setMagicked(pxConfig *config, psS64 magic_ds_id, psString component, bool clearMagicked)
 {
     // first query the magicDSRun to find the stage and the stage_id
@@ -741,47 +744,64 @@
     psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
     psS64 magic_id = psMetadataLookupS64(NULL, row, "magic_id");
+    psFree(output);
+
+    psS64 newMagickedValue;
+    if (clearMagicked) {
+        newMagickedValue = 0;
+    } else {
+        newMagickedValue = magic_id;
+    }
 
     ippStage stageNum = ippStringToStage(stage);
 
     // chose the appropriate query based on the stage
+    char *clearRunQuery = NULL;
     switch (stageNum) {
     case IPP_STAGE_RAW:
         query = "UPDATE rawImfile SET magicked = %" PRId64 " where exp_id = %" PRId64 " AND class_id = '%s'";
+        clearRunQuery = "UPDATE rawExp SET magicked = 0 where exp_id = %" PRId64;
         break;
     case IPP_STAGE_CHIP:
         query = "UPDATE chipProcessedImfile SET magicked = %" PRId64 " where chip_id = %" PRId64 " AND class_id = '%s'";
+        clearRunQuery = "UPDATE chipRun set magicked = 0 where chip_id = %" PRId64;
         break;
     case IPP_STAGE_CHIP_BG:
         query = "UPDATE chipBackgroundImfile SET magicked = %" PRId64 " where chip_bg_id = %" PRId64 " AND class_id = '%s'";
+        clearRunQuery = "UPDATE chipBackgroundRun SET magicked = 0 where chip_bg_id = %" PRId64;
         break;
     case IPP_STAGE_CAMERA:
-        // no there is no magicked column in camProcessedExp so we have nothing to do
-        psFree(output);
-        return true;
+        psFree(query);
+        query = NULL;
+        clearRunQuery = "UPDATE chipBackgroundRun SET magicked = 0 where chip_bg_id = %" PRId64;
+        break;
     case IPP_STAGE_WARP:
         query = "UPDATE warpSkyfile SET magicked = %" PRId64 " where warp_id = %" PRId64 " AND skycell_id = '%s'";
+        clearRunQuery = "UPDATE warpRun SET magicked = 0 where warp_id = %" PRId64;
         break;
     case IPP_STAGE_WARP_BG:
         query = "UPDATE warpBackgroundSkyfile SET magicked = %" PRId64 " where warp_bg_id = %" PRId64 " AND skycell_id = '%s'";
+        clearRunQuery = "UPDATE warpBackgroundRun SET magicked = 0 where warp_bg_id = %" PRId64;
         break;
     case IPP_STAGE_DIFF:
         query = "UPDATE diffSkyfile SET magicked = %" PRId64 " where diff_id = %" PRId64 " AND skycell_id = '%s'";
+        clearRunQuery = "UPDATE diffRun SET magicked = 0 where diff_id = %" PRId64;
         break;
     default:
         psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
-        psFree(output);
-        return false;
-    }
-
-    if (!p_psDBRunQueryF(config->dbh, query, magic_id, stage_id, component)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-    psFree(output);
-
-    psU64 affected = psDBAffectedRows(config->dbh);
-    if (affected != 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected 1 row");
-        return false;
+        return false;
+    }
+
+    if (query) {
+        if (!p_psDBRunQueryF(config->dbh, query, newMagickedValue, stage_id, component)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+    }
+
+    if (clearMagicked && clearRunQuery) {
+        if (!p_psDBRunQueryF(config->dbh, clearRunQuery, stage_id)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
     }
 
@@ -896,5 +916,5 @@
     if (setmagicked) {
         // set the image file's magicked flag
-        if (!setMagicked(config, magic_ds_id, component)) {
+        if (!setMagicked(config, magic_ds_id, component, false)) {
             psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
             if (!psDBRollback(config->dbh)) {
@@ -1111,4 +1131,6 @@
     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
 
+    PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id", true, false);
+    PXOPT_LOOKUP_STR(component, config->args, "-component", true, false);
     PXOPT_LOOKUP_STR(state, config->args, "-state", false, false);
     PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
@@ -1116,6 +1138,7 @@
     psString queryFile = NULL;
     bool stateIsUpdate = false;
+    bool toRestored = !strcmp(state, "goto_restored");
     if (state) {
-        if (! strcmp(state, "new") || !strcmp(state, "goto_restored")) {
+        if (! strcmp(state, "new") || toRestored) {
             queryFile = "magicdstool_revertdestreakedfile.sql";
         } else if (!strcmp(state, "update")) {
@@ -1153,4 +1176,8 @@
     }
 
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
     if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "failed to revert");
@@ -1159,4 +1186,18 @@
     }
     psFree(query);
+    if (toRestored) {
+        // clear the underlying component's magicked value
+        if (!setMagicked(config, magic_ds_id, component, true)) {
+            psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+    }
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
     return true;
 }
@@ -1661,8 +1702,7 @@
     }
     if (!strcmp(data_state, "full")) {
-        // if -tofullfile optoinally set the magicked value for the component
-        PXOPT_LOOKUP_BOOL(setmagicked, config->args, "-setmagicked", false);
-        // set the image file's magicked flag
-        if (!setMagicked(config, magic_ds_id, component)) {
+        // set the component's magicked flag
+        
+        if (!setMagicked(config, magic_ds_id, component, false)) {
             psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
             if (!psDBRollback(config->dbh)) {
@@ -1716,14 +1756,4 @@
     return change_file_data_state(config, "full");
 }
-/*
-static bool topurgedimfileMode(pxConfig *config)
-{
-    return change_imfile_data_state(config, "purged", "goto_purged");
-}
-static bool toscrubbedfileMode(pxConfig *config)
-{
-  return change_file_data_state(config, "scrubbed", "goto_scrubbed");
-}
-*/
 
 // a very specfic function to queue a cleaned magicDSFile to be updated
