compare database structure


/ Published in: PHP
Save to your folder(s)

comparte structure databases


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3. * Script para sincronizar tablas
  4. **/
  5. function compareTables(
  6. $tableToCompare,
  7. $tableOriginal
  8. )
  9. {
  10. $comparsionResult = array(
  11. 'columnNotExist' => array(),
  12. 'differentDataType' => array()
  13. );
  14.  
  15. foreach ($tableToCompare as $columnName => $columnType) {
  16. if (!isset($tableOriginal[$columnName])) { // no existe la columna en la tabla
  17. $comparsionResult['columnNotExist'],
  18. $columnName
  19. );
  20. } else { // existe la columna, pero el tipo de datos es diferente
  21. if ($tableOriginal[$columnName] != $columnType) {
  22. $comparsionResult['differentDataType'],
  23. array($columnName => $columnType)
  24. );
  25. }
  26. }
  27. }
  28.  
  29. return $comparsionResult;
  30. }
  31.  
  32. class Database
  33. {
  34.  
  35. protected $_cnx;
  36.  
  37. protected $_database;
  38.  
  39. protected $_result;
  40.  
  41. protected $_queryResource;
  42.  
  43. protected $_sql;
  44.  
  45. public function __construct(
  46. $host,
  47. $user,
  48. $pass,
  49. $database
  50. )
  51. {
  52. $this->_connect($host, $user, $pass, $database);
  53. }
  54.  
  55. public function getSchema()
  56. {
  57. $tables = $this->_getTables();
  58. $schemaTables = array();
  59.  
  60. foreach ($tables as $table) {
  61. $schemaTables[$table] = $this->_getTableSchema( $table );
  62. }
  63.  
  64. return $schemaTables;
  65. }
  66.  
  67. public function createTable($table)
  68. {
  69. $this->_sql = 'CREATE TABLE ';
  70. return $this->_query();
  71. }
  72.  
  73. public function alterTable()
  74. {}
  75.  
  76. private function _getTables()
  77. {
  78. $this->_sql = 'show tables';
  79. return $this->_query()
  80. ->_fetch();
  81. }
  82.  
  83. private function _getTableSchema($table)
  84. {
  85. $this->_sql = "SELECT column_name,data_type
  86. FROM information_schema.columns
  87. WHERE table_schema = '" . $this->_database . "'
  88. AND table_name = '" . $table . "'
  89. ORDER BY column_name";
  90.  
  91. return $this->_query()
  92. ->_fetchSchema();
  93. }
  94.  
  95. private function _query()
  96. {
  97. $this->_queryResource = mysql_query(
  98. $this->_sql,
  99. $this->_cnx
  100. );
  101. return $this;
  102. }
  103.  
  104. private function _fetchSchema()
  105. {
  106. $result = array();
  107.  
  108. while ($rs = mysql_fetch_array($this->_queryResource)) {
  109. $result[$rs['column_name']] = $rs['data_type'];
  110. }
  111.  
  112. return $result;
  113. }
  114.  
  115. private function _fetch()
  116. {
  117. $result = array();
  118.  
  119. while ($rs = mysql_fetch_array($this->_queryResource)) {
  120. array_push($result, $rs[0]);
  121. }
  122.  
  123. return $result;
  124. }
  125.  
  126. private function _connect($host, $user, $pass, $database)
  127. {
  128. $this->_database = $database;
  129. $this->_cnx =& mysql_connect($host, $user, $pass) or die('No se pudo conectar al host ' . $host);
  130. mysql_select_db($database);
  131. }
  132.  
  133. }
  134.  
  135. //*************************************\\
  136. // \\
  137. // SE INICIA EL SCRIPT ACA... \\
  138. // \\
  139. //*************************************\\
  140.  
  141. /** Conecto las bases **/
  142. $qaDatabase = new Database(
  143. 'domain',
  144. 'user',
  145. '****',
  146. 'database'
  147. );
  148.  
  149. $liveDatabase = new Database(
  150. 'domain',
  151. 'user',
  152. '****',
  153. 'database'
  154. );
  155.  
  156. // mysqldump --lock-tables=false -hlive-latam-01.811.mtvi.com -ubrazilsvsmtvlamw -p mtvbrazilservices > mtvbrazilbackup.sql
  157.  
  158. /** Obtengo la estructura de la base de datos **/
  159. $schemaQaTables = $qaDatabase->getSchema();
  160. $schemaLiveTables = $liveDatabase->getSchema();
  161.  
  162. // Inicializo la memoria para generar un resultado
  163. $scriptSincronizationResult = array(
  164. 'missingTables' => array(),
  165. 'tableDiffs' => array()
  166. );
  167.  
  168. /** Comparación de esquema de Tablas de Live a QA **/
  169. foreach ($schemaQaTables as $tableName => $columns) {
  170.  
  171. if (!isset($schemaLiveTables[$tableName])) { // Si la tabla de QA no está en Live, la creamos
  172.  
  173. /*if ($qaDatabase->createTable($schemaQaTables[$tableName])) {
  174. $scriptSincronizationResult['created'] = array($tableName);
  175. }*/
  176. array_push($scriptSincronizationResult['missingTables'], $tableName);
  177.  
  178. } else { // Si existe, comparamos las tablas
  179.  
  180. $scriptSincronizationResult['tableDiffs'][$tableName] = compareTables(
  181. $schemaLiveTables[$tableName],
  182. $schemaQaTables[$tableName],
  183. $tableName
  184. );
  185.  
  186. }
  187.  
  188. }
  189.  
  190. //*************************************\\
  191. // \\
  192. // OUTPUT DEL SCRIPT ACA... \\
  193. // \\
  194. //*************************************\\
  195. echo "<pre>";
  196. var_dump($scriptSincronizationResult);
  197.  
  198. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.