FixProjectPath.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_FixProjectPath extends RouteBase {
  4. public function defaultAction() {
  5. echo 'TODO: F.' . __FUNCTION__;
  6. //$sqlList['Check'] = "SHOW PROCEDURE STATUS where `Name`='update_project_path_idx_rec'";
  7. }
  8. public function runAction() {
  9. $sql = "call `update_project_path_idx_rec`();";
  10. // TODO: update fields:
  11. // `IN7_MK_BAZA_DYSTRYBUCJI`.`path`
  12. // `IN7_DZIENNIK_KORESP`.`path`
  13. // `PROBLEMS`.`ID_PROJECT_path`
  14. $db = DB::getDB();
  15. if ($db->has_errors()) {
  16. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  17. }
  18. $res = $db->query($sql);
  19. if ($db->has_errors()) {
  20. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  21. }
  22. die('OK');
  23. }
  24. public function reinstallAction() {
  25. // TODO: reinstall triggers for after insert/update `IN7_MK_BAZA_DYSTRYBUCJI`?
  26. // TODO: reinstall triggers for after insert/update `IN7_DZIENNIK_KORESP` and `PROBLEMS`?
  27. $sqlList = array();
  28. $sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `_project_path_idx`";
  29. $sqlList['InstallTable'] = "
  30. CREATE TABLE IF NOT EXISTS `_project_path_idx` (
  31. `ID` int(11) NOT NULL
  32. , `P_ID` int(11) NOT NULL DEFAULT '0'
  33. , `idx_PATH` varchar(255) NOT NULL DEFAULT ''
  34. , KEY `ID` (`ID`)
  35. , KEY `P_ID` (`P_ID`)
  36. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  37. ";
  38. $sqlList['Remove'] = "DROP PROCEDURE if exists `update_project_path_idx_rec`";
  39. $sqlList['Create'] = "
  40. CREATE PROCEDURE `update_project_path_idx_rec`()
  41. BEGIN
  42. SET @conf_last_exec_key = 'tbl_indexer_project_last_exec';
  43. replace into `CRM_CONFIG` (`conf_key`, `conf_val`) values (@conf_last_exec_key, NOW());
  44. truncate table `_project_path_idx`;
  45. -- delete from `_project_path_idx`;
  46. insert into `_project_path_idx` (`ID`,`P_ID`)
  47. select p.`ID`, p.`P_ID`
  48. from `IN7_MK_BAZA_DYSTRYBUCJI` p
  49. where 1=1
  50. ;
  51. update `_project_path_idx` as p set p.`idx_PATH`=concat('0-', p.`ID`) where p.`P_ID` is null or p.`P_ID`=0;
  52. SET @i = 0;
  53. SET @loopLomit = 100;
  54. SET @pinitCnt = 1;
  55. WHILE @i < @loopLomit and @pinitCnt > 0 DO
  56. update `_project_path_idx` p join `_project_path_idx` pp on(pp.`ID`=p.`P_ID`)
  57. set p.`idx_PATH`=concat(pp.`idx_PATH`, '-', p.`ID`)
  58. where p.`idx_PATH`='' and pp.`idx_PATH`!='';
  59. SET @pinitCnt = ROW_COUNT();
  60. SET @i = @i + 1;
  61. END WHILE;
  62. END ;
  63. ";
  64. $db = DB::getDB();
  65. if ($db->has_errors()) {
  66. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  67. }
  68. foreach ($sqlList as $sql) {
  69. $res = $db->query($sql);
  70. if ($db->has_errors()) {
  71. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  72. }
  73. }
  74. die('OK');
  75. }
  76. }