/** * @param string $xml * * @return Database */ public function applyXml($xml) { $this->readDatabase(); $builder = new QuickBuilder(); $builder->setPlatform($this->database->getPlatform()); $builder->setSchema($xml); $database = $builder->getDatabase(); $database->setSchema('migration'); $database->setPlatform($this->database->getPlatform()); $diff = DatabaseComparator::computeDiff($this->database, $database); if (false === $diff) { return null; } $sql = $this->database->getPlatform()->getModifyDatabaseDDL($diff); $this->con->beginTransaction(); $statements = SqlParser::parseString($sql); foreach ($statements as $statement) { try { $stmt = $this->con->prepare($statement); $stmt->execute(); } catch (\Exception $e) { $this->con->rollBack(); throw new BuildException(sprintf("Can not execute SQL: \n%s\nFrom database: \n%s\n\nTo database: \n%s\n", $statement, $this->database, $database), null, $e); } } $this->con->commit(); return $database; }
/** * @param string $xml * * @return Database|boolean */ public function applyXml($xml, $changeRequired = false) { $this->readDatabase(); $builder = new QuickBuilder(); $builder->setIdentifierQuoting(true); $builder->setPlatform($this->database->getPlatform()); $builder->setSchema($xml); $database = $builder->getDatabase(); $database->setSchema('migration'); $database->setPlatform($this->database->getPlatform()); $diff = DatabaseComparator::computeDiff($this->database, $database); if (false === $diff) { if ($changeRequired) { throw new BuildException(sprintf("No changes in schema to current database: \nSchema database:\n%s\n\nCurrent Database:\n%s", $database, $this->database)); } return false; } $sql = $this->database->getPlatform()->getModifyDatabaseDDL($diff); $this->con->beginTransaction(); if (!$sql) { throw new BuildException(sprintf('Ooops. There is a diff between current database and schema xml but no SQL has been generated. Change: %s', $diff)); } $statements = SqlParser::parseString($sql); foreach ($statements as $statement) { try { $stmt = $this->con->prepare($statement); $stmt->execute(); } catch (\Exception $e) { throw new BuildException(sprintf("Can not execute SQL: \n%s\nFrom database: \n%s\n\nTo database: \n%s\n", $statement, $this->database, $database), null, $e); } } $this->con->commit(); return $database; }
private function checkDeletedFk() { $this->readDatabase(); $diff = DatabaseComparator::computeDiff($this->database, $this->updatedBuilder->getDatabase()); $sql = $this->database->getPlatform()->getModifyDatabaseDDL($diff); $expected = 'issue617_user'; $this->assertNotContains($expected, $sql); }
public function testValidateReturnsTrueForValidSchema() { $schema = <<<EOF <database name="bookstore"> <table name="book"> <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" /> <column name="title" type="VARCHAR" size="100" primaryString="true" /> </table> </database> EOF; $builder = new QuickBuilder(); $builder->setSchema($schema); $database = $builder->getDatabase(); $appData = new AppData(); $appData->addDatabase($database); $validator = new SchemaValidator($appData); $this->assertTrue($validator->validate()); }
/** * Setups the initial schema. */ private function setupInitSchema() { /* * Create issue617 tables with foreign keys */ $schema = ' <database name="bookstore"> <table name="issue617_user"> <vendor type="mysql"> <parameter name="Engine" value="InnoDB"/> <parameter name="Charset" value="utf8"/> </vendor> <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" /> <column name="full_name" type="VARCHAR" size="50" required="true" /> <!-- this column (and FK) will be removed from schema, but not from DB on migrate --> <column name="group_id" type="INTEGER" /> <foreign-key foreignTable="issue617_group" onDelete="setnull"> <reference local="group_id" foreign="id" /> </foreign-key> </table> <table name="issue617_group"> <vendor type="mysql"> <parameter name="Engine" value="InnoDB"/> <parameter name="Charset" value="utf8"/> </vendor> <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" /> <column name="name" type="VARCHAR" size="50" required="true" /> </table> </database> '; $builder = new QuickBuilder(); $builder->setPlatform($this->database->getPlatform()); $builder->setSchema($schema); $diff = DatabaseComparator::computeDiff($this->database, $builder->getDatabase()); $sql = $this->database->getPlatform()->getModifyDatabaseDDL($diff); $expected = ' CREATE TABLE `issue617_user` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `full_name` VARCHAR(50) NOT NULL, `group_id` INTEGER, PRIMARY KEY (`id`), INDEX `issue617_user_fi_5936b3` (`group_id`), CONSTRAINT `issue617_user_fk_5936b3` FOREIGN KEY (`group_id`) REFERENCES `issue617_group` (`id`) ON DELETE SET NULL ) ENGINE=InnoDB CHARACTER SET=\'utf8\'; CREATE TABLE `issue617_group` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `name` VARCHAR(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB CHARACTER SET=\'utf8\'; '; $this->assertContains($expected, $sql); $this->updateSchema($builder->getDatabase()); }
public function testModiFyTableDoesNotMoveValidatorsOnNonI18nColumns() { $schema = <<<EOF <database name="i18n_behavior_test_0"> <table name="i18n_behavior_test_0"> <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" /> <validator column="id"> <rule name="minLength" value="4" message="title must be at least 4 characters !" /> </validator> <column name="title" type="VARCHAR" /> <behavior name="i18n"> <parameter name="i18n_columns" value="title" /> </behavior> </table> </database> EOF; $builder = new QuickBuilder(); $builder->setSchema($schema); $table = $builder->getDatabase()->getTable('i18n_behavior_test_0'); $this->assertEquals(1, count($table->getValidators())); $i18nTable = $builder->getDatabase()->getTable('i18n_behavior_test_0_i18n'); $this->assertEquals(array(), $i18nTable->getValidators()); }