Index: trunk/ippTools/src/magicdstool.c
===================================================================
--- trunk/ippTools/src/magicdstool.c	(revision 23389)
+++ trunk/ippTools/src/magicdstool.c	(revision 23438)
@@ -40,5 +40,5 @@
 
 static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, const char *state);
-static bool magicDSRunComplete(pxConfig *config);
+static bool magicDSRunComplete(pxConfig *config, bool setmagicked);
 static bool magicDSGetIDs(pxConfig *config, psString stage, psS64 magic_id, psS64 *stage_id, psS64 *cam_id);
 
@@ -528,4 +528,66 @@
 }
 
+static bool
+setRunMagicked(pxConfig *config, psS64 magic_ds_id)
+{
+    // first query the magicDSRun to find the stage and the stage_id
+    psString query = "SELECT stage, stage_id from magicDSRun where magic_ds_id = %" PRId64;
+
+    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psError(PS_ERR_UNKNOWN, true, "magicDSRun not found for magic_ds_id %" PRId64, magic_ds_id);
+        psFree(output);
+        return false;
+    }
+    if (psArrayLength(output) > 1) {
+        psError(PS_ERR_UNKNOWN, true, "unexpected number of rows found %ld for magic_ds_id %" PRId64,
+            psArrayLength(output), magic_ds_id);
+        psFree(output);
+        return false;
+    }
+    psMetadata *row = output->data[0];
+
+    psString stage = psMetadataLookupStr(NULL, row, "stage");
+    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
+
+
+    // chose the appropriate query based on the stage
+    if (!strcmp(stage, "raw")) {
+        query = "UPDATE rawExp SET magicked = 1 where exp_id = %" PRId64;
+    } else if (!strcmp(stage, "chip")) {
+        query = "UPDATE chipRun SET magicked = 1 where chip_id = %" PRId64;
+    } else if (!strcmp(stage, "warp")) {
+        query = "UPDATE warpRun SET magicked = 1 where warp_id = %" PRId64;
+    } else if (!strcmp(stage, "diff")) {
+        query = "UPDATE diffRun SET magicked = 1 where diff_id = %" PRId64;
+    } else {
+        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
+        psFree(output);
+        return false;
+    }
+    if (!p_psDBRunQueryF(config->dbh, query, stage_id)) {
+        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 true;
+}
+
 static bool adddestreakedfileMode(pxConfig *config)
 {
@@ -553,6 +615,10 @@
 
     if (setmagicked) {
+        // set the image file's magicked flag
         if (!setMagicked(config, magic_ds_id, component)) {
             psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
             return false;
         }
@@ -568,5 +634,5 @@
     }
 
-    if (!magicDSRunComplete(config)) {
+    if (!magicDSRunComplete(config, setmagicked)) {
             // rollback
         if (!psDBRollback(config->dbh)) {
@@ -593,5 +659,5 @@
 
     if (!strcmp(stage, "diff")) {
-        // don't need these ids for diff stage
+        // don't need these ids for diff stage because diff_id is in the magicRun
         *stage_id = 0;
         *cam_id = 0;
@@ -654,5 +720,5 @@
 }
 
-static bool magicDSRunComplete(pxConfig *config)
+static bool magicDSRunComplete(pxConfig *config, bool setmagicked)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -686,4 +752,11 @@
 
         psS64 magic_ds_id = psMetadataLookupS64(NULL, row, "magic_ds_id");
+
+        // if requested, set stageRun.magicked
+        if (setmagicked && !setRunMagicked(config, magic_ds_id)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to change stageRun.magicked for magic_ds_id: %" PRId64,
+                magic_ds_id);
+            return false;
+        }
 
         // set magicDSRun.state to 'full'
