Example #1
0
/**
 * Checks if the referenced table have foreign keys.
 * uses    PMA_createTargetTables()
 *
 * @param string $src_db                  name of source database
 * @param mixed  $src_link                connection established with source server
 * @param string $trg_db                  name of target database
 * @param mixed  $trg_link                connection established with target server
 * @param string $referenced_table        table whose column is a foreign key in another table
 * @param array  &$uncommon_tables        names that are uncommon
 * @param array  &$uncommon_tables_fields field names of the uncommon table
 * @param bool   $display
 */
function PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $referenced_table, &$uncommon_tables, &$uncommon_tables_fields, $display)
{
    $is_fk_query = "SELECT * FROM  information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $src_db . "'\n                    AND TABLE_NAME = '" . $referenced_table . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";
    $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
    if (sizeof($is_fk_result) > 0) {
        for ($j = 0; $j < sizeof($is_fk_result); $j++) {
            if (in_array($is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
                $table_index = array_keys($uncommon_tables, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
                PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables, $uncommon_tables_fields, $display);
                PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields, $display);
                unset($uncommon_tables[$table_index[0]]);
            }
        }
    }
}
/**
 * This function provides synchronization of structure and data between two mysql servers. 
 * TODO: improve code sharing between the function and synchronization
 *
 * @param String $db - name of database, which should be synchronized
 * @param mixed $src_link - link of source server, note: if the server is current PMA server, use null
 * @param mixed $trg_link - link of target server, note: if the server is current PMA server, use null
 * @param boolean $data - if true, then data will be copied as well
 */
function PMA_replication_synchronize_db($db, $src_link, $trg_link, $data = true)
{
    $src_db = $trg_db = $db;
    $src_connection = PMA_DBI_select_db($src_db, $src_link);
    $trg_connection = PMA_DBI_select_db($trg_db, $trg_link);
    $src_tables = PMA_DBI_get_tables($src_db, $src_link);
    $source_tables_num = sizeof($src_tables);
    $trg_tables = PMA_DBI_get_tables($trg_db, $trg_link);
    $target_tables_num = sizeof($trg_tables);
    /**
     * initializing arrays to save table names 
     */
    $unmatched_num_src = 0;
    $source_tables_uncommon = array();
    $unmatched_num_trg = 0;
    $target_tables_uncommon = array();
    $matching_tables = array();
    $matching_tables_num = 0;
    /**
     * Criterion for matching tables is just their names.
     * Finding the uncommon tables for the source database
     * BY comparing the matching tables with all the tables in the source database
     */
    PMA_getMatchingTables($trg_tables, $src_tables, $matching_tables, $source_tables_uncommon);
    /**
     * Finding the uncommon tables for the target database
     * BY comparing the matching tables with all the tables in the target database
     */
    PMA_getNonMatchingTargetTables($trg_tables, $matching_tables, $target_tables_uncommon);
    /**
     * 
     * Comparing Data In the Matching Tables 
     * It is assumed that the matching tables are structurally 
     * and typely exactly the same  
     */
    $fields_num = array();
    $matching_tables_fields = array();
    $matching_tables_keys = array();
    $insert_array = array(array(array()));
    $update_array = array(array(array()));
    $delete_array = array();
    $row_count = array();
    $uncommon_tables_fields = array();
    $matching_tables_num = sizeof($matching_tables);
    for ($i = 0; $i < sizeof($matching_tables); $i++) {
        PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $matching_tables_fields, $update_array, $insert_array, $delete_array, $fields_num, $i, $matching_tables_keys);
    }
    for ($j = 0; $j < sizeof($source_tables_uncommon); $j++) {
        PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $j, $row_count);
    }
    /**
     * INTEGRATION OF STRUCTURE DIFFERENCE CODE
     *
     */
    $source_columns = array();
    $target_columns = array();
    $alter_str_array = array(array());
    $add_column_array = array(array());
    $uncommon_columns = array();
    $target_tables_keys = array();
    $source_indexes = array();
    $target_indexes = array();
    $add_indexes_array = array();
    $remove_indexes_array = array();
    $criteria = array('Field', 'Type', 'Null', 'Collation', 'Key', 'Default', 'Comment');
    for ($counter = 0; $counter < $matching_tables_num; $counter++) {
        PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns, $target_columns, $alter_str_array, $add_column_array, $uncommon_columns, $criteria, $target_tables_keys, $counter);
        PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array, $remove_indexes_array, $counter);
    }
    $matching_table_data_diff = array();
    $matching_table_structure_diff = array();
    $uncommon_table_structure_diff = array();
    $uncommon_table_data_diff = array();
    $uncommon_tables = $source_tables_uncommon;
    /**
     * Generating Create Table query for all the non-matching tables present in Source but not in Target and populating tables.  
     */
    for ($q = 0; $q < sizeof($source_tables_uncommon); $q++) {
        if (isset($uncommon_tables[$q])) {
            PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);
        }
        if (isset($row_count[$q]) && $data) {
            PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);
        }
    }
}
        }
        if (isset($matching_table_structure_diff) && (isset($add_indexes_array[$matching_table_structure_diff[$p]]) || isset($remove_indexes_array[$matching_table_structure_diff[$p]]) || isset($alter_indexes_array[$matching_table_structure_diff[$p]]))) {
            PMA_applyIndexesDiff($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array, $remove_indexes_array, $matching_table_structure_diff[$p], true);
            unset($add_indexes_array[$matching_table_structure_diff[$p]]);
            unset($alter_indexes_array[$matching_table_structure_diff[$p]]);
            unset($remove_indexes_array[$matching_table_structure_diff[$p]]);
        }
        PMA_updateTargetTables($matching_tables, $update_array, $src_db, $trg_db, $trg_link, $p, $matching_tables_keys, true);
        PMA_insertIntoTargetTable($matching_tables, $src_db, $trg_db, $src_link, $trg_link, $matching_tables_fields, $insert_array, $p, $matching_tables_keys, $matching_tables_keys, $source_columns, $add_column_array, $criteria, $target_tables_keys, $uncommon_tables, $uncommon_tables_fields, $uncommon_cols, $alter_str_array, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array, $delete_array, $update_array, true);
    }
    /**
     *  Creating and populating tables present in source but absent from target database.  
     */
    for ($q = 0; $q < sizeof($source_tables_uncommon); $q++) {
        if (isset($uncommon_tables[$q])) {
            PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, true);
        }
        if (isset($row_count[$q])) {
            PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, true);
        }
    }
    echo "</div>";
}
/**
 * Displays the main page when none of the following buttons is pressed
 */
if (!isset($_REQUEST['submit_connect']) && !isset($_REQUEST['synchronize_db']) && !isset($_REQUEST['Table_ids'])) {
    /**                      
    * Displays the sub-page heading
    */
    echo '<h2>' . ($GLOBALS['cfg']['MainPageIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 's_sync.png" width="18"' . ' height="18" alt="" />' : '') . $strSynchronize . '</h2>';