-- Get runs to publish
SELECT
    client_id,
    stage_id,
    src_label
FROM (
    -- Get diffs to publish
    SELECT
        client_id,
        diff_id AS stage_id,
        label AS src_label
    FROM publishClient
    JOIN diffRun
    WHERE publishClient.stage = 'diff'
        AND publishClient.active = 1
        AND diffRun.state = 'full'
        AND (diffRun.magicked > 0 OR diffRun.diff_mode = 4 OR publishClient.magicked = 0)
    -- WHERE hook %s
    UNION
    -- Get cameras to publish
    SELECT
        client_id,
        cam_id AS stage_id,
        label AS src_label
    FROM publishClient
    JOIN camRun
    WHERE publishClient.stage = 'camera'
        AND publishClient.active = 1
        AND camRun.state = 'full'
        AND (camRun.magicked > 0 OR publishClient.magicked = 0)
    -- WHERE hook %s
    ) AS publishToDo
-- Only get stuff that hasn't been published
LEFT JOIN publishRun USING(client_id, stage_id)
WHERE publishRun.client_id IS NULL
