/ Published in: PHP
comparte structure databases
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * Script para sincronizar tablas **/ function compareTables( $tableToCompare, $tableOriginal ) { ); foreach ($tableToCompare as $columnName => $columnType) { $comparsionResult['columnNotExist'], $columnName ); } else { // existe la columna, pero el tipo de datos es diferente if ($tableOriginal[$columnName] != $columnType) { $comparsionResult['differentDataType'], ); } } } return $comparsionResult; } class Database { protected $_cnx; protected $_database; protected $_result; protected $_queryResource; protected $_sql; public function __construct( $host, $user, $pass, $database ) { $this->_connect($host, $user, $pass, $database); } public function getSchema() { $tables = $this->_getTables(); foreach ($tables as $table) { $schemaTables[$table] = $this->_getTableSchema( $table ); } return $schemaTables; } public function createTable($table) { $this->_sql = 'CREATE TABLE '; return $this->_query(); } public function alterTable() {} private function _getTables() { $this->_sql = 'show tables'; return $this->_query() ->_fetch(); } private function _getTableSchema($table) { $this->_sql = "SELECT column_name,data_type FROM information_schema.columns WHERE table_schema = '" . $this->_database . "' AND table_name = '" . $table . "' ORDER BY column_name"; return $this->_query() ->_fetchSchema(); } private function _query() { $this->_sql, $this->_cnx ); return $this; } private function _fetchSchema() { $result[$rs['column_name']] = $rs['data_type']; } return $result; } private function _fetch() { } return $result; } private function _connect($host, $user, $pass, $database) { $this->_database = $database; } } //*************************************\\ // \\ // SE INICIA EL SCRIPT ACA... \\ // \\ //*************************************\\ /** Conecto las bases **/ $qaDatabase = new Database( 'domain', 'user', '****', 'database' ); $liveDatabase = new Database( 'domain', 'user', '****', 'database' ); // mysqldump --lock-tables=false -hlive-latam-01.811.mtvi.com -ubrazilsvsmtvlamw -p mtvbrazilservices > mtvbrazilbackup.sql /** Obtengo la estructura de la base de datos **/ $schemaQaTables = $qaDatabase->getSchema(); $schemaLiveTables = $liveDatabase->getSchema(); // Inicializo la memoria para generar un resultado ); /** Comparación de esquema de Tablas de Live a QA **/ foreach ($schemaQaTables as $tableName => $columns) { /*if ($qaDatabase->createTable($schemaQaTables[$tableName])) { $scriptSincronizationResult['created'] = array($tableName); }*/ } else { // Si existe, comparamos las tablas $scriptSincronizationResult['tableDiffs'][$tableName] = compareTables( $schemaLiveTables[$tableName], $schemaQaTables[$tableName], $tableName ); } } //*************************************\\ // \\ // OUTPUT DEL SCRIPT ACA... \\ // \\ //*************************************\\ echo "<pre>"; exit; ?>