SingleDatabaseSynchronizer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  36. * @param \Doctrine\DBAL\Connection $conn
  37. */
  38. public function __construct(Connection $conn)
  39. {
  40. parent::__construct($conn);
  41. $this->platform = $conn->getDatabasePlatform();
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getCreateSchema(Schema $createSchema)
  47. {
  48. return $createSchema->toSql($this->platform);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getUpdateSchema(Schema $toSchema, $noDrops = false)
  54. {
  55. $comparator = new Comparator();
  56. $sm = $this->conn->getSchemaManager();
  57. $fromSchema = $sm->createSchema();
  58. $schemaDiff = $comparator->compare($fromSchema, $toSchema);
  59. if ($noDrops) {
  60. return $schemaDiff->toSaveSql($this->platform);
  61. }
  62. return $schemaDiff->toSql($this->platform);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getDropSchema(Schema $dropSchema)
  68. {
  69. $visitor = new DropSchemaSqlCollector($this->platform);
  70. $sm = $this->conn->getSchemaManager();
  71. $fullSchema = $sm->createSchema();
  72. foreach ($fullSchema->getTables() as $table) {
  73. if ( $dropSchema->hasTable($table->getName())) {
  74. $visitor->acceptTable($table);
  75. }
  76. foreach ($table->getForeignKeys() as $foreignKey) {
  77. if ( ! $dropSchema->hasTable($table->getName())) {
  78. continue;
  79. }
  80. if ( ! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
  81. continue;
  82. }
  83. $visitor->acceptForeignKey($table, $foreignKey);
  84. }
  85. }
  86. if ( ! $this->platform->supportsSequences()) {
  87. return $visitor->getQueries();
  88. }
  89. foreach ($dropSchema->getSequences() as $sequence) {
  90. $visitor->acceptSequence($sequence);
  91. }
  92. foreach ($dropSchema->getTables() as $table) {
  93. if ( ! $table->hasPrimaryKey()) {
  94. continue;
  95. }
  96. $columns = $table->getPrimaryKey()->getColumns();
  97. if (count($columns) > 1) {
  98. continue;
  99. }
  100. $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
  101. if ($fullSchema->hasSequence($checkSequence)) {
  102. $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
  103. }
  104. }
  105. return $visitor->getQueries();
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getDropAllSchema()
  111. {
  112. $sm = $this->conn->getSchemaManager();
  113. $visitor = new DropSchemaSqlCollector($this->platform);
  114. /* @var $schema \Doctrine\DBAL\Schema\Schema */
  115. $schema = $sm->createSchema();
  116. $schema->visit($visitor);
  117. return $visitor->getQueries();
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function createSchema(Schema $createSchema)
  123. {
  124. $this->processSql($this->getCreateSchema($createSchema));
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function updateSchema(Schema $toSchema, $noDrops = false)
  130. {
  131. $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function dropSchema(Schema $dropSchema)
  137. {
  138. $this->processSqlSafely($this->getDropSchema($dropSchema));
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function dropAllSchema()
  144. {
  145. $this->processSql($this->getDropAllSchema());
  146. }
  147. }