<?php
	# Mantis - a php based bugtracking system
	# Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
	# Copyright (C) 2002 - 2004  Mantis Team   - mantisbt-dev@lists.sourceforge.net
	# This program is distributed under the terms and conditions of the GPL
	# See the README and LICENSE files for details

	# Changes applied to 0.18 database

	# --------------------------------------------------------
	# $Id: 0_18_inc.php,v 1.12 2004/07/15 21:51:16 vboctor Exp $
	# --------------------------------------------------------
?>
<?php
	require_once( 'db_table_names_inc.php' );

	$upgrades = array();

	$query = "SELECT id, duplicate_id
			   FROM $t_bug_table
			   WHERE duplicate_id != '';";
	$result = db_query( $query );
	$t_count = db_num_rows( $result );

	for ( $i = 0 ; $i < $t_count ; $i++ ) {
		$t_bug = db_fetch_array( $result );
		$t_bug_id = $t_bug['id'];
		$t_duplicate_bug_id = $t_bug['duplicate_id'];

		$query = "SELECT id
					FROM $t_bug_relationship_table
					WHERE
						relationship_type = '" . BUG_DUPLICATE . "' and
						source_bug_id = '$t_duplicate_bug_id' and
						destination_bug_id = '$t_bug_id';";
		$result2 = db_query( $query );
		$t_count2 = db_num_rows( $result2 );

		if( $t_count2 != 1 ) {
			continue;
		}

		$t_relationship = db_fetch_array( $result2 );
		$t_relationship_id = $t_relationship['id'];

		$query = "UPDATE $t_bug_relationship_table
			SET source_bug_id = '$t_bug_id',
			destination_bug_id = '$t_duplicate_bug_id'
			WHERE id='$t_relationship_id'";
		db_query( $query );
	}

	echo 'done.';
?>