compare.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. require_once "tables.php";
  3. require_once "config.php";
  4. require_once "function.php";
  5. if(php_sapi_name() !== "cli") {
  6. echo 'no cli';
  7. return;
  8. }
  9. if(count($argv)<3){
  10. echo 'expect 2 db '.(count($argv)-1).' gave';
  11. return;
  12. }
  13. $src_db = $argv[1];
  14. $dest_db = $argv[2];
  15. #打开源数据库
  16. $PDO_SRC = openDb($config[$src_db]);
  17. $PDO_SRC->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  18. #打开目标数据库
  19. $PDO_DEST = openDb($config[$dest_db]);
  20. $PDO_DEST->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  21. foreach ($tables as $tableName => $table) {
  22. //A -> B 差异
  23. fwrite(STDOUT,$tableName.PHP_EOL);
  24. if($table['user'] === false){
  25. fwrite(STDOUT,'not user data ignore'.PHP_EOL);
  26. continue;
  27. }
  28. $keys = array();
  29. if(is_array($table['key'])){
  30. $keys = $table['key'];
  31. }else{
  32. $keys[] = $table['key'];
  33. }
  34. $select = $keys;
  35. if(!empty($table['time1'])){
  36. $select[] = $table['time1'];
  37. }
  38. if(!empty($table['time2'])){
  39. $select[] = $table['time2'];
  40. }
  41. $query = "SELECT * FROM {$tableName} ";
  42. $stmtSrc = $PDO_SRC->prepare($query);
  43. $stmtSrc->execute();
  44. $count = 0;
  45. $where = [];
  46. foreach ($keys as $value) {
  47. $where[] = "{$value} = ? ";
  48. }
  49. $query = "SELECT ".implode(',',$select)." FROM {$tableName} where ". implode(' and ',$where);
  50. $stmtDest = $PDO_DEST->prepare($query);
  51. $countUpdate = 0;
  52. $countDown = 0;
  53. $countNew = 0;
  54. while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
  55. $count++;
  56. if($count % 1000 === 0){
  57. fwrite(STDOUT,$count.PHP_EOL);
  58. }
  59. $param = [];
  60. foreach ($keys as $value) {
  61. $param[] = $srcData[$value];
  62. }
  63. $stmtDest->execute($param);
  64. $row = $stmtDest->fetch(PDO::FETCH_ASSOC);
  65. $realKey = implode(' and ',$where).' = '.implode(',',$param);
  66. if($row) {
  67. //时间
  68. $t1a=0;
  69. $t1b=0;
  70. $t2a=0;
  71. $t2b=0;
  72. $ta=0;
  73. $tb=0;
  74. if(!empty($table['time1'])){
  75. $t1a=$srcData[$table['time1']]/1000;
  76. $t1b=$row[$table['time1']]/1000;
  77. }
  78. if(!empty($table['time2'])){
  79. $t2a=strtotime($srcData[$table['time2']]);
  80. $t2b=strtotime($row[$table['time2']]);
  81. }
  82. //两个时间戳,取较新的。
  83. $ta = $t1a > $t2a? $t1a:$t2a;
  84. $tb = $t1b > $t2b? $t1b:$t2b;
  85. if($ta > $tb ){
  86. fwrite(STDOUT,$tableName.' update '.$realKey.PHP_EOL);
  87. fwrite(STDOUT,' update '.$row[$table['time2']].' -> '.$srcData[$table['time2']].PHP_EOL);
  88. $countUpdate++;
  89. }else if($ta < $tb){
  90. fwrite(STDOUT,$tableName.' down date '.$realKey.PHP_EOL);
  91. $countDown++;
  92. }
  93. }else{
  94. //缺失
  95. fwrite(STDERR,$tableName.' new '.$realKey.PHP_EOL);
  96. $countNew++;
  97. }
  98. }
  99. fwrite(STDOUT,"table={$tableName}".PHP_EOL);
  100. fwrite(STDOUT,"Update={$countUpdate}".PHP_EOL);
  101. fwrite(STDOUT,"Down={$countDown}".PHP_EOL);
  102. fwrite(STDOUT,"New={$countNew}".PHP_EOL);
  103. fwrite(STDOUT,PHP_EOL);
  104. }