SchemaTool.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Tools;
  20. use Doctrine\ORM\ORMException,
  21. Doctrine\DBAL\Types\Type,
  22. Doctrine\ORM\EntityManager,
  23. Doctrine\ORM\Mapping\ClassMetadata,
  24. Doctrine\ORM\Internal\CommitOrderCalculator,
  25. Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs,
  26. Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
  27. /**
  28. * The SchemaTool is a tool to create/drop/update database schemas based on
  29. * <tt>ClassMetadata</tt> class descriptors.
  30. *
  31. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  32. * @link www.doctrine-project.org
  33. * @since 2.0
  34. * @version $Revision$
  35. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  36. * @author Jonathan Wage <jonwage@gmail.com>
  37. * @author Roman Borschel <roman@code-factory.org>
  38. * @author Benjamin Eberlei <kontakt@beberlei.de>
  39. */
  40. class SchemaTool
  41. {
  42. /**
  43. * @var \Doctrine\ORM\EntityManager
  44. */
  45. private $_em;
  46. /**
  47. * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  48. */
  49. private $_platform;
  50. /**
  51. * Initializes a new SchemaTool instance that uses the connection of the
  52. * provided EntityManager.
  53. *
  54. * @param \Doctrine\ORM\EntityManager $em
  55. */
  56. public function __construct(EntityManager $em)
  57. {
  58. $this->_em = $em;
  59. $this->_platform = $em->getConnection()->getDatabasePlatform();
  60. }
  61. /**
  62. * Creates the database schema for the given array of ClassMetadata instances.
  63. *
  64. * @param array $classes
  65. */
  66. public function createSchema(array $classes)
  67. {
  68. $createSchemaSql = $this->getCreateSchemaSql($classes);
  69. $conn = $this->_em->getConnection();
  70. foreach ($createSchemaSql as $sql) {
  71. $conn->executeQuery($sql);
  72. }
  73. }
  74. /**
  75. * Gets the list of DDL statements that are required to create the database schema for
  76. * the given list of ClassMetadata instances.
  77. *
  78. * @param array $classes
  79. * @return array $sql The SQL statements needed to create the schema for the classes.
  80. */
  81. public function getCreateSchemaSql(array $classes)
  82. {
  83. $schema = $this->getSchemaFromMetadata($classes);
  84. return $schema->toSql($this->_platform);
  85. }
  86. /**
  87. * Some instances of ClassMetadata don't need to be processed in the SchemaTool context. This method detects them.
  88. *
  89. * @param ClassMetadata $class
  90. * @param array $processedClasses
  91. * @return bool
  92. */
  93. private function processingNotRequired($class, array $processedClasses)
  94. {
  95. return (
  96. isset($processedClasses[$class->name]) ||
  97. $class->isMappedSuperclass ||
  98. ($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
  99. );
  100. }
  101. /**
  102. * From a given set of metadata classes this method creates a Schema instance.
  103. *
  104. * @param array $classes
  105. * @return Schema
  106. */
  107. public function getSchemaFromMetadata(array $classes)
  108. {
  109. $processedClasses = array(); // Reminder for processed classes, used for hierarchies
  110. $sm = $this->_em->getConnection()->getSchemaManager();
  111. $metadataSchemaConfig = $sm->createSchemaConfig();
  112. $metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
  113. $schema = new \Doctrine\DBAL\Schema\Schema(array(), array(), $metadataSchemaConfig);
  114. $evm = $this->_em->getEventManager();
  115. foreach ($classes as $class) {
  116. if ($this->processingNotRequired($class, $processedClasses)) {
  117. continue;
  118. }
  119. $table = $schema->createTable($class->getQuotedTableName($this->_platform));
  120. $columns = array(); // table columns
  121. if ($class->isInheritanceTypeSingleTable()) {
  122. $columns = $this->_gatherColumns($class, $table);
  123. $this->_gatherRelationsSql($class, $table, $schema);
  124. // Add the discriminator column
  125. $discrColumnDef = $this->_getDiscriminatorColumnDefinition($class, $table);
  126. // Aggregate all the information from all classes in the hierarchy
  127. foreach ($class->parentClasses as $parentClassName) {
  128. // Parent class information is already contained in this class
  129. $processedClasses[$parentClassName] = true;
  130. }
  131. foreach ($class->subClasses as $subClassName) {
  132. $subClass = $this->_em->getClassMetadata($subClassName);
  133. $this->_gatherColumns($subClass, $table);
  134. $this->_gatherRelationsSql($subClass, $table, $schema);
  135. $processedClasses[$subClassName] = true;
  136. }
  137. } else if ($class->isInheritanceTypeJoined()) {
  138. // Add all non-inherited fields as columns
  139. $pkColumns = array();
  140. foreach ($class->fieldMappings as $fieldName => $mapping) {
  141. if ( ! isset($mapping['inherited'])) {
  142. $columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
  143. $this->_gatherColumn($class, $mapping, $table);
  144. if ($class->isIdentifier($fieldName)) {
  145. $pkColumns[] = $columnName;
  146. }
  147. }
  148. }
  149. $this->_gatherRelationsSql($class, $table, $schema);
  150. // Add the discriminator column only to the root table
  151. if ($class->name == $class->rootEntityName) {
  152. $discrColumnDef = $this->_getDiscriminatorColumnDefinition($class, $table);
  153. } else {
  154. // Add an ID FK column to child tables
  155. /* @var \Doctrine\ORM\Mapping\ClassMetadata $class */
  156. $idMapping = $class->fieldMappings[$class->identifier[0]];
  157. $this->_gatherColumn($class, $idMapping, $table);
  158. $columnName = $class->getQuotedColumnName($class->identifier[0], $this->_platform);
  159. // TODO: This seems rather hackish, can we optimize it?
  160. $table->getColumn($columnName)->setAutoincrement(false);
  161. $pkColumns[] = $columnName;
  162. // Add a FK constraint on the ID column
  163. $table->addUnnamedForeignKeyConstraint(
  164. $this->_em->getClassMetadata($class->rootEntityName)->getQuotedTableName($this->_platform),
  165. array($columnName), array($columnName), array('onDelete' => 'CASCADE')
  166. );
  167. }
  168. $table->setPrimaryKey($pkColumns);
  169. } else if ($class->isInheritanceTypeTablePerClass()) {
  170. throw ORMException::notSupported();
  171. } else {
  172. $this->_gatherColumns($class, $table);
  173. $this->_gatherRelationsSql($class, $table, $schema);
  174. }
  175. $pkColumns = array();
  176. foreach ($class->identifier AS $identifierField) {
  177. if (isset($class->fieldMappings[$identifierField])) {
  178. $pkColumns[] = $class->getQuotedColumnName($identifierField, $this->_platform);
  179. } else if (isset($class->associationMappings[$identifierField])) {
  180. /* @var $assoc \Doctrine\ORM\Mapping\OneToOne */
  181. $assoc = $class->associationMappings[$identifierField];
  182. foreach ($assoc['joinColumns'] AS $joinColumn) {
  183. $pkColumns[] = $joinColumn['name'];
  184. }
  185. }
  186. }
  187. if (!$table->hasIndex('primary')) {
  188. $table->setPrimaryKey($pkColumns);
  189. }
  190. if (isset($class->table['indexes'])) {
  191. foreach ($class->table['indexes'] AS $indexName => $indexData) {
  192. $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName);
  193. }
  194. }
  195. if (isset($class->table['uniqueConstraints'])) {
  196. foreach ($class->table['uniqueConstraints'] AS $indexName => $indexData) {
  197. $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName);
  198. }
  199. }
  200. $processedClasses[$class->name] = true;
  201. if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
  202. $seqDef = $class->sequenceGeneratorDefinition;
  203. if (!$schema->hasSequence($seqDef['sequenceName'])) {
  204. $schema->createSequence(
  205. $seqDef['sequenceName'],
  206. $seqDef['allocationSize'],
  207. $seqDef['initialValue']
  208. );
  209. }
  210. }
  211. if ($evm->hasListeners(ToolEvents::postGenerateSchemaTable)) {
  212. $evm->dispatchEvent(ToolEvents::postGenerateSchemaTable, new GenerateSchemaTableEventArgs($class, $schema, $table));
  213. }
  214. }
  215. if ($evm->hasListeners(ToolEvents::postGenerateSchema)) {
  216. $evm->dispatchEvent(ToolEvents::postGenerateSchema, new GenerateSchemaEventArgs($this->_em, $schema));
  217. }
  218. return $schema;
  219. }
  220. /**
  221. * Gets a portable column definition as required by the DBAL for the discriminator
  222. * column of a class.
  223. *
  224. * @param ClassMetadata $class
  225. * @return array The portable column definition of the discriminator column as required by
  226. * the DBAL.
  227. */
  228. private function _getDiscriminatorColumnDefinition($class, $table)
  229. {
  230. $discrColumn = $class->discriminatorColumn;
  231. if (!isset($discrColumn['type']) || (strtolower($discrColumn['type']) == 'string' && $discrColumn['length'] === null)) {
  232. $discrColumn['type'] = 'string';
  233. $discrColumn['length'] = 255;
  234. }
  235. $table->addColumn(
  236. $discrColumn['name'],
  237. $discrColumn['type'],
  238. array('length' => $discrColumn['length'], 'notnull' => true)
  239. );
  240. }
  241. /**
  242. * Gathers the column definitions as required by the DBAL of all field mappings
  243. * found in the given class.
  244. *
  245. * @param ClassMetadata $class
  246. * @param Table $table
  247. * @return array The list of portable column definitions as required by the DBAL.
  248. */
  249. private function _gatherColumns($class, $table)
  250. {
  251. $columns = array();
  252. $pkColumns = array();
  253. foreach ($class->fieldMappings as $fieldName => $mapping) {
  254. if ($class->isInheritanceTypeSingleTable() && isset($mapping['inherited'])) {
  255. continue;
  256. }
  257. $column = $this->_gatherColumn($class, $mapping, $table);
  258. if ($class->isIdentifier($mapping['fieldName'])) {
  259. $pkColumns[] = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
  260. }
  261. }
  262. // For now, this is a hack required for single table inheritence, since this method is called
  263. // twice by single table inheritence relations
  264. if(!$table->hasIndex('primary')) {
  265. //$table->setPrimaryKey($pkColumns);
  266. }
  267. return $columns;
  268. }
  269. /**
  270. * Creates a column definition as required by the DBAL from an ORM field mapping definition.
  271. *
  272. * @param ClassMetadata $class The class that owns the field mapping.
  273. * @param array $mapping The field mapping.
  274. * @param Table $table
  275. * @return array The portable column definition as required by the DBAL.
  276. */
  277. private function _gatherColumn($class, array $mapping, $table)
  278. {
  279. $columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
  280. $columnType = $mapping['type'];
  281. $options = array();
  282. $options['length'] = isset($mapping['length']) ? $mapping['length'] : null;
  283. $options['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true;
  284. if ($class->isInheritanceTypeSingleTable() && count($class->parentClasses) > 0) {
  285. $options['notnull'] = false;
  286. }
  287. $options['platformOptions'] = array();
  288. $options['platformOptions']['version'] = $class->isVersioned && $class->versionField == $mapping['fieldName'] ? true : false;
  289. if(strtolower($columnType) == 'string' && $options['length'] === null) {
  290. $options['length'] = 255;
  291. }
  292. if (isset($mapping['precision'])) {
  293. $options['precision'] = $mapping['precision'];
  294. }
  295. if (isset($mapping['scale'])) {
  296. $options['scale'] = $mapping['scale'];
  297. }
  298. if (isset($mapping['default'])) {
  299. $options['default'] = $mapping['default'];
  300. }
  301. if (isset($mapping['columnDefinition'])) {
  302. $options['columnDefinition'] = $mapping['columnDefinition'];
  303. }
  304. if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() == array($mapping['fieldName'])) {
  305. $options['autoincrement'] = true;
  306. }
  307. if ($class->isInheritanceTypeJoined() && $class->name != $class->rootEntityName) {
  308. $options['autoincrement'] = false;
  309. }
  310. if ($table->hasColumn($columnName)) {
  311. // required in some inheritance scenarios
  312. $table->changeColumn($columnName, $options);
  313. } else {
  314. $table->addColumn($columnName, $columnType, $options);
  315. }
  316. $isUnique = isset($mapping['unique']) ? $mapping['unique'] : false;
  317. if ($isUnique) {
  318. $table->addUniqueIndex(array($columnName));
  319. }
  320. }
  321. /**
  322. * Gathers the SQL for properly setting up the relations of the given class.
  323. * This includes the SQL for foreign key constraints and join tables.
  324. *
  325. * @param ClassMetadata $class
  326. * @param \Doctrine\DBAL\Schema\Table $table
  327. * @param \Doctrine\DBAL\Schema\Schema $schema
  328. * @return void
  329. */
  330. private function _gatherRelationsSql($class, $table, $schema)
  331. {
  332. foreach ($class->associationMappings as $fieldName => $mapping) {
  333. if (isset($mapping['inherited'])) {
  334. continue;
  335. }
  336. $foreignClass = $this->_em->getClassMetadata($mapping['targetEntity']);
  337. if ($mapping['type'] & ClassMetadata::TO_ONE && $mapping['isOwningSide']) {
  338. $primaryKeyColumns = $uniqueConstraints = array(); // PK is unnecessary for this relation-type
  339. $this->_gatherRelationJoinColumns($mapping['joinColumns'], $table, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
  340. foreach($uniqueConstraints AS $indexName => $unique) {
  341. $table->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
  342. }
  343. } else if ($mapping['type'] == ClassMetadata::ONE_TO_MANY && $mapping['isOwningSide']) {
  344. //... create join table, one-many through join table supported later
  345. throw ORMException::notSupported();
  346. } else if ($mapping['type'] == ClassMetadata::MANY_TO_MANY && $mapping['isOwningSide']) {
  347. // create join table
  348. $joinTable = $mapping['joinTable'];
  349. $theJoinTable = $schema->createTable($foreignClass->getQuotedJoinTableName($mapping, $this->_platform));
  350. $primaryKeyColumns = $uniqueConstraints = array();
  351. // Build first FK constraint (relation table => source table)
  352. $this->_gatherRelationJoinColumns($joinTable['joinColumns'], $theJoinTable, $class, $mapping, $primaryKeyColumns, $uniqueConstraints);
  353. // Build second FK constraint (relation table => target table)
  354. $this->_gatherRelationJoinColumns($joinTable['inverseJoinColumns'], $theJoinTable, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
  355. $theJoinTable->setPrimaryKey($primaryKeyColumns);
  356. foreach($uniqueConstraints AS $indexName => $unique) {
  357. $theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
  358. }
  359. }
  360. }
  361. }
  362. /**
  363. * Get the class metadata that is responsible for the definition of the referenced column name.
  364. *
  365. * Previously this was a simple task, but with DDC-117 this problem is actually recursive. If its
  366. * not a simple field, go through all identifier field names that are associations recursivly and
  367. * find that referenced column name.
  368. *
  369. * TODO: Is there any way to make this code more pleasing?
  370. *
  371. * @param ClassMetadata $class
  372. * @param string $referencedColumnName
  373. * @return array(ClassMetadata, referencedFieldName)
  374. */
  375. private function getDefiningClass($class, $referencedColumnName)
  376. {
  377. $referencedFieldName = $class->getFieldName($referencedColumnName);
  378. if ($class->hasField($referencedFieldName)) {
  379. return array($class, $referencedFieldName);
  380. } else if (in_array($referencedColumnName, $class->getIdentifierColumnNames())) {
  381. // it seems to be an entity as foreign key
  382. foreach ($class->getIdentifierFieldNames() AS $fieldName) {
  383. if ($class->hasAssociation($fieldName) && $class->getSingleAssociationJoinColumnName($fieldName) == $referencedColumnName) {
  384. return $this->getDefiningClass(
  385. $this->_em->getClassMetadata($class->associationMappings[$fieldName]['targetEntity']),
  386. $class->getSingleAssociationReferencedJoinColumnName($fieldName)
  387. );
  388. }
  389. }
  390. }
  391. return null;
  392. }
  393. /**
  394. * Gather columns and fk constraints that are required for one part of relationship.
  395. *
  396. * @param array $joinColumns
  397. * @param \Doctrine\DBAL\Schema\Table $theJoinTable
  398. * @param ClassMetadata $class
  399. * @param array $mapping
  400. * @param array $primaryKeyColumns
  401. * @param array $uniqueConstraints
  402. */
  403. private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints)
  404. {
  405. $localColumns = array();
  406. $foreignColumns = array();
  407. $fkOptions = array();
  408. $foreignTableName = $class->getQuotedTableName($this->_platform);
  409. foreach ($joinColumns as $joinColumn) {
  410. $columnName = $joinColumn['name'];
  411. list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $joinColumn['referencedColumnName']);
  412. if (!$definingClass) {
  413. throw new \Doctrine\ORM\ORMException(
  414. "Column name `".$joinColumn['referencedColumnName']."` referenced for relation from ".
  415. $mapping['sourceEntity'] . " towards ". $mapping['targetEntity'] . " does not exist."
  416. );
  417. }
  418. $primaryKeyColumns[] = $columnName;
  419. $localColumns[] = $columnName;
  420. $foreignColumns[] = $joinColumn['referencedColumnName'];
  421. if ( ! $theJoinTable->hasColumn($joinColumn['name'])) {
  422. // Only add the column to the table if it does not exist already.
  423. // It might exist already if the foreign key is mapped into a regular
  424. // property as well.
  425. $fieldMapping = $definingClass->getFieldMapping($referencedFieldName);
  426. $columnDef = null;
  427. if (isset($joinColumn['columnDefinition'])) {
  428. $columnDef = $joinColumn['columnDefinition'];
  429. } else if (isset($fieldMapping['columnDefinition'])) {
  430. $columnDef = $fieldMapping['columnDefinition'];
  431. }
  432. $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef);
  433. if (isset($joinColumn['nullable'])) {
  434. $columnOptions['notnull'] = !$joinColumn['nullable'];
  435. }
  436. if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) {
  437. $columnOptions['length'] = $fieldMapping['length'];
  438. } else if ($fieldMapping['type'] == "decimal") {
  439. $columnOptions['scale'] = $fieldMapping['scale'];
  440. $columnOptions['precision'] = $fieldMapping['precision'];
  441. }
  442. $theJoinTable->addColumn($columnName, $fieldMapping['type'], $columnOptions);
  443. }
  444. if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
  445. $uniqueConstraints[] = array('columns' => array($columnName));
  446. }
  447. if (isset($joinColumn['onUpdate'])) {
  448. $fkOptions['onUpdate'] = $joinColumn['onUpdate'];
  449. }
  450. if (isset($joinColumn['onDelete'])) {
  451. $fkOptions['onDelete'] = $joinColumn['onDelete'];
  452. }
  453. }
  454. $theJoinTable->addUnnamedForeignKeyConstraint(
  455. $foreignTableName, $localColumns, $foreignColumns, $fkOptions
  456. );
  457. }
  458. /**
  459. * Drops the database schema for the given classes.
  460. *
  461. * In any way when an exception is thrown it is supressed since drop was
  462. * issued for all classes of the schema and some probably just don't exist.
  463. *
  464. * @param array $classes
  465. * @return void
  466. */
  467. public function dropSchema(array $classes)
  468. {
  469. $dropSchemaSql = $this->getDropSchemaSQL($classes);
  470. $conn = $this->_em->getConnection();
  471. foreach ($dropSchemaSql as $sql) {
  472. try {
  473. $conn->executeQuery($sql);
  474. } catch(\Exception $e) {
  475. }
  476. }
  477. }
  478. /**
  479. * Drops all elements in the database of the current connection.
  480. *
  481. * @return void
  482. */
  483. public function dropDatabase()
  484. {
  485. $dropSchemaSql = $this->getDropDatabaseSQL();
  486. $conn = $this->_em->getConnection();
  487. foreach ($dropSchemaSql as $sql) {
  488. $conn->executeQuery($sql);
  489. }
  490. }
  491. /**
  492. * Gets the SQL needed to drop the database schema for the connections database.
  493. *
  494. * @return array
  495. */
  496. public function getDropDatabaseSQL()
  497. {
  498. $sm = $this->_em->getConnection()->getSchemaManager();
  499. $schema = $sm->createSchema();
  500. $visitor = new \Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector($this->_platform);
  501. /* @var $schema \Doctrine\DBAL\Schema\Schema */
  502. $schema->visit($visitor);
  503. return $visitor->getQueries();
  504. }
  505. /**
  506. * Get SQL to drop the tables defined by the passed classes.
  507. *
  508. * @param array $classes
  509. * @return array
  510. */
  511. public function getDropSchemaSQL(array $classes)
  512. {
  513. $visitor = new \Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector($this->_platform);
  514. $schema = $this->getSchemaFromMetadata($classes);
  515. $sm = $this->_em->getConnection()->getSchemaManager();
  516. $fullSchema = $sm->createSchema();
  517. foreach ($fullSchema->getTables() AS $table) {
  518. if (!$schema->hasTable($table->getName())) {
  519. foreach ($table->getForeignKeys() AS $foreignKey) {
  520. /* @var $foreignKey \Doctrine\DBAL\Schema\ForeignKeyConstraint */
  521. if ($schema->hasTable($foreignKey->getForeignTableName())) {
  522. $visitor->acceptForeignKey($table, $foreignKey);
  523. }
  524. }
  525. } else {
  526. $visitor->acceptTable($table);
  527. foreach ($table->getForeignKeys() AS $foreignKey) {
  528. $visitor->acceptForeignKey($table, $foreignKey);
  529. }
  530. }
  531. }
  532. if ($this->_platform->supportsSequences()) {
  533. foreach ($schema->getSequences() AS $sequence) {
  534. $visitor->acceptSequence($sequence);
  535. }
  536. foreach ($schema->getTables() AS $table) {
  537. /* @var $sequence Table */
  538. if ($table->hasPrimaryKey()) {
  539. $columns = $table->getPrimaryKey()->getColumns();
  540. if (count($columns) == 1) {
  541. $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
  542. if ($fullSchema->hasSequence($checkSequence)) {
  543. $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
  544. }
  545. }
  546. }
  547. }
  548. }
  549. return $visitor->getQueries();
  550. }
  551. /**
  552. * Updates the database schema of the given classes by comparing the ClassMetadata
  553. * instances to the current database schema that is inspected. If $saveMode is set
  554. * to true the command is executed in the Database, else SQL is returned.
  555. *
  556. * @param array $classes
  557. * @param boolean $saveMode
  558. * @return void
  559. */
  560. public function updateSchema(array $classes, $saveMode=false)
  561. {
  562. $updateSchemaSql = $this->getUpdateSchemaSql($classes, $saveMode);
  563. $conn = $this->_em->getConnection();
  564. foreach ($updateSchemaSql as $sql) {
  565. $conn->executeQuery($sql);
  566. }
  567. }
  568. /**
  569. * Gets the sequence of SQL statements that need to be performed in order
  570. * to bring the given class mappings in-synch with the relational schema.
  571. * If $saveMode is set to true the command is executed in the Database,
  572. * else SQL is returned.
  573. *
  574. * @param array $classes The classes to consider.
  575. * @param boolean $saveMode True for writing to DB, false for SQL string
  576. * @return array The sequence of SQL statements.
  577. */
  578. public function getUpdateSchemaSql(array $classes, $saveMode=false)
  579. {
  580. $sm = $this->_em->getConnection()->getSchemaManager();
  581. $fromSchema = $sm->createSchema();
  582. $toSchema = $this->getSchemaFromMetadata($classes);
  583. $comparator = new \Doctrine\DBAL\Schema\Comparator();
  584. $schemaDiff = $comparator->compare($fromSchema, $toSchema);
  585. if ($saveMode) {
  586. return $schemaDiff->toSaveSql($this->_platform);
  587. } else {
  588. return $schemaDiff->toSql($this->_platform);
  589. }
  590. }
  591. }