SELECT DISTINCT
    magicDSRun.magic_ds_id,
    magicRun.magic_id,
    magicRun.exp_id,
    magicDSRun.label,
    rawExp.camera,
    magicMask.uri as streaks_uri,
    CAST(NULL AS CHAR(255)) AS inv_streaks_uri,
    stage,
    stage_id,
    rawExp.magicked,
    class_id as component,
    rawImfile.uri AS uri,
    -- XXX: replace this with rawImfile.path_base once it exists
    TRIM(TRAILING '.fits' FROM rawImfile.uri) AS path_base,
    magicRun.inverse,
    camProcessedExp.path_base as cam_path_base,
    camRun.reduction AS cam_reduction,
    outroot,
    recoveryroot,
    re_place,
    remove
FROM magicDSRun
JOIN magicMask USING (magic_id)
JOIN magicRun USING (magic_id)
JOIN camRun USING(cam_id)
JOIN camProcessedExp USING(cam_id)
JOIN rawExp ON magicRun.exp_id = rawExp.exp_id
JOIN rawImfile ON rawExp.exp_id = rawImfile.exp_id
LEFT JOIN magicDSFile
    ON magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
    AND magicDSFile.component = rawImfile.class_id
WHERE
    magicDSRun.state = 'new'
    AND magicDSRun.stage = 'raw'
    AND magicDSFile.component IS NULL
