db_structupdater.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. error_reporting(E_ALL & ~E_NOTICE);
  3. define('DS', DIRECTORY_SEPARATOR);
  4. define('APP_PATH_ROOT', dirname(__FILE__));
  5. define('APP_PATH_WWW', dirname(__FILE__));
  6. define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');
  7. //session_save_path("../session") ;
  8. session_start();
  9. date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
  10. if (file_exists(".config.php")) include(".config.php");
  11. //SEF('DEBUG_S');
  12. require_once dirname(__FILE__) . '/' . 'se-lib' . '/' . 'Lib.php';
  13. Lib::loadClass('V');
  14. Lib::loadClass('User');
  15. Lib::loadClass('Config');
  16. Lib::loadClass('App');
  17. Lib::loadClass('DB');
  18. Lib::loadClass('S');
  19. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
  20. echo "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-2\"/>";
  21. echo '<script type="text/javascript" src="stuff/jquery.js"></script>';
  22. echo "\n".'<link rel="stylesheet" type="text/css" href="stuff/main.css" />'."\n";
  23. echo '</head>';
  24. echo '<body>';
  25. Lib::loadClass('DB_Dump');
  26. if (!User::logged()) {
  27. die("Require login!");
  28. }
  29. if (User::get('ADM_ADMIN_LEVEL') != 0) {
  30. die("Access denied!");
  31. }
  32. $conf = Config::getZasobConf('Xdb_structupdater');
  33. if (!$conf) {
  34. die("Error config for 'Xdb_structupdater' not exists!");
  35. }
  36. $db_allowed = V::get('allowed_db', '', $conf);
  37. if (empty($db_allowed)) {
  38. die("Error in config - no allowe db set!");
  39. }
  40. $db_allowed = explode(",", $db_allowed);
  41. $tbl_prefix_allowed = V::get('allowed_tbl_prefix', '', $conf);
  42. $tbl_prefix_allowed = (!empty($tbl_prefix_allowed))? explode(",", $tbl_prefix_allowed) : array('%');
  43. $struct1_source = V::get('_s1', 'default', $_GET);
  44. $struct2_source = V::get('_s2', '', $_GET);
  45. $_tbl_prefix = V::get('_tbl_prefix', '', $_GET);
  46. if (!in_array($struct1_source, $db_allowed)) $struct1_source = '';
  47. if (!in_array($struct2_source, $db_allowed)) $struct2_source = '';
  48. echo '<form action="" method="get">';
  49. echo "Source 1: ";
  50. echo '<select name="_s1">';
  51. echo '<option value="">' . " ... " . '</option>';
  52. foreach ($db_allowed as $db_name) {
  53. $sel = ($struct1_source == $db_name)? ' selected="selected"' : '';
  54. echo '<option value="' . $db_name . '"' . $sel . '>' . $db_name . '</option>';
  55. }
  56. echo '</select>';
  57. echo "Source 2: ";
  58. echo '<select name="_s2">';
  59. echo '<option value="">' . " ... " . '</option>';
  60. foreach ($db_allowed as $db_name) {
  61. $sel = ($struct2_source == $db_name)? ' selected="selected"' : '';
  62. echo '<option value="' . $db_name . '"' . $sel . '>' . $db_name . '</option>';
  63. }
  64. echo '</select>';
  65. echo "Tbl prefix: ";
  66. echo '<select name="_tbl_prefix">';
  67. echo '<option value="">' . " ... " . '</option>';
  68. foreach ($tbl_prefix_allowed as $tbl_prefix) {
  69. $sel = ($_tbl_prefix == $tbl_prefix)? ' selected="selected"' : '';
  70. echo '<option value="' . $tbl_prefix . '"' . $sel . '>' . $tbl_prefix . '</option>';
  71. }
  72. echo '</select>';
  73. echo " " . '<input type="submit" value="' . "Wy¶lij" . '" />';
  74. echo '</form>';
  75. if (empty($struct1_source) || empty($struct2_source) || empty($_tbl_prefix)) {
  76. die('<p>' . "Wybierz bazy danych i prefix tabel" . '</p>');
  77. }
  78. $db = DB::getDB($struct1_source);
  79. if (!$db) {
  80. die("No db '" . $struct1_source . "'");
  81. } else if ($db->has_errors()) {
  82. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">'."Errors db '" . $struct1_source . "':\n";print_r($db->get_errors());echo'</pre>';
  83. return;
  84. }
  85. $db2 = DB::getDB($struct2_source);// main db - sowa
  86. if (!$db2) {
  87. die("No db '" . $struct2_source . "'");
  88. } else if ($db2->has_errors()) {
  89. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">'."Errors db '" . $struct2_source . "':\n";print_r($db2->get_errors());echo'</pre>';
  90. return;
  91. }
  92. Lib::loadClass('DB_Util');
  93. $tbl_name = 'CRM_LISTA_ZASOBOW';
  94. $ret = DB_Util::get_table_struct($db, $tbl_name);
  95. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">ret: ';print_r($ret);echo'</pre>';
  96. $ret = DB_Util::get_table_struct($db2, $tbl_name);
  97. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">ret2: ';print_r($ret);echo'</pre>';
  98. $ret = DB_Util::compare_db_table($db, $db2, $tbl_name);
  99. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">ret3: ';print_r($ret);echo'</pre>';
  100. if ($ret === null || !is_array($ret)) {
  101. echo 'diff null';
  102. } else if (empty($ret)) {
  103. echo 'diff empty';
  104. } else {
  105. $sqls = DB_Util::compare_db_table_to_sql($tbl_name, $ret);
  106. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sqls: ';print_r($sqls);echo'</pre>';
  107. }
  108. echo '<hr />';
  109. $struct1 = DB_Dump::get_db_structure_by_table_prefix($db, $_tbl_prefix);
  110. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">$struct1: ';print_r($struct1);echo'</pre>';
  111. if (!$struct1) {
  112. die("No struct for '" . $struct1_source . "'.");
  113. }
  114. $struct2 = DB_Dump::get_db_structure_by_table_prefix($db2, $_tbl_prefix);
  115. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">$struct2: ';print_r($struct2);echo'</pre>';
  116. if (!$struct2) {
  117. die("No struct for '" . $struct2_source . "'.");
  118. }
  119. echo '<p>'."generating struct diff...".'</p>';
  120. Lib::loadClass('DB_StructUpdater');
  121. if (V::get('test', '', $_GET) == '123') {
  122. $file_struct1 = dirname(__FILE__) . '/../../sources/sowa.sql-bup-last/dump-diff-test.1.sql';
  123. $file_struct2 = dirname(__FILE__) . '/../../sources/sowa.sql-bup-last/dump-diff-test.2.sql';
  124. if (!file_exists($file_struct1)) {
  125. die("file struct1.sql not exists!");
  126. }
  127. if (!file_exists($file_struct2)) {
  128. die("file struct2.sql not exists!");
  129. }
  130. $struct1 = file_get_contents($file_struct1);
  131. $struct2 = file_get_contents($file_struct2);
  132. }
  133. echo '<table width="100%">';
  134. echo '<tr>';
  135. echo '<td width="50%">';
  136. echo '<p style="margin:0;padding:10px 0 0 0;">'."struct1.sql (" . $struct1_source . ")".'</p>';
  137. echo '<textarea style="width:100%;height:300px;border:1px solid #333">';
  138. echo $struct1;
  139. echo '</textarea>';
  140. echo '</td>';
  141. echo '<td width="50%">';
  142. echo '<p style="margin:0;padding:10px 0 0 0;">'."struct2.sql (" . $struct2_source . ")".'</p>';
  143. echo '<textarea style="width:100%;height:300px;border:1px solid #333">';
  144. echo $struct2;
  145. echo '</textarea>';
  146. echo '</td>';
  147. echo '</tr>';
  148. echo '</table>';
  149. $updater = new DB_StructUpdater();
  150. $res = $updater->getUpdates($struct2, $struct1);
  151. db_structupdater_print($res, "sync struct2 ($struct2_source) to struct1 ($struct1_source)");
  152. $res = $updater->getUpdates($struct1, $struct2);
  153. db_structupdater_print($res, "sync struct1 ($struct1_source) to struct2 ($struct2_source)");
  154. ?>
  155. <style type="text/css">
  156. .collapse {margin:0; padding:0;}
  157. .collapse dt {display:block; margin:0; padding:0 10px; cursor:pointer;}
  158. .collapse dd {display:block; margin:0; padding:0; overflow:auto;}
  159. .collapse dt em {color:#777; font-size:small;}
  160. .collapse dd .box {display:block; float:left; width:98px; height:120px; text-align:center; font-size:11px; color:#999;}
  161. .collapse dd .box img {clear:both; margin:0 0 3px 0;}
  162. .collapse dd .tbl-view {background:#fff;}
  163. .collapse dt .add-show,
  164. .collapse dt .add-hidden {font-size:normal; font-family:monospace; font-weight:bold;}
  165. .collapse-row-0 dt {background:#BFBFBF; color:black;}
  166. .collapse-row-0 dd {background:#BFBFBF; color:black; border:1px solid #BFBFBF;}
  167. .collapse-row-1 dt {background:#E5E5E5; color:black;}
  168. .collapse-row-1 dd {background:#E5E5E5; color:black; border:1px solid #E5E5E5;}
  169. .collapse-hide dd {display:none;}
  170. .collapse dt .add-show {display:inline;}
  171. .collapse dt .add-hidden {display:none;}
  172. .collapse-hide dt .add-show {display:none;}
  173. .collapse-hide dt .add-hidden {display:inline;}
  174. .collapse .tbl-view th,
  175. .collapse .tbl-view td {font-size:small;}
  176. </style>
  177. <?php
  178. ?>
  179. <script type="text/javascript">
  180. jQuery(document).ready(function(){
  181. jQuery(".collapse dt").click(function(){
  182. jQuery(this).parent().toggleClass('collapse-hide');
  183. return false
  184. });
  185. });
  186. </script>
  187. <?php
  188. function db_structupdater_print($sql_arr, $title) {
  189. $total = count($sql_arr);
  190. echo '<p>' . $title . " (" . $total . ")" . '</p>';
  191. if ($total > 0) {
  192. $t = 0;
  193. foreach ($sql_arr as $line_nr => $line) {
  194. $sql_first_line = substr($line, 0, min(80, strpos($line . "\n", "\n")));
  195. echo '<dl class="collapse collapse-hide collapse-row-' . ($t = 1 - $t) . '">';
  196. echo '<dt>' . "-- " . $title . " (SQL " . ($line_nr + 1) . ")";
  197. echo '<span class="add-show">' . " (- ukryj)" . '</span>';
  198. echo '<span class="add-hidden">' . " (+ poka¿) " . '<em>' . $sql_first_line . " ..." . '</em>' . '</span>';
  199. echo '</dt>';
  200. echo '<dd>';
  201. $out_rows = min(10, count(explode("\n", $line)));
  202. echo '<textarea rows="'.$out_rows.'" style="margin:0 0 0 2%;width:97%;border:1px solid #333;background:#fff;">';
  203. echo $line;
  204. echo '</textarea>';
  205. echo '</dd>';
  206. echo '</dl>';// .collapse
  207. }
  208. }
  209. }