SchemaTool.php 27 KB

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