Skip to content

Instantly share code, notes, and snippets.

@darrylhein
Created May 2, 2012 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrylhein/2573767 to your computer and use it in GitHub Desktop.
Save darrylhein/2573767 to your computer and use it in GitHub Desktop.
Copy data from another MantisBT into another install
<?php
// migrates the data from one mantis install to another
// make sure to backup your data before running, just incase!!!!
// will keep the bug id (ie, mantis id)
// before importing, make sure that the bug ids won't conflict (ie, mantis_bug_table)
// because this may also migrate the files inside the mantis tables, run this at the command line with the memory set: php -d memory_limit=256M run.php
// todo: only include the bugnote_text and bug_text records that have associated bugnotes and bugs (right now it copies all of them)
// db connection params
$db_server = 'localhost';
$db_username = 'mantis';
$db_password = '`password';
$db_source = 'mantis_source';
$db_destination = 'mantis_destination';
// tables to migrate
// all tables prefixed with $db_prefix and suffixed with $db_suffix
$source_db_prefix = 'source_';
$source_db_suffix = '_table';
$destination_db_prefix = 'mantis_';
$destination_db_suffix = '_table';
// projects IDs to migrate
// leave empty for all projects
$projects_to_migrate = array();
// includes or excludes the text field in the custom_field_string table
// v1.2 doesn't have the field; it's a new field in v1.3
$include_custom_field_string_text_field = FALSE;
// old and new user ids
// only these users will be migrated
$user_old_new = array(
   <orig user id> => <new/existing user id>
);
// this is the user that will be used if the user doesn't exist in the db
$default_user_id = 2;
$_tables = array(
   'bugnote',
   'bugnote_text',
   'bug_file',
   'bug_history',
   'bug_relationship',
   'bug_revision',
   'bug',
   'bug_text',
   'category',
   'custom_field_project',
   'custom_field_string',
   'custom_field',
   'project_hierarchy',
   'project',
   'project_user_list',
   'project_version',
);
$source_tables = array();
$destination_tables = array();
foreach ($_tables as $i => $table) {
   $source_tables[$table] = $source_db_prefix . $table . $source_db_suffix;
   $destination_tables[$table] = $destination_db_prefix . $table . $destination_db_suffix;
}
// connect to databases
$source_connection = mysql_connect($db_server, $db_username, $db_password, TRUE);
if ( ! $source_connection) {
   die('Could not connect to source database');
}
$db_selected = mysql_select_db($db_source, $source_connection);
if ( ! $db_selected) {
   die ('Can\'t select db: ' . mysql_error());
}
$destination_connection = mysql_connect($db_server, $db_username, $db_password, TRUE);
if ( ! $destination_connection) {
   die('Could not connect to destination database');
}
$db_selected = mysql_select_db($db_destination, $destination_connection);
if ( ! $db_selected) {
   die ('Can\'t select db: ' . mysql_error());
}
// ******************** Projects ************************
// get projects
if ( ! empty($projects_to_migrate)) {
   $project_where = " WHERE id IN (" . implode(',', $projects_to_migrate) . ")";
} else {
   $project_where = '';
}
$project_result = mysql_query("SELECT * FROM `{$source_tables['project']}`{$project_where}", $source_connection);
if ( ! $project_result) {
   die('Unable to retrive the projects: ' . mysql_error($source_connection));
}
$projects = array();
while ($row = mysql_fetch_assoc($project_result)) {
   $projects[$row['id']] = $row;
}
if (empty($projects)) {
   die('No project found to migrate');
}
// get project users
$project_user_list_result = mysql_query("SELECT * FROM `{$source_tables['project_user_list']}` WHERE user_id IN (" . implode(',', array_keys($user_old_new)) . ") AND project_id IN (" . implode(',', array_keys($projects)) . ")", $source_connection);
if ( ! $project_user_list_result) {
   die('Unable to retrive the project_user_list: ' . mysql_error($source_connection));
}
$project_user_lists = array();
while ($row = mysql_fetch_assoc($project_user_list_result)) {
   $project_user_lists[] = $row;
}
// get project versions
$project_version_result = mysql_query("SELECT * FROM `{$source_tables['project_version']}` WHERE project_id IN (" . implode(',', array_keys($projects)) . ")", $source_connection);
if ( ! $project_version_result) {
   die('Unable to retrive the project_version: ' . mysql_error($source_connection));
}
$project_versions = array();
while ($row = mysql_fetch_assoc($project_version_result)) {
   $project_versions[] = $row;
}
// get project hierarchy
$project_hierarchy_result = mysql_query("SELECT * FROM `{$source_tables['project_hierarchy']}` WHERE child_id IN (" . implode(',', array_keys($projects)) . ") OR parent_id IN (" . implode(',', array_keys($projects)) . ")", $source_connection);
if ( ! $project_hierarchy_result) {
   die('Unable to retrive the project_hierarchy: ' . mysql_error($source_connection));
}
$project_hierarchies = array();
while ($row = mysql_fetch_assoc($project_hierarchy_result)) {
   $project_hierarchies[] = $row;
}
// add the new projects
$project_old_new = array();
foreach ($projects as $project_id => $project) {
   $project_insert = "INSERT INTO `{$destination_tables['project']}` (";
   $i = 0;
   foreach ($project as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $project_insert .= ', ';
       }
       $project_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $project_insert .= ") VALUES (";
   $i = 0;
   foreach ($project as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $project_insert .= ', ';
       }
       $project_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $project_insert .= ")";
   $insert_result = mysql_query($project_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the project: ' . mysql_error($destination_connection));
   }
   $new_project_id = mysql_insert_id($destination_connection);
   $project_old_new[$project_id] = $new_project_id;
}
// add the new project users
foreach ($project_user_lists as $project_user_list) {
   $project_user_insert = "INSERT INTO `{$destination_tables['project_user_list']}` VALUES (";
   $i = 0;
   foreach ($project_user_list as $field => $value) {
       switch ($field) {
           case 'project_id' :
               $value = $project_old_new[$value];
               break;
           case 'user_id' :
               $value = $user_old_new[$value];
               break;
       }
       if ($i > 0) {
           $project_user_insert .= ', ';
       }
       $project_user_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $project_user_insert .= ")";
   $insert_result = mysql_query($project_user_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the project_user_list: ' . mysql_error($destination_connection));
   }
}
// add the new project versions
foreach ($project_versions as $project_version) {
   $project_version_insert = "INSERT INTO `{$destination_tables['project_version']}` (";
   $i = 0;
   foreach ($project_version as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $project_version_insert .= ', ';
       }
       $project_version_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $project_version_insert .= ") VALUES (";
   $i = 0;
   foreach ($project_version as $field => $value) {
       switch ($field) {
           case 'project_id' :
               $value = $project_old_new[$value];
               break;
           case 'id' :
               continue 2;
       }
       if ($i > 0) {
           $project_version_insert .= ', ';
       }
       $project_version_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $project_version_insert .= ")";
   $insert_result = mysql_query($project_version_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the project_version: ' . mysql_error($destination_connection));
   }
}
// add the new project hierarchy
foreach ($project_hierarchies as $project_hierarchy) {
   $project_hierarchy_insert = "INSERT INTO `{$destination_tables['project_hierarchy']}` VALUES (";
   $i = 0;
   foreach ($project_hierarchy as $field => $value) {
       switch ($field) {
           case 'child_id' :
           case 'parent_id' :
               $value = $project_old_new[$value];
               break;
       }
       if ($i > 0) {
           $project_hierarchy_insert .= ', ';
       }
       $project_hierarchy_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $project_hierarchy_insert .= ")";
   $insert_result = mysql_query($project_hierarchy_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the project_hierarchy: ' . mysql_error($destination_connection));
   }
}
// ******************** Custom Fields ************************
// get custom fields
$custom_field_result = mysql_query("SELECT * FROM `{$source_tables['custom_field']}`", $source_connection);
if ( ! $custom_field_result) {
   die('Unable to retrive the custom_field: ' . mysql_error($source_connection));
}
$custom_fields = array();
while ($row = mysql_fetch_assoc($custom_field_result)) {
   $custom_fields[$row['id']] = $row;
}
// get custom field projects
$custom_field_project_result = mysql_query("SELECT * FROM `{$source_tables['custom_field_project']}` WHERE project_id IN (" . implode(',', array_keys($projects)) . ") AND field_id IN (" . implode(',', array_keys($custom_fields)) . ")", $source_connection);
if ( ! $custom_field_project_result) {
   die('Unable to retrive the custom_field_project: ' . mysql_error($source_connection));
}
$custom_field_projects = array();
while ($row = mysql_fetch_assoc($custom_field_project_result)) {
   $custom_field_projects[] = $row;
}
// add the new custom fields
$custom_field_old_new = array();
foreach ($custom_fields as $custom_field_id => $custom_field) {
   $custom_field_insert = "INSERT INTO `{$destination_tables['custom_field']}` (";
   $i = 0;
   foreach ($custom_field as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $custom_field_insert .= ', ';
       }
       $custom_field_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $custom_field_insert .= ") VALUES (";
   $i = 0;
   foreach ($custom_field as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $custom_field_insert .= ', ';
       }
       $custom_field_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $custom_field_insert .= ")";
   $insert_result = mysql_query($custom_field_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the custom_field: ' . mysql_error($destination_connection));
   }
   $new_custom_field_id = mysql_insert_id($destination_connection);
   $custom_field_old_new[$custom_field_id] = $new_custom_field_id;
}
// add the new custom field projects
foreach ($custom_field_projects as $custom_field_project) {
   $custom_field_project_insert = "INSERT INTO `{$destination_tables['custom_field_project']}` VALUES (";
   $i = 0;
   foreach ($custom_field_project as $field => $value) {
       switch ($field) {
           case 'field_id' :
               $value = $custom_field_old_new[$value];
               break;
           case 'project_id' :
               $value = $project_old_new[$value];
               break;
       }
       if ($i > 0) {
           $custom_field_project_insert .= ', ';
       }
       $custom_field_project_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $custom_field_project_insert .= ")";
   $insert_result = mysql_query($custom_field_project_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the custom_field_project: ' . mysql_error($destination_connection));
   }
}
// ******************** Category ************************
// get categories
$category_result = mysql_query("SELECT * FROM `{$source_tables['category']}` WHERE project_id IN (" . implode(',', array_keys($projects)) . ")", $source_connection);
if ( ! $category_result) {
   die('Unable to retrive the category: ' . mysql_error($source_connection));
}
$categories = array();
while ($row = mysql_fetch_assoc($category_result)) {
   $categories[$row['id']] = $row;
}
// add the new categories
$category_old_new = array();
foreach ($categories as $category_id => $category) {
   $category_insert = "INSERT INTO `{$destination_tables['category']}` (";
   $i = 0;
   foreach ($category as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $category_insert .= ', ';
       }
       $category_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $category_insert .= ") VALUES (";
   $i = 0;
   foreach ($category as $field => $value) {
       switch ($field) {
           case 'id' :
               continue 2;
           case 'project_id' :
               $value = $project_old_new[$value];
               break;
           case 'user_id' :
               if ( ! empty($value)) {
                   $value = $user_old_new[$value];
               }
               break;
       }
       if ($i > 0) {
           $category_insert .= ', ';
       }
       $category_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $category_insert .= ")";
   $insert_result = mysql_query($category_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the category: ' . mysql_error($destination_connection));
   }
   $new_category_id = mysql_insert_id($destination_connection);
   $category_old_new[$category_id] = $new_category_id;
}
// ******************** Bugs ************************
// get bugs
$bug_result = mysql_query("SELECT * FROM `{$source_tables['bug']}` WHERE project_id IN (" . implode(',', array_keys($projects)) . ")", $source_connection);
if ( ! $bug_result) {
   die('Unable to retrive the bug: ' . mysql_error($source_connection));
}
$bugs = array();
while ($row = mysql_fetch_assoc($bug_result)) {
   $bugs[$row['id']] = $row;
}
// get bug texts
$bug_text_result = mysql_query("SELECT * FROM `{$source_tables['bug_text']}`", $source_connection);
if ( ! $bug_text_result) {
   die('Unable to retrive the bug_text: ' . mysql_error($source_connection));
}
$bug_texts = array();
while ($row = mysql_fetch_assoc($bug_text_result)) {
   $bug_texts[$row['id']] = $row;
}
// get custom field strings
// these are the values of the custom fields for each bug
$custom_field_string_result = mysql_query("SELECT * FROM `{$source_tables['custom_field_string']}` WHERE field_id IN (" . implode(',', array_keys($custom_fields)) . ") AND bug_id IN (" . implode(',', array_keys($bugs)) . ")", $source_connection);
if ( ! $custom_field_string_result) {
   die('Unable to retrive the custom_field_string: ' . mysql_error($source_connection));
}
$custom_field_strings = array();
while ($row = mysql_fetch_assoc($custom_field_string_result)) {
   $custom_field_strings[] = $row;
}
// get bug history
$bug_history_result = mysql_query("SELECT * FROM `{$source_tables['bug_history']}` WHERE bug_id IN (" . implode(',', array_keys($bugs)) . ")", $source_connection);
if ( ! $bug_history_result) {
   die('Unable to retrive the bug_history: ' . mysql_error($source_connection));
}
$bug_histories = array();
while ($row = mysql_fetch_assoc($bug_history_result)) {
   $bug_histories[] = $row;
}
// get bug relationship
$bug_relationship_result = mysql_query("SELECT * FROM `{$source_tables['bug_relationship']}` WHERE source_bug_id IN (" . implode(',', array_keys($bugs)) . ") OR destination_bug_id IN (" . implode(',', array_keys($bugs)) . ")", $source_connection);
if ( ! $bug_relationship_result) {
   die('Unable to retrive the bug_relationship: ' . mysql_error($source_connection));
}
$bug_relationships = array();
while ($row = mysql_fetch_assoc($bug_relationship_result)) {
   $bug_relationships[] = $row;
}
// get bug file
$bug_file_result = mysql_query("SELECT * FROM `{$source_tables['bug_file']}` WHERE bug_id IN (" . implode(',', array_keys($bugs)) . ")", $source_connection);
if ( ! $bug_file_result) {
   die('Unable to retrive the bug_file: ' . mysql_error($source_connection));
}
$bug_files = array();
while ($row = mysql_fetch_assoc($bug_file_result)) {
   $bug_files[] = $row;
}
// add the new bug texts
// insert this before bugs because it's id is used in bugs
$bug_text_old_new = array();
foreach ($bug_texts as $bug_text_id => $bug_text) {
   $bug_text_insert = "INSERT INTO `{$destination_tables['bug_text']}` (";
   $i = 0;
   foreach ($bug_text as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_text_insert .= ', ';
       }
       $bug_text_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $bug_text_insert .= ") VALUES (";
   $i = 0;
   foreach ($bug_text as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_text_insert .= ', ';
       }
       $bug_text_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $bug_text_insert .= ")";
   $insert_result = mysql_query($bug_text_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the bug_text: ' . mysql_error($destination_connection));
   }
   $new_bug_text_id = mysql_insert_id($destination_connection);
   $bug_text_old_new[$bug_text_id] = $new_bug_text_id;
}
// add the new bugs
// we'll keep the ids on these for future reference
foreach ($bugs as $bug_id => $bug) {
   $bug_insert = "INSERT INTO `{$destination_tables['bug']}` VALUES (";
   $i = 0;
   foreach ($bug as $field => $value) {
       switch ($field) {
           case 'project_id' :
               $value = $project_old_new[$value];
               break;
           case 'reporter_id' :
           case 'handler_id' :
               if ( ! empty($value)) {
                   if ( ! isset($user_old_new[$value])) {
                       $value = $default_user_id;
                   } else {
                       $value = $user_old_new[$value];
                   }
               }
               break;
           case 'bug_text_id' :
               $value = $bug_text_old_new[$value];
               break;
           case 'category_id' :
               if ( ! empty($value)) {
                   $value = $category_old_new[$value];
               }
               break;
       }
       if ($i > 0) {
           $bug_insert .= ', ';
       }
       $bug_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $bug_insert .= ")";
   $insert_result = mysql_query($bug_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the bug: ' . mysql_error($destination_connection));
   }
}
// add the new custom field strings
foreach ($custom_field_strings as $custom_field_string) {
   $custom_field_string_insert = "INSERT INTO `{$destination_tables['custom_field_string']}` VALUES (";
   $i = 0;
   foreach ($custom_field_string as $field => $value) {
       switch ($field) {
           case 'field_id' :
               $value = $custom_field_old_new[$value];
               break;
           case 'text' :
               if ( ! $include_custom_field_string_text_field) {
                   continue 2;
               }
       }
       if ($i > 0) {
           $custom_field_string_insert .= ', ';
       }
       $custom_field_string_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $custom_field_string_insert .= ")";
   $insert_result = mysql_query($custom_field_string_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the custom_field_string: ' . mysql_error($destination_connection));
   }
}
// add the new bug histories
foreach ($bug_histories as $bug_history) {
   $bug_history_insert = "INSERT INTO `{$destination_tables['bug_history']}` (";
   $i = 0;
   foreach ($bug_history as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_history_insert .= ', ';
       }
       $bug_history_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $bug_history_insert .= ") VALUES (";
   $i = 0;
   $old_value = $new_value = NULL;
   foreach ($bug_history as $field => $value) {
       switch ($field) {
           case 'id' :
               continue 2;
           case 'user_id' :
               if ( ! empty($value)) {
                   if ( ! isset($user_old_new[$value])) {
                       $value = $default_user_id;
                   } else {
                       $value = $user_old_new[$value];
                   }
               }
               break;
           case 'field_name' :
               if ($value == 'handler_id') {
                   if ( ! empty($bug_history['old_value'])) {
                       if ( ! isset($user_old_new[$bug_history['old_value']])) {
                           $old_value = $default_user_id;
                       } else {
                           $old_value = $user_old_new[$bug_history['old_value']];
                       }
                   }
                   if ( ! empty($bug_history['new_value'])) {
                       if ( ! isset($user_old_new[$bug_history['new_value']])) {
                           $new_value = $default_user_id;
                       } else {
                           $new_value = $user_old_new[$bug_history['new_value']];
                       }
                   }
               } else if ($value == 'reporter_id') {
                   if ( ! empty($bug_history['old_value'])) {
                       if ( ! isset($user_old_new[$bug_history['old_value']])) {
                           $old_value = $default_user_id;
                       } else {
                           $old_value = $user_old_new[$bug_history['old_value']];
                       }
                   }
                   if ( ! empty($bug_history['new_value'])) {
                       if ( ! isset($user_old_new[$bug_history['new_value']])) {
                           $new_value = $default_user_id;
                       } else {
                           $new_value = $user_old_new[$bug_history['new_value']];
                       }
                   }
               }
               break;
           case 'old_value' :
               if ($old_value !== NULL) {
                   $value = $old_value;
               }
               break;
           case 'new_value' :
               if ($new_value !== NULL) {
                   $value = $new_value;
               }
               break;
       }
       if ($i > 0) {
           $bug_history_insert .= ', ';
       }
       $bug_history_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $bug_history_insert .= ")";
   $insert_result = mysql_query($bug_history_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the bug_history: ' . mysql_error($destination_connection));
   }
}
// add the new bug relationships
foreach ($bug_relationships as $bug_relationship) {
   $bug_relationship_insert = "INSERT INTO `{$destination_tables['bug_relationship']}` (";
   $i = 0;
   foreach ($bug_relationship as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_relationship_insert .= ', ';
       }
       $bug_relationship_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $bug_relationship_insert .= ") VALUES (";
   $i = 0;
   foreach ($bug_relationship as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_relationship_insert .= ', ';
       }
       $bug_relationship_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $bug_relationship_insert .= ")";
   $insert_result = mysql_query($bug_relationship_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the bug_relationship: ' . mysql_error($destination_connection));
   }
}
// add the new bug files
foreach ($bug_files as $bug_file) {
   $bug_file_insert = "INSERT INTO `{$destination_tables['bug_file']}` (";
   $i = 0;
   foreach ($bug_file as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_file_insert .= ', ';
       }
       $bug_file_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $bug_file_insert .= ") VALUES (";
   $i = 0;
   foreach ($bug_file as $field => $value) {
       switch ($field) {
           case 'id' :
               continue 2;
           case 'user_id' :
               if ( ! empty($value)) {
                   if ( ! isset($user_old_new[$value])) {
                       $value = $default_user_id;
                   } else {
                       $value = $user_old_new[$value];
                   }
               }
               break;
       }
       if ($i > 0) {
           $bug_file_insert .= ', ';
       }
       $bug_file_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $bug_file_insert .= ")";
   $insert_result = mysql_query($bug_file_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the bug_file: ' . mysql_error($destination_connection));
   }
}
// ******************** Bug Notes ************************
// get bug notes
$bug_note_result = mysql_query("SELECT * FROM `{$source_tables['bugnote']}` WHERE bug_id IN (" . implode(',', array_keys($bugs)) . ")", $source_connection);
if ( ! $bug_note_result) {
   die('Unable to retrive the bug_note: ' . mysql_error($source_connection));
}
$bug_notes = array();
while ($row = mysql_fetch_assoc($bug_note_result)) {
   $bug_notes[$row['id']] = $row;
}
// get bug note texts
$bug_note_text_result = mysql_query("SELECT * FROM `{$source_tables['bugnote_text']}`", $source_connection);
if ( ! $bug_note_text_result) {
   die('Unable to retrive the bug_note_text: ' . mysql_error($source_connection));
}
$bug_note_texts = array();
while ($row = mysql_fetch_assoc($bug_note_text_result)) {
   $bug_note_texts[$row['id']] = $row;
}
// add the new bug note texts
// insert this before bug notes because it's id is used in bug notes
$bug_note_text_old_new = array();
foreach ($bug_note_texts as $bug_note_text_id => $bug_note_text) {
   $bug_note_text_insert = "INSERT INTO `{$destination_tables['bugnote_text']}` (";
   $i = 0;
   foreach ($bug_note_text as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_note_text_insert .= ', ';
       }
       $bug_note_text_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $bug_note_text_insert .= ") VALUES (";
   $i = 0;
   foreach ($bug_note_text as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_note_text_insert .= ', ';
       }
       $bug_note_text_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $bug_note_text_insert .= ")";
   $insert_result = mysql_query($bug_note_text_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the bug_note_text: ' . mysql_error($destination_connection));
   }
   $new_bug_note_text_id = mysql_insert_id($destination_connection);
   $bug_note_text_old_new[$bug_note_text_id] = $new_bug_note_text_id;
}
// add the new bugs
// we'll keep the ids on these for future reference
foreach ($bug_notes as $bug_note_id => $bug_note) {
   $bug_note_insert = "INSERT INTO `{$destination_tables['bugnote']}` VALUES (";
   $i = 0;
   foreach ($bug_note as $field => $value) {
       switch ($field) {
           case 'reporter_id' :
               if ( ! empty($value)) {
                   if ( ! isset($user_old_new[$value])) {
                       $value = $default_user_id;
                   } else {
                       $value = $user_old_new[$value];
                   }
               }
               break;
           case 'bugnote_text_id' :
               $value = $bug_note_text_old_new[$value];
               break;
       }
       if ($i > 0) {
           $bug_note_insert .= ', ';
       }
       $bug_note_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $bug_note_insert .= ")";
   $insert_result = mysql_query($bug_note_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the bug_note: ' . mysql_error($destination_connection));
   }
}
// ******************** Bug Revisions ************************
// get bug revisions
$bug_revision_result = mysql_query("SELECT * FROM `{$source_tables['bug_revision']}` WHERE bug_id IN (" . implode(',', array_keys($bugs)) . ")", $source_connection);
if ( ! $bug_revision_result) {
   die('Unable to retrive the bug_revision: ' . mysql_error($source_connection));
}
$bug_revisions = array();
while ($row = mysql_fetch_assoc($bug_revision_result)) {
   $bug_revisions[] = $row;
}
// add the new bug revisions
foreach ($bug_revisions as $bug_revision) {
   $bug_revision_insert = "INSERT INTO `{$destination_tables['bug_revision']}` (";
   $i = 0;
   foreach ($bug_revision as $field => $value) {
       if ($field == 'id') continue;
       if ($i > 0) {
           $bug_revision_insert .= ', ';
       }
       $bug_revision_insert .= "`" . mysql_real_escape_string($field) . "`";
       ++ $i;
   }
   $bug_revision_insert .= ") VALUES (";
   $i = 0;
   foreach ($bug_revision as $field => $value) {
       switch ($field) {
           case 'id' :
               continue 2;
           case 'user_id' :
               if ( ! empty($value)) {
                   if ( ! isset($user_old_new[$value])) {
                       $value = $default_user_id;
                   } else {
                       $value = $user_old_new[$value];
                   }
               }
               break;
       }
       if ($i > 0) {
           $bug_revision_insert .= ', ';
       }
       $bug_revision_insert .= "'" . mysql_real_escape_string($value) . "'";
       ++ $i;
   }
   $bug_revision_insert .= ")";
   $insert_result = mysql_query($bug_revision_insert, $destination_connection);
   if ( ! $insert_result) {
       die('Unable to insert the bug_revision: ' . mysql_error($destination_connection));
   }
}
echo 'done' . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment