$sql = 'SELECT majorid FROM sq_shdw_ast_lnk where minorid = ' . MatrixDAL::quote($new_dn);
$existing_links = MatrixDAL::executeSqlAssoc($sql);
foreach ($existing_links as $index => $existing_link) {
    $existing_links[] = $existing_links[$index]['majorid'];
    unset($existing_links[$index]);
}
// update the dn here but be sure to not to try to insert
// duplicate entries incase the updated ldap user is already
// linked to target asset from backend for more info see bug
// 5686 LDAP update script fails if unique constraint violated
$sql = 'UPDATE sq_shdw_ast_lnk
			SET minorid = ' . MatrixDAL::quote($new_dn) . '
			WHERE minorid = ' . MatrixDAL::quote($old_dn) . '
			AND majorid NOT IN (SELECT s.majorid FROM sq_shdw_ast_lnk s where s.minorid = ' . MatrixDAL::quote($new_dn) . ')';
$result = MatrixDAL::executeSql($sql);
MatrixDAL::executeQuery('core', 'changeShadowLinkUpdatedDateUser', $bind_vars);
printActionStatus('OK');
printActionName('Changing shadow links (rollback)');
$sql = 'UPDATE sq_rb_shdw_ast_lnk
			SET minorid = ' . MatrixDAL::quote($new_dn) . '
			WHERE minorid = ' . MatrixDAL::quote($old_dn);
$result = MatrixDAL::executeSql($sql);
$sql = 'UPDATE sq_rb_shdw_ast_lnk
			SET updated_userid = ' . MatrixDAL::quote($new_dn) . '
			WHERE updated_userid = ' . MatrixDAL::quote($old_dn);
$result = MatrixDAL::executeSql($sql);
printActionStatus('OK');
printActionName('Changing asset permissions');
$sql = 'UPDATE sq_ast_perm
				SET userid = ' . MatrixDAL::quote($new_dn) . '
				WHERE userid = ' . MatrixDAL::quote($old_dn);
				sort_order = sort_order - 1
			WHERE
					majorid		= :majorid
				AND	sort_order	> :sort_order';
    $update_sort_order_query = MatrixDAL::preparePdoQuery($sql);
    try {
        MatrixDAL::bindValueToPdo($update_sort_order_query, 'majorid', $link['majorid']);
        MatrixDAL::bindValueToPdo($update_sort_order_query, 'sort_order', $link['sort_order']);
        MatrixDAL::execPdoQuery($update_sort_order_query);
    } catch (Exception $e) {
        trigger_error('Unable to update sort orders for majorid: ' . $link['majorid'] . ' due to database error: ' . $e->getMessage(), E_USER_ERROR);
    }
    // Delete from the link table
    try {
        $bind_vars = array('linkid' => $link['linkid'], 'majorid' => $link['majorid']);
        MatrixDAL::executeQuery('core', 'deleteLink', $bind_vars);
    } catch (Exception $e) {
        trigger_error('Unable to delete link with linkid: ' . $link['linkid'] . ' due to database error: ' . $e->getMessage(), E_USER_ERROR);
    }
    $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
    echo "Deleted Link ID: " . $link['linkid'] . " with Major ID: " . $link['majorid'] . " and Minor ID: " . $link['minorid'] . "\n";
}
// end foreach link
$GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
$GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db');
$sql = 'SELECT count(*)
		FROM sq_ast a
		WHERE NOT EXISTS (SELECT linkid FROM sq_ast_lnk WHERE minorid = a.assetid OR majorid = a.assetid)';
try {
    $query = MatrixDAL::preparePdoQuery($sql);
    $orphans = DAL::executePdoOne($query);