protected function proceedChangeSetsNeededByRelation()
    {
        $dbHandle = $this->getDatabase()->getDatabaseConnection();

        $tmpTable = Database::TABLE_TMP_DEPENDS_NEEDED;
        $neededByTable = Database::TABLE_CHANGESET_NEEDEDBY;
        $changeSetTable = Database::TABLE_CHANGESET;
        $patchSetTable = Database::TABLE_PATCHSET;

        $query = '
            INSERT INTO ' . $neededByTable . ' (`changeset`, `needed_by`, `tstamp`, `crdate`)
            SELECT
                ' . $changeSetTable . '.`id`,
                neededByChangeset.`id`,
                UNIX_TIMESTAMP(),
                UNIX_TIMESTAMP()
            FROM
                ' . $tmpTable . '
                INNER JOIN ' . $changeSetTable . ' AS neededByChangeset ON (
                    ' . $tmpTable . '.`identifier` = neededByChangeset.`identifier`
                    AND ' . $tmpTable . '.`number` = neededByChangeset.`number`
                )
                INNER JOIN gerrit_patchset ON (
                    neededByChangeset.`id` = ' . $patchSetTable . '.`changeset`
                    AND ' . $patchSetTable . '.`revision` = ' . $tmpTable . '.`revision`
                    AND ' . $patchSetTable . '.`ref` = ' . $tmpTable . '.`ref`
                )
                INNER JOIN ' . $changeSetTable . ' ON (
                    ' . $changeSetTable . '.`id` = ' . $tmpTable . '.`changeset`
                )
            WHERE
                ' . $tmpTable . '.`status` = ' . intval(Database::TMP_DEPENDS_NEEDED_STATUS_NEEDEDBY) . '
            GROUP BY
                ' . $changeSetTable . '.`id`, neededByChangeset.`id`
            ON DUPLICATE KEY UPDATE
                ' . $neededByTable . '.`tstamp` = UNIX_TIMESTAMP()';

        $dbHandle->exec($query);
    }