SELECT
    magicRun.magic_id,
    exp_id,
    'diff' AS stage,
    diff_id AS stage_id,
    0 AS cam_id,
    magicRun.label,
    magicRun.workdir
FROM magicRun
    JOIN magicMask USING(magic_id)
    JOIN diffRun USING(diff_id)
    LEFT JOIN magicDSRun ON magicRun.magic_id = magicDSRun.magic_id
                         AND magicDSRun.stage  = 'diff'
WHERE magicRun.state = 'full'
    AND ( -- rerun HOOK magicdstool sends "\n1 " if rerun else "\n0 " %s
        OR magicDSRun.magic_ds_id IS NULL)
    AND diffRun.magicked  = 0
    AND diffRun.state = 'full'

