|
| 1 | +<?php |
| 2 | + |
| 3 | +// credentials file |
| 4 | +require 'config.php'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @param array $config |
| 8 | + * |
| 9 | + * @return PDO |
| 10 | + */ |
| 11 | +function getPDO(array $config) |
| 12 | +{ |
| 13 | + $pdo = new PDO( |
| 14 | + sprintf( |
| 15 | + '%s:host=%s;port=%d;dbname=%s', |
| 16 | + $config['driver'], |
| 17 | + $config['hostname'], |
| 18 | + $config['port'], |
| 19 | + $config['database'] |
| 20 | + ), |
| 21 | + $config['user'], |
| 22 | + $config['password'] |
| 23 | + ); |
| 24 | + |
| 25 | + return $pdo; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Validate console input |
| 30 | + * |
| 31 | + * Die if input is not valid |
| 32 | + * |
| 33 | + * @param array $argv user inputs |
| 34 | + */ |
| 35 | +function validateInput($argv) |
| 36 | +{ |
| 37 | + $isValid = false; |
| 38 | + |
| 39 | + if (count($argv) === 1) { |
| 40 | + $isValid = true; |
| 41 | + } |
| 42 | + |
| 43 | + if (!$isValid) { |
| 44 | + echo 'php compare_sql_tables.php' . PHP_EOL; |
| 45 | + die(1); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +validateInput($argv); |
| 50 | + |
| 51 | +require 'Util/TextColorWriter.php'; |
| 52 | + |
| 53 | +$pdo1 = getPDO($config); |
| 54 | +$pdo2 = getPDO($config); |
| 55 | + |
| 56 | +// implement these functions in config.php file |
| 57 | +$orderedColumns = getOrderedColumns(); |
| 58 | +$orderBy = getOrderBy(); |
| 59 | + |
| 60 | +$query1 = "SELECT $orderedColumns from $table1 ORDER BY $orderBy"; |
| 61 | +$query2 = "SELECT $orderedColumns from $table2 ORDER BY $orderBy"; |
| 62 | + |
| 63 | +$pdo1->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); |
| 64 | +$stmt1 = $pdo1->query($query1); |
| 65 | + |
| 66 | +if (!$stmt1) { |
| 67 | + throw new \RuntimeException(join(' : ', $pdo1->errorInfo())); |
| 68 | +} |
| 69 | + |
| 70 | +$pdo2->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); |
| 71 | +$stmt2 = $pdo2->query($query2); |
| 72 | + |
| 73 | +if (!$stmt1) { |
| 74 | + throw new \RuntimeException(join(' : ', $pdo2->errorInfo())); |
| 75 | +} |
| 76 | + |
| 77 | +$errors = array(); |
| 78 | +$i = 0; |
| 79 | +while ($line = $stmt1->fetch(\PDO::FETCH_NUM)) { |
| 80 | + if (!$stmt1) { |
| 81 | + throw new RuntimeException(join(' : ', $pdo1->errorInfo())); |
| 82 | + } |
| 83 | + |
| 84 | + $otherLine = $stmt2->fetch(\PDO::FETCH_NUM); |
| 85 | + if (!$stmt2) { |
| 86 | + throw new RuntimeException(join(' : ', $pdo2->errorInfo())); |
| 87 | + } |
| 88 | + |
| 89 | + $string1 = implode(';', $line); |
| 90 | + $string2 = implode(';', $otherLine); |
| 91 | + |
| 92 | + if ($string1 != $string2) { |
| 93 | + $errors[] = $string1 . ' / ' . $string2; |
| 94 | + } |
| 95 | + |
| 96 | + $i++; |
| 97 | + echo "\033[G$i lines analysed"; |
| 98 | +} |
| 99 | +echo PHP_EOL; |
| 100 | + |
| 101 | +$stmt1->closeCursor(); |
| 102 | +$stmt2->closeCursor(); |
| 103 | + |
| 104 | + |
| 105 | +// echo result |
| 106 | +echo TextColorWriter::textColor('SQL TABLE COMPARISON:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL; |
| 107 | + |
| 108 | +if (empty($errors)) { |
| 109 | + echo TextColorWriter::textColor('Table content is identical', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL; |
| 110 | +} else { |
| 111 | + echo TextColorWriter::textColor(count($errors) . 'different lines :', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL; |
| 112 | + foreach($errors as $e) { |
| 113 | + echo "$e" . PHP_EOL; |
| 114 | + } |
| 115 | +} |
0 commit comments