2
0

compare.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if(isset($argv[3])){
  22. $on = false;
  23. }else{
  24. $on = true;
  25. }
  26. foreach ($tables as $tableName => $table) {
  27. if(isset($argv[3])){
  28. if($tableName !== $argv[3]){
  29. continue;
  30. //$on = true;
  31. }
  32. }
  33. /*
  34. if(!$on){
  35. continue;
  36. }
  37. */
  38. //A -> B 差异
  39. fwrite(STDOUT,$tableName.PHP_EOL);
  40. if($table['user'] === false){
  41. fwrite(STDOUT,'not user data ignore'.PHP_EOL);
  42. continue;
  43. }
  44. $keys = array();
  45. if(is_array($table['key'])){
  46. $keys = $table['key'];
  47. }else{
  48. $keys[] = $table['key'];
  49. }
  50. $select = $keys;
  51. if(!empty($table['time1'])){
  52. $select[] = $table['time1'];
  53. }
  54. if(!empty($table['time2'])){
  55. $select[] = $table['time2'];
  56. }
  57. $query = "SELECT * FROM {$tableName} ";
  58. $stmtSrc = $PDO_SRC->prepare($query);
  59. $stmtSrc->execute();
  60. $count = 0;
  61. $where = [];
  62. foreach ($keys as $value) {
  63. $where[] = "{$value} = ? ";
  64. }
  65. $query = "SELECT ".implode(',',$select)." FROM {$tableName} where ". implode(' and ',$where);
  66. $stmtDest = $PDO_DEST->prepare($query);
  67. $countUpdate = 0;
  68. $countDown = 0;
  69. $countNew = 0;
  70. while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
  71. $count++;
  72. if($count % 1000 === 0){
  73. fwrite(STDOUT,$count.PHP_EOL);
  74. }
  75. $param = [];
  76. foreach ($keys as $value) {
  77. $param[] = $srcData[$value];
  78. }
  79. $stmtDest->execute($param);
  80. $row = $stmtDest->fetch(PDO::FETCH_ASSOC);
  81. $realKey = implode(' and ',$where).' = '.implode(',',$param);
  82. if($row) {
  83. //时间
  84. $t1a=0;
  85. $t1b=0;
  86. $t2a=0;
  87. $t2b=0;
  88. $ta=0;
  89. $tb=0;
  90. if(!empty($table['time1'])){
  91. $t1a=$srcData[$table['time1']]/1000;
  92. $t1b=$row[$table['time1']]/1000;
  93. }
  94. if(!empty($table['time2'])){
  95. $t2a=strtotime($srcData[$table['time2']]);
  96. $t2b=strtotime($row[$table['time2']]);
  97. }
  98. //两个时间戳,取较新的。
  99. $ta = $t1a > $t2a? $t1a:$t2a;
  100. $tb = $t1b > $t2b? $t1b:$t2b;
  101. if($ta > $tb ){
  102. fwrite(STDOUT,$tableName.' update '.$realKey.PHP_EOL);
  103. fwrite(STDOUT,' update '.$row[$table['time2']].' -> '.$srcData[$table['time2']].PHP_EOL);
  104. $countUpdate++;
  105. }else if($ta < $tb){
  106. fwrite(STDOUT,$tableName.' down date '.$realKey.PHP_EOL);
  107. $countDown++;
  108. }
  109. }else{
  110. //缺失
  111. if(isset($srcData['uid'])){
  112. $title = $srcData['uid'];
  113. }else{
  114. $title = '';
  115. }
  116. fwrite(STDOUT,$tableName." new title={$title} ".$realKey.PHP_EOL);
  117. $countNew++;
  118. }
  119. }
  120. fwrite(STDOUT,"table={$tableName}".PHP_EOL);
  121. fwrite(STDOUT,"Update={$countUpdate}".PHP_EOL);
  122. fwrite(STDOUT,"Down={$countDown}".PHP_EOL);
  123. fwrite(STDOUT,"New={$countNew}".PHP_EOL);
  124. fwrite(STDOUT,PHP_EOL);
  125. }