Example #1
0
 /**
  * Method create dump of database
  *
  * @param null $module
  * @param string $name
  * @param string $whitelist
  * @param string $blacklist
  * @return string
  * @throws Zend_Exception
  */
 public function create($module = null, $name = '', $whitelist = "", $blacklist = "")
 {
     $database = new Core_Db_Database($this->getOptions($whitelist, $blacklist));
     if ($dump = $database->getDump()) {
         $path = $this->getDumpsDirectoryPath($module);
         if (!$name) {
             list($sec, $msec) = explode(".", microtime(true));
             $name = date('Ymd_His_') . substr($msec, 0, 2) . '.sql';
         }
         file_put_contents($path . DIRECTORY_SEPARATOR . $name, $dump);
         return $name;
     } else {
         throw new Zend_Exception("Can not get database dump!");
     }
 }
Example #2
0
 /**
  * Get difference between table indexes
  *
  * @param $table
  */
 protected function createIndexDifference($table)
 {
     $currentIndexes = $this->_currentDb->getIndexList($table);
     $publishedIndexes = $this->_publishedDb->getIndexList($table);
     foreach ($currentIndexes as $curIndex) {
         $indexForCompare = $this->findIndex($curIndex, $publishedIndexes);
         if (!$indexForCompare) {
             $this->up(Core_Db_Database::addIndex($curIndex));
             $this->up(Core_Db_Database::addConstraint($curIndex));
             $this->down(Core_Db_Database::dropConstraint($curIndex));
             $this->down(Core_Db_Database::dropIndex($curIndex));
         } elseif ($indexForCompare !== $curIndex) {
             $this->up(Core_Db_Database::dropConstraint($curIndex));
             $this->up(Core_Db_Database::dropIndex($curIndex));
             $this->down(Core_Db_Database::dropConstraint($curIndex));
             $this->down(Core_Db_Database::dropIndex($curIndex));
             $this->down(Core_Db_Database::addIndex($indexForCompare));
             $this->down(Core_Db_Database::addConstraint($indexForCompare));
         }
     }
     //For creating deleted indexes
     $deletedIndexes = $this->getDeletedIndexes($currentIndexes, $publishedIndexes);
     if ($deletedIndexes) {
         foreach ($deletedIndexes as $deletedIndex) {
             //Create deleted index
             $this->up(Core_Db_Database::dropConstraint($deletedIndex));
             $this->up(Core_Db_Database::dropIndex($deletedIndex));
             //Delete index
             $this->down(Core_Db_Database::addConstraint($deletedIndex));
             $this->down(Core_Db_Database::addIndex($deletedIndex));
         }
     }
 }
Example #3
0
 public function testCreateSuccess()
 {
     $db = new Core_Migration_Adapter_Mysql(Zend_Db_Table::getDefaultAdapter());
     $db->query(Core_Db_Database::dropTable(self::TABLE_NAME));
     $db->createTable(self::TABLE_NAME);
     $db->createColumn(self::TABLE_NAME, 'col1', Core_Migration_Abstract::TYPE_INT);
     $db->createColumn(self::TABLE_NAME, 'col2', Core_Migration_Abstract::TYPE_VARCHAR, 50);
     $db->query(Core_Db_Database::dropTable('test_black_table1'));
     $db->createTable('test_black_table1');
     $db->query(Core_Db_Database::dropTable('test_black_table2'));
     $db->createTable('test_black_table2');
     $dumpName = $this->_getManager()->create(self::FIXTURE_MODULE, self::DUMP_FILE_NAME, self::TABLE_NAME, 'test_black_table1,test_black_table2');
     $compareTo = Core_Db_Database::dropTable(self::TABLE_NAME) . ';' . PHP_EOL . Core_Db_Database::createTable(self::TABLE_NAME) . ';' . PHP_EOL;
     $this->assertEquals(self::DUMP_FILE_NAME, $dumpName);
     $dumpFullPath = $this->_getManager()->getDumpsDirectoryPath(self::FIXTURE_MODULE) . DIRECTORY_SEPARATOR . $dumpName;
     if (file_exists($dumpFullPath)) {
         $dump = file_get_contents($dumpFullPath);
         $this->assertEquals($compareTo, $dump);
     } else {
         $this->fail('Dump file not exist!');
     }
     $db->dropTable('test_table');
     $db->dropTable('test_black_table1');
     $db->dropTable('test_black_table2');
 }
Example #4
0
 public function testCreateSuccess()
 {
     $db = new Core_Migration_Adapter_Mysql(Zend_Db_Table::getDefaultAdapter());
     $db->query(Core_Db_Database::dropTable(self::TABLE_NAME));
     $db->createTable(self::TABLE_NAME);
     $db->createColumn(self::TABLE_NAME, 'col1', Core_Migration_Abstract::TYPE_INT);
     $db->createColumn(self::TABLE_NAME, 'col2', Core_Migration_Abstract::TYPE_TEXT);
     $db->query(Core_Db_Database::dropTable('test_black_table1'));
     $db->createTable('test_black_table1');
     $db->query(Core_Db_Database::dropTable('test_black_table2'));
     $db->createTable('test_black_table2');
     $testData = array('id' => '1', 'col1' => '11', 'col2' => '<p>ZFCore is a content management framework based on Zend Framework. It was developed by PHP Team of <a href="http://nixsolutions.com/">NIX Solutions Ltd</a>. In case you are interested in getting a multifunctional, secure project reliable in data processing like ZFCore, you can always turn to our skillful and experienced team of PHP developers. Moreover, we don\'t limit ourselves with PHP because sometimes it\'s useful to support projects with mobile applications. iPhone and Android Teams of NIX Solutions Ltd. are always ready to help and develop for you any kind of mobile apps.</p>');
     $db->insert(self::TABLE_NAME, $testData);
     $dumpName = $this->_getManager()->create(self::FIXTURE_MODULE, self::DUMP_FILE_NAME, self::TABLE_NAME, 'test_black_table1,test_black_table2');
     $compareTo = Core_Db_Database::dropTable(self::TABLE_NAME) . ';' . PHP_EOL . Core_Db_Database::createTable(self::TABLE_NAME) . ';' . PHP_EOL . Core_Db_Database::insert(self::TABLE_NAME, $testData) . ';' . PHP_EOL;
     $this->assertEquals(self::DUMP_FILE_NAME, $dumpName);
     $dumpFullPath = $this->_getManager()->getDumpsDirectoryPath(self::FIXTURE_MODULE) . DIRECTORY_SEPARATOR . $dumpName;
     if (file_exists($dumpFullPath)) {
         $dump = file_get_contents($dumpFullPath);
         $this->assertEquals($compareTo, $dump);
     } else {
         $this->fail('Dump file not exist!');
     }
     $db->dropTable('test_table');
     $db->dropTable('test_black_table1');
     $db->dropTable('test_black_table2');
 }
Example #5
0
 /**
  * get difference between table indexes
  * @param $table
  */
 protected function createIndexDifference($table)
 {
     $currentIndexes = $this->_currentDb->getIndexList($table);
     $publishedIndexes = $this->_publishedDb->getIndexList($table);
     foreach ($currentIndexes as $curIndex) {
         $indexForCompare = $this->checkIndexExists($curIndex, $publishedIndexes);
         if (!$indexForCompare) {
             $this->down(Core_Db_Database::dropConstraint($curIndex));
             $this->down(Core_Db_Database::dropIndex($curIndex));
             $this->up(Core_Db_Database::dropConstraint($curIndex));
             $this->up(Core_Db_Database::dropIndex($curIndex));
             $this->up(Core_Db_Database::addIndex($curIndex));
             $this->up(Core_Db_Database::addConstraint($curIndex));
         } elseif ($indexForCompare === $curIndex) {
             continue;
         } else {
             $this->down(Core_Db_Database::dropConstraint($curIndex));
             $this->down(Core_Db_Database::dropIndex($curIndex));
             $this->down(Core_Db_Database::addIndex($indexForCompare));
             $this->down(Core_Db_Database::addConstraint($indexForCompare));
             $this->up(Core_Db_Database::dropConstraint($curIndex));
             $this->up(Core_Db_Database::dropIndex($curIndex));
             $this->up(Core_Db_Database::addIndex($curIndex));
             $this->up(Core_Db_Database::addConstraint($curIndex));
         }
     }
 }
Example #6
0
 /**
  * Test for method `generate`
  */
 public function testGenerateMigrationSuccess()
 {
     $db = new Core_Migration_Adapter_Mysql(Zend_Db_Table::getDefaultAdapter());
     $db->query(Core_Db_Database::dropTable('test_table'));
     $db->createTable('test_table');
     $db->createColumn('test_table', 'col1', Core_Migration_Abstract::TYPE_INT);
     $db->createColumn('test_table', 'col2', Core_Migration_Abstract::TYPE_VARCHAR, 50);
     $diff = $this->_getManager()->generateMigration(null, '', 'test_table', true);
     $compareTo = array('down' => array(Core_Db_Database::dropTable('test_table')), 'up' => array(Core_Db_Database::dropTable('test_table'), Core_Db_Database::createTable('test_table')));
     $this->assertEquals($compareTo, $diff);
     $db->dropTable('test_table');
 }
Example #7
0
 /**
  * check db state in last migration, if state is empty
  * save current db state to migration
  * @param $module
  * @return bool
  */
 public function checkState($module)
 {
     $lastMigration = $this->getLastMigration($module);
     $dbAdapter = Zend_Db_Table::getDefaultAdapter();
     $dbState = $this->getLastDbState($module);
     if (!$dbState) {
         $db = new Core_Db_Database();
         $dbAdapter->update($this->_options['migrationsSchemaTable'], array('state' => $db->toString()), array($dbAdapter->quoteInto('migration=?', $lastMigration), $dbAdapter->quoteInto('module=?', $module)));
         return true;
     }
     return false;
 }
Example #8
0
 /**
  * Method upgrade all migration or migrations to selected
  *
  * @param string $module Module name
  * @param string $to     Migration name or label
  * @throws Core_Exception
  * @return void
  */
 public function up($module = null, $to = null)
 {
     $lastMigration = $this->getLastMigration($module);
     $lastMigration = $lastMigration['migration'];
     if ($fullMigrationName = $this->getMigrationFullName($to, $module)) {
         $to = $fullMigrationName;
     }
     if ($to) {
         if (!self::isMigration($to)) {
             throw new Core_Exception("Migration name `{$to}` is not valid");
         } elseif ($lastMigration == $to) {
             throw new Core_Exception("Migration `{$to}` is current");
         } elseif ($lastMigration > $to) {
             throw new Core_Exception("Migration `{$to}` is older than current " . "migration `{$lastMigration}`");
         }
     }
     $exists = $this->getExistsMigrations($module);
     $loaded = $this->getLoadedMigrations($module);
     $ready = array_diff($exists, $loaded);
     if (sizeof($ready) == 0) {
         if ($module) {
             array_push($this->_messages, $module . ': no migrations to upgrade.');
         } else {
             array_push($this->_messages, 'No migrations to upgrade.');
         }
         return;
     }
     sort($ready);
     if ($to && !in_array($to, $exists)) {
         throw new Core_Exception("Migration `{$to}` not exists");
     }
     @set_time_limit(0);
     foreach ($ready as $migration) {
         if ($migration < $lastMigration) {
             throw new Core_Exception("Migration `{$migration}` is conflicted");
         }
         try {
             $includePath = $this->getMigrationsDirectoryPath($module) . '/' . $migration . '.php';
             include_once $includePath;
             $moduleAddon = null !== $module ? ucfirst($module) . '_' : '';
             $migrationClass = $moduleAddon . 'Migration_' . $migration;
             /** @var Core_Migration_Abstract $migrationObject */
             $migrationObject = new $migrationClass();
             $migrationObject->setMigrationMananger($this);
             if (!$this->_transactionFlag) {
                 $migrationObject->getDbAdapter()->beginTransaction();
                 $this->_transactionFlag = true;
                 try {
                     $migrationObject->up();
                     $migrationObject->getDbAdapter()->commit();
                 } catch (Exception $e) {
                     $migrationObject->getDbAdapter()->rollBack();
                     throw new Core_Exception($e->getMessage());
                 }
                 $this->_transactionFlag = false;
             } else {
                 $migrationObject->up();
             }
             if ($module) {
                 array_push($this->_messages, $module . ": upgrade to revision `{$migration}`");
             } else {
                 array_push($this->_messages, "Upgrade to revision `{$migration}`");
             }
             $this->_pushMigration($module, $migration);
             // add db state to migration
             $db = new Core_Db_Database(array('blacklist' => $this->_options['migrationsSchemaTable']));
             $dbAdapter = Zend_Db_Table::getDefaultAdapter();
             $dbAdapter->update($this->_options['migrationsSchemaTable'], array('state' => $db->toString()), array($dbAdapter->quoteInto('migration=?', $migration), $dbAdapter->quoteInto('module=?', $module)));
         } catch (Exception $e) {
             throw new Core_Exception("Migration `{$migration}` return exception:\n" . $e->getMessage());
         }
         if ($to && $migration == $to) {
             break;
         }
     }
 }
Example #9
0
 /**
  * create query for change column
  * @param $tableName
  * @param $column
  * @return string
  */
 public static function changeColumn($tableName, $column)
 {
     $sql = "ALTER TABLE `{$tableName}` CHANGE " . " `{$column['COLUMN_NAME']}` `{$column['COLUMN_NAME']}` " . addslashes($column['DATA_TYPE']);
     Core_Db_Database::addSqlExtras($sql, $column);
     return $sql;
 }