script.truncate_all_data.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. $query = "show tables";
  9. $result = DB::getPDO()->fetchAll($query);
  10. $tables = array_map("reset", $result);
  11. $mainTables = array_filter($tables, function ($table) {
  12. if (preg_match('/^BI_audit/', $table)) return true;
  13. return false;
  14. });
  15. sort($mainTables);
  16. $refTables = [];
  17. foreach ($mainTables as $tableFrom) {
  18. foreach ($mainTables as $tableTo) {
  19. try {
  20. $refTables[] = ACL::getRefTable("default_db/{$tableFrom}/{$tableFrom}", "default_db__x3A__{$tableTo}:{$tableTo}");
  21. } catch (Exception $e) {
  22. }
  23. }
  24. }
  25. $refTables = array_filter($refTables, function ($table) {
  26. if (preg_match('/_VIEW$/', $table)) return false;
  27. return true;
  28. });
  29. sort($refTables);
  30. echo "Główne tabele do wyczyszczenia: (" . count($mainTables) . "):\n" . implode(", ", $mainTables) . "\n\n";
  31. echo "Tabele relacyjne do wyczyszczenia (" . count($refTables) . "):\n" . implode(", ", $refTables) . "\n\n";
  32. $prompt = null;
  33. while (!in_array($prompt, ['t', 'n'])) {
  34. $prompt = strtolower(readline("Jesteś pewien, że chcesz wyczyścić te wszystkie tabele? [T/N]: "));
  35. }
  36. if ($prompt === 'n') die("Przerywam\n");
  37. try {
  38. Lib::loadClass('Token');
  39. $pass = Config::getConfFile('default_db')['pass'];
  40. $tokenObj = new Token($pass);
  41. $token = $tokenObj->genToken();
  42. echo "Token: {$token}\n";
  43. $hash = readline("Hash: ");
  44. if ($pass !== $tokenObj->verify($hash)) die("Błędny token, przerywam\n");
  45. } catch (Exception $e) {
  46. die("Wystąpił nieznany błąd, przerywam\n");
  47. }
  48. $prompt = null;
  49. while (!in_array($prompt, ['t', 'n'])) {
  50. $prompt = strtolower(readline("Na pewno jesteś pewien, że chcesz wyczyścić te wszystkie tabele? [T/N]: "));
  51. }
  52. if ($prompt === 'n') die("Przerywam\n");
  53. echo "\nCzyszczę tabele:\n";
  54. foreach (array_merge($mainTables, $refTables) as $table) {
  55. echo $table;
  56. try {
  57. DB::getPDO()->query("truncate table `{$table}`");
  58. echo " - OK\n";
  59. } catch (Exception $e) {
  60. echo " - ERROR\n";
  61. }
  62. }