/** * 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(Database::addIndex($curIndex)); $this->up(Database::addConstraint($curIndex)); $this->down(Database::dropConstraint($curIndex)); $this->down(Database::dropIndex($curIndex)); } elseif ($indexForCompare !== $curIndex) { $this->up(Database::dropConstraint($curIndex)); $this->up(Database::dropIndex($curIndex)); $this->down(Database::dropConstraint($curIndex)); $this->down(Database::dropIndex($curIndex)); $this->down(Database::addIndex($indexForCompare)); $this->down(Database::addConstraint($indexForCompare)); } } //For creating deleted indexes $deletedIndexes = $this->getDeletedIndexes($currentIndexes, $publishedIndexes); if ($deletedIndexes) { foreach ($deletedIndexes as $deletedIndex) { //Create deleted index $this->up(Database::dropConstraint($deletedIndex)); $this->up(Database::dropIndex($deletedIndex)); //Delete index $this->down(Database::addConstraint($deletedIndex)); $this->down(Database::addIndex($deletedIndex)); } } }
public function testCreateDumpSuccess() { $db = new Mysql(self::$db); $db->query(Database::dropTable(self::TABLE_NAME)); $db->createTable(self::TABLE_NAME); $db->createColumn(self::TABLE_NAME, 'col1', AbstractMigration::TYPE_INT); $db->createColumn(self::TABLE_NAME, 'col2', AbstractMigration::TYPE_TEXT); // dispatch url $this->dispatch('create dump --name=' . self::DUMP_FILE_NAME . ' --whitelist=' . self::TABLE_NAME); $this->assertResponseStatusCode(0); $this->assertActionName('create'); $this->assertControllerName('ZFCTool\\Controller\\Dump'); $this->assertControllerClass('DumpController'); $this->assertMatchedRouteName('create-dump'); $path = self::$manager->getDumpsDirectoryPath(); $this->assertTrue(is_file($path . DIRECTORY_SEPARATOR . self::DUMP_FILE_NAME)); }
/** * Add db state to migration * * @param string|null $module * @param string| $migration */ protected function updateDbState($module, $migration) { $db = new Database($this->db, array('blacklist' => $this->options['migrationsSchemaTable'])); $sql = new Sql($this->db); $update = $sql->update($this->getMigrationsSchemaTable()); $where = new Where(); $where->equalTo('module', $module)->and->equalTo('migration', $migration); $update->where($where); $update->set(array('state' => $db->toString())); $selectString = $sql->getSqlStringForSqlObject($update); $this->db->query($selectString, Adapter::QUERY_MODE_EXECUTE); }
public function testCreateSuccess() { $db = new Mysql(self::$db); $db->query(Database::dropTable(self::TABLE_NAME)); $db->createTable(self::TABLE_NAME); $db->createColumn(self::TABLE_NAME, 'col1', AbstractMigration::TYPE_INT); $db->createColumn(self::TABLE_NAME, 'col2', AbstractMigration::TYPE_TEXT); $db->query(Database::dropTable('test_black_table1')); $db->createTable('test_black_table1'); $db->query(Database::dropTable('test_black_table2')); $db->createTable('test_black_table2'); $testData = array('id' => '1', 'col1' => '11', 'col2' => '<p>ZFCTool - Zend Framework 2 command line Tool</p>'); $db->insert(self::TABLE_NAME, $testData); $dumpName = self::$manager->create(self::FIXTURE_MODULE, self::DUMP_FILE_NAME, self::TABLE_NAME, 'test_black_table1,test_black_table2'); $compareTo = Database::getDisableChecksNotation() . Database::dropTable(self::TABLE_NAME) . ';' . PHP_EOL . Database::createTable(self::TABLE_NAME) . ';' . PHP_EOL . Database::insert(self::TABLE_NAME, $testData) . ';' . PHP_EOL; $this->assertEquals(self::DUMP_FILE_NAME, $dumpName); $dumpFullPath = self::$manager->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!'); } //Test generate name and creating file $dumpName = self::$manager->create(null, null, self::TABLE_NAME, 'test_black_table1,test_black_table2'); $dumpFullPath = self::$manager->getDumpsDirectoryPath() . DIRECTORY_SEPARATOR . $dumpName; if (file_exists($dumpFullPath)) { $dump = file_get_contents($dumpFullPath); $this->assertEquals($compareTo, $dump); unlink($dumpFullPath); } else { $this->fail('Dump file not exist!'); } $db->dropTable(self::TABLE_NAME); $db->dropTable('test_black_table1'); $db->dropTable('test_black_table2'); }
/** * Test for method `generate` */ public function testGenerateMigrationSuccess() { $tableName = 'test_table'; //Test empty diff $result = self::$manager->generateMigration(null, '', $tableName); $this->assertFalse($result); $db = new Mysql(self::$db); $db->createTable($tableName); $db->createColumn($tableName, 'col1', AbstractMigration::TYPE_INT); $db->createColumn($tableName, 'col2', AbstractMigration::TYPE_VARCHAR, 50); //Test diff $diff = self::$manager->generateMigration(null, '', $tableName, true); $compareTo = array('down' => array(Database::dropTable($tableName)), 'up' => array(Database::dropTable($tableName), Database::createTable($tableName))); $this->assertEquals($compareTo, $diff); //Test create $migrationPath = self::$manager->generateMigration(null, '', $tableName); $this->assertTrue(is_file($migrationPath)); $migrationFile = file_get_contents($migrationPath); $this->assertContains('CREATE TABLE `' . $tableName . '`', $migrationFile); $this->assertContains('$this->query("DROP TABLE IF EXISTS `' . $tableName . '`");', $migrationFile); unlink($migrationPath); $db->dropTable($tableName); }
/** * Method create dump of database * * @param null $module * @param string $name * @param string $whitelist * @param string $blacklist * @return string * @throws ZFCToolException */ public function create($module = null, $name = '', $whitelist = "", $blacklist = "") { $database = new Database($this->db, $this->getOptions($whitelist, $blacklist)); if ($dump = $database->getDump()) { $path = $this->getDumpsDirectoryPath($module); if (!$name) { list(, $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 EmptyDumpException("Can not get database dump!"); } }
public function testDiffModuleDb() { $db = new Mysql(self::$db); $db->query(Database::dropTable(self::TABLE_NAME)); $db->createTable(self::TABLE_NAME); $db->createColumn(self::TABLE_NAME, 'col1', AbstractMigration::TYPE_INT); $db->createColumn(self::TABLE_NAME, 'col2', AbstractMigration::TYPE_TEXT); // dispatch url $this->dispatch('diff db --module=' . self::FIXTURE_MODULE . ' --whitelist=' . self::TABLE_NAME); $this->assertResponseStatusCode(0); $this->assertActionName('diff'); $this->assertControllerName('ZFCTool\\Controller\\Migration'); $this->assertControllerClass('MigrationController'); $this->assertMatchedRouteName('diff-db'); $response = ob_get_contents(); $this->assertContains('Queries (2) :', $response); $db->query(Database::dropTable(self::TABLE_NAME)); }
/** * 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']); Database::addSqlExtras($sql, $column); return $sql; }