SingleDatabaseSynchronizer.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema\Synchronizer;
  20. use Doctrine\DBAL\Connection;
  21. use Doctrine\DBAL\Schema\Schema;
  22. use Doctrine\DBAL\Schema\Comparator;
  23. use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
  24. /**
  25. * Schema Synchronizer for Default DBAL Connection
  26. *
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. */
  29. class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
  30. {
  31. /**
  32. * @var Doctrine\DBAL\Platforms\AbstractPlatform
  33. */
  34. private $platform;
  35. public function __construct(Connection $conn)
  36. {
  37. parent::__construct($conn);
  38. $this->platform = $conn->getDatabasePlatform();
  39. }
  40. /**
  41. * Get the SQL statements that can be executed to create the schema.
  42. *
  43. * @param Schema $createSchema
  44. * @return array
  45. */
  46. public function getCreateSchema(Schema $createSchema)
  47. {
  48. return $createSchema->toSql($this->platform);
  49. }
  50. /**
  51. * Get the SQL Statements to update given schema with the underlying db.
  52. *
  53. * @param Schema $toSchema
  54. * @param bool $noDrops
  55. * @return array
  56. */
  57. public function getUpdateSchema(Schema $toSchema, $noDrops = false)
  58. {
  59. $comparator = new Comparator();
  60. $sm = $this->conn->getSchemaManager();
  61. $fromSchema = $sm->createSchema();
  62. $schemaDiff = $comparator->compare($fromSchema, $toSchema);
  63. if ($noDrops) {
  64. return $schemaDiff->toSaveSql($this->platform);
  65. }
  66. return $schemaDiff->toSql($this->platform);
  67. }
  68. /**
  69. * Get the SQL Statements to drop the given schema from underlying db.
  70. *
  71. * @param Schema $dropSchema
  72. * @return array
  73. */
  74. public function getDropSchema(Schema $dropSchema)
  75. {
  76. $visitor = new DropSchemaSqlCollector($this->platform);
  77. $sm = $this->conn->getSchemaManager();
  78. $fullSchema = $sm->createSchema();
  79. foreach ($fullSchema->getTables() as $table) {
  80. if ( $dropSchema->hasTable($table->getName())) {
  81. $visitor->acceptTable($table);
  82. }
  83. foreach ($table->getForeignKeys() as $foreignKey) {
  84. if ( ! $dropSchema->hasTable($table->getName())) {
  85. continue;
  86. }
  87. if ( ! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
  88. continue;
  89. }
  90. $visitor->acceptForeignKey($table, $foreignKey);
  91. }
  92. }
  93. if ( ! $this->platform->supportsSequences()) {
  94. return $visitor->getQueries();
  95. }
  96. foreach ($dropSchema->getSequences() as $sequence) {
  97. $visitor->acceptSequence($sequence);
  98. }
  99. foreach ($dropSchema->getTables() as $table) {
  100. /* @var $sequence Table */
  101. if ( ! $table->hasPrimaryKey()) {
  102. continue;
  103. }
  104. $columns = $table->getPrimaryKey()->getColumns();
  105. if (count($columns) > 1) {
  106. continue;
  107. }
  108. $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
  109. if ($fullSchema->hasSequence($checkSequence)) {
  110. $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
  111. }
  112. }
  113. return $visitor->getQueries();
  114. }
  115. /**
  116. * Get the SQL statements to drop all schema assets from underlying db.
  117. *
  118. * @return array
  119. */
  120. public function getDropAllSchema()
  121. {
  122. $sm = $this->conn->getSchemaManager();
  123. $visitor = new DropSchemaSqlCollector($this->platform);
  124. /* @var $schema \Doctrine\DBAL\Schema\Schema */
  125. $schema = $sm->createSchema();
  126. $schema->visit($visitor);
  127. return $visitor->getQueries();
  128. }
  129. /**
  130. * Create the Schema
  131. *
  132. * @param Schema $createSchema
  133. * @return void
  134. */
  135. public function createSchema(Schema $createSchema)
  136. {
  137. $this->processSql($this->getCreateSchema($createSchema));
  138. }
  139. /**
  140. * Update the Schema to new schema version.
  141. *
  142. * @param Schema $toSchema
  143. * @param bool $noDrops
  144. * @return void
  145. */
  146. public function updateSchema(Schema $toSchema, $noDrops = false)
  147. {
  148. $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
  149. }
  150. /**
  151. * Drop the given database schema from the underlying db.
  152. *
  153. * @param Schema $dropSchema
  154. * @return void
  155. */
  156. public function dropSchema(Schema $dropSchema)
  157. {
  158. $this->processSqlSafely($this->getDropSchema($dropSchema));
  159. }
  160. /**
  161. * Drop all assets from the underyling db.
  162. *
  163. * @return void
  164. */
  165. public function dropAllSchema()
  166. {
  167. $this->processSql($this->getDropAllSchema());
  168. }
  169. }