script.clear_krs_msig.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env php
  2. <?php
  3. error_reporting(E_ALL & ~E_NOTICE);
  4. $_SERVER['SERVER_NAME'] = gethostname();
  5. //require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
  6. require("/Library/Server/Web/Data/Sites/Default/SE/se-lib/bootstrap.php");
  7. date_default_timezone_set('Europe/Warsaw');
  8. $mainTables = ['BI_audit_KRS', 'BI_audit_KRS_address', 'BI_audit_KRS_company', 'BI_audit_KRS_name', 'BI_audit_KRS_person',
  9. 'BI_audit_MSIG', 'BI_audit_MSIG_address', 'BI_audit_MSIG_company', 'BI_audit_MSIG_name', 'BI_audit_MSIG_person'];
  10. $query = "show tables";
  11. $result = DB::getPDO()->fetchAll($query);
  12. $tables = array_map("reset", $result);
  13. $allTables = array_filter($tables, function ($table) {
  14. if (preg_match('/(__@|_HIST$|^BI_audit_ALL|^BI_audit_BENFORD|^BI_audit_VALIDATE)/', $table)) return false;
  15. if (preg_match('/^BI_audit/', $table)) return true;
  16. return false;
  17. });
  18. sort($allTables);
  19. $refTables = [];
  20. foreach ($mainTables as $mainTable) {
  21. foreach ($allTables as $allTable) {
  22. try {
  23. $refTables[] = ACL::getRefTable("default_db/{$mainTable}/{$mainTable}", "default_db__x3A__{$allTable}:{$allTable}");
  24. } catch (Exception $e) {
  25. }
  26. try {
  27. $refTables[] = ACL::getRefTable("default_db/{$allTable}/{$allTable}", "default_db__x3A__{$mainTable}:{$mainTable}");
  28. } catch (Exception $e) {
  29. }
  30. }
  31. }
  32. $refTables = array_unique(array_filter($refTables, function ($table) {
  33. if (preg_match('/_VIEW$/', $table)) return false;
  34. return true;
  35. }));
  36. usort($refTables, function ($a, $b) {
  37. list($ai, $bi) = array_map(function($x) {
  38. preg_match('/__([[:digit:]]+)$/', $x, $matches);
  39. return (int)$matches[1];
  40. }, [$a, $b]);
  41. if ($ai == $bi) return 0;
  42. return ($ai > $bi);
  43. });
  44. echo "Główne tabele do wyczyszczenia: (" . count($mainTables) . "):\n" . implode(", ", $mainTables) . "\n\n";
  45. echo "Tabele relacyjne do wyczyszczenia (" . count($refTables) . "):\n" . implode(", ", $refTables) . "\n\n";
  46. $prompt = null;
  47. while (!in_array($prompt, ['t', 'n'])) {
  48. $prompt = strtolower(readline("Jesteś pewien, że chcesz wyczyścić te wszystkie tabele? [T/N]: "));
  49. }
  50. if ($prompt === 'n') die("Przerywam\n");
  51. /*
  52. try {
  53. Lib::loadClass('Token');
  54. $pass = Config::getConfFile('default_db')['pass'];
  55. $tokenObj = new Token($pass);
  56. $token = $tokenObj->genToken();
  57. echo "Token: {$token}\n";
  58. $hash = readline("Hash: ");
  59. if ($pass !== $tokenObj->verify($hash)) die("Błędny token, przerywam\n");
  60. } catch (Exception $e) {
  61. die("Wystąpił nieznany błąd, przerywam\n");
  62. }
  63. */
  64. $prompt = null;
  65. while (!in_array($prompt, ['t', 'n'])) {
  66. $prompt = strtolower(readline("Na pewno jesteś pewien, że chcesz wyczyścić te wszystkie tabele? [T/N]: "));
  67. }
  68. if ($prompt === 'n') die("Przerywam\n");
  69. $queries = [];
  70. foreach ($mainTables as $table) $queries[] = [
  71. "desc" => "Czyszczę tabelę główną `{$table}`",
  72. "sql" => "delete from `{$table}`",
  73. ];
  74. foreach ($refTables as $table) $queries[] = [
  75. "desc" => "Czyszczę tabelę relacyjną `{$table}`",
  76. "sql" => "truncate table `{$table}`",
  77. ];
  78. $queries[] = [
  79. "desc" => "Usuwam relacje z `BI_audit_ALL_ref` (relacje OD)",
  80. "sql" => "delete from `ref` using `BI_audit_ALL` `all` join `BI_audit_ALL_ref` `ref` on `all`.`ID` = `ref`.`ID1` where `all`.`REMOTE_TABLE` in ('" . implode("', '", $mainTables) . "')",
  81. ];
  82. $queries[] = [
  83. "desc" => "Usuwam relacje z `BI_audit_ALL_ref` (relacje DO)",
  84. "sql" => "delete from `ref` using `BI_audit_ALL` `all` join `BI_audit_ALL_ref` `ref` on `all`.`ID` = `ref`.`ID2` where `all`.`REMOTE_TABLE` in ('" . implode("', '", $mainTables) . "')",
  85. ];
  86. $queries[] = [
  87. "desc" => "Usuwam dane z `BI_audit_ALL`",
  88. "sql" => "delete from `BI_audit_ALL` where `REMOTE_TABLE` in ('" . implode("', '", $mainTables) . "')",
  89. ];
  90. $queries[] = [
  91. "desc" => "Optymalizuję tabelę `BI_audit_ALL`",
  92. "sql" => "optimize table `BI_audit_ALL`",
  93. ];
  94. $queries[] = [
  95. "desc" => "Optymalizuję tabelę `BI_audit_ALL_ref`",
  96. "sql" => "optimize table `BI_audit_ALL_ref`",
  97. ];
  98. foreach ($queries as $query) {
  99. echo $query['desc'];
  100. // echo " ({$query['sql']})";
  101. try {
  102. DB::getPDO()->query($query['sql']);
  103. echo " - OK\n";
  104. } catch (Exception $e) {
  105. echo " - ERROR\n";
  106. }
  107. }