test.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. fwrite(STDOUT,$tableName.PHP_EOL);
  23. $query = "SELECT count(*) FROM {$tableName}";
  24. $stmtSrc = $PDO_SRC->prepare($query);
  25. $stmtSrc->execute();
  26. $srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC);
  27. fwrite(STDOUT,"table {$src_db} count=".$srcData['count'].PHP_EOL);
  28. $stmtDest = $PDO_DEST->prepare($query);
  29. $stmtDest->execute();
  30. $destData = $stmtDest->fetch(PDO::FETCH_ASSOC);
  31. fwrite(STDOUT,"table {$dest_db} count=".$destData['count'].PHP_EOL);
  32. fwrite(STDOUT,'field='.count($table['fields']).PHP_EOL);
  33. $fields = '"' . implode('","',$table['fields']) . '"' ;
  34. $query = "SELECT {$fields} FROM {$tableName} limit 1 ";
  35. $stmtSrc = $PDO_SRC->prepare($query);
  36. $stmtSrc->execute();
  37. $keys = array();
  38. if(is_array($table['key'])){
  39. $keys = $table['key'];
  40. }else{
  41. $keys[] = $table['key'];
  42. }
  43. if(!empty($table['time1'])){
  44. $keys[] = $table['time1'];
  45. }
  46. if(!empty($table['time1'])){
  47. $keys[] = $table['time2'];
  48. }
  49. foreach ($keys as $key) {
  50. if(!in_array($key,$table['fields'])){
  51. fwrite(STDERR,$tableName.' no field '.$key.PHP_EOL);
  52. }
  53. }
  54. fwrite(STDOUT,PHP_EOL);
  55. }