Skip to content

Instantly share code, notes, and snippets.

@acmh
Created April 13, 2016 20:45
Show Gist options
  • Save acmh/ceba99b836801eb370e7ff7344b9b9a4 to your computer and use it in GitHub Desktop.
Save acmh/ceba99b836801eb370e7ff7344b9b9a4 to your computer and use it in GitHub Desktop.
Compare two databases in mysql
/*
* Author: Artur Montenegro
* Codigo para comparar o banco de testes e o banco de producao
*/
<?php
$dbhost = '';
$dbuser = '';
$dbpass = '';
$query = null;
$teste = '';
$producao = '';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if(! $conn ){
die('Could not connect: ' . mysql_error());
}
mysql_select_db($teste);
$query = "show tables";
$retval = mysql_query($query, $conn );
$tablesA = array();
while($table = mysql_fetch_row($retval)){
$tablesA[] = $table[0];
}
mysql_select_db($producao);
$query = "show tables";
$retval = mysql_query($query, $conn );
$tablesB = array();
while($table = mysql_fetch_row($retval)){
$tablesB[] = $table[0];
}
sort($tablesA, SORT_STRING);
sort($tablesB, SORT_STRING);
for ($i = 0; $i < count($tablesA); $i++){
echo "$tableA[$i]" . "\n";
mysql_select_db($teste);
$query = "DESCRIBE " . $tablesA[$i];
$retval = mysql_query($query, $conn );
$resA = array();
while($desc = mysql_fetch_row($retval)){
$resA[] = $desc[0];
}
mysql_select_db($producao);
$query = "DESCRIBE " . $tablesA[$i];
$retval = mysql_query($query, $conn );
$resB = array();
while($desc = mysql_fetch_row($retval)){
$resB[] = $desc[0];
}
if(count($resA) != count($resB)){
echo("A tabela " . $tablesA[$i] . " tem quantidade de atributos diferentes entre o banco de testes e de produção" . "\n\n");
echo("Os elementos são: \n");
$diffResult = array_diff($resA, $resB);
print_r($diffResult);
echo "------------------------------\n\n";
die("QTD em teste " . count($resA) . " xxxxxxxx " . "QTD em producao " . count($resB));
}
sort($resA, SORT_STRING);
sort($resB, SORT_STRING);
for($j = 0; $j < count($resA); $j++){
echo ($resA[$j] . ">>>>>><<<<<<" . $resB[$j] . "\n");
if(strcmp($resA[$j],$resB[$j]) != 0){
echo("BD TESTE " . $resA[$j] . " ::::::: " . "BD PRODUCAO " . $resB[$j] . "\n\n");
die("A tabela " . $tablesA[$i] . " tem atributo diferente entre o banco de testes e o de produção" . "\n\n");
}
}
}
echo "\n\n\n\nNenhuma diferença foi encontrada";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment