SchemaDiff.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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;
  20. use \Doctrine\DBAL\Platforms\AbstractPlatform;
  21. /**
  22. * Schema Diff.
  23. *
  24. * @link www.doctrine-project.org
  25. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  26. * @license http://ez.no/licenses/new_bsd New BSD License
  27. * @since 2.0
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. */
  30. class SchemaDiff
  31. {
  32. /**
  33. * @var \Doctrine\DBAL\Schema\Schema
  34. */
  35. public $fromSchema;
  36. /**
  37. * All added tables.
  38. *
  39. * @var \Doctrine\DBAL\Schema\Table[]
  40. */
  41. public $newTables = array();
  42. /**
  43. * All changed tables.
  44. *
  45. * @var \Doctrine\DBAL\Schema\TableDiff[]
  46. */
  47. public $changedTables = array();
  48. /**
  49. * All removed tables.
  50. *
  51. * @var \Doctrine\DBAL\Schema\Table[]
  52. */
  53. public $removedTables = array();
  54. /**
  55. * @var \Doctrine\DBAL\Schema\Sequence[]
  56. */
  57. public $newSequences = array();
  58. /**
  59. * @var \Doctrine\DBAL\Schema\Sequence[]
  60. */
  61. public $changedSequences = array();
  62. /**
  63. * @var \Doctrine\DBAL\Schema\Sequence[]
  64. */
  65. public $removedSequences = array();
  66. /**
  67. * @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
  68. */
  69. public $orphanedForeignKeys = array();
  70. /**
  71. * Constructs an SchemaDiff object.
  72. *
  73. * @param \Doctrine\DBAL\Schema\Table[] $newTables
  74. * @param \Doctrine\DBAL\Schema\TableDiff[] $changedTables
  75. * @param \Doctrine\DBAL\Schema\Table[] $removedTables
  76. * @param \Doctrine\DBAL\Schema\Schema|null $fromSchema
  77. */
  78. public function __construct($newTables = array(), $changedTables = array(), $removedTables = array(), Schema $fromSchema = null)
  79. {
  80. $this->newTables = $newTables;
  81. $this->changedTables = $changedTables;
  82. $this->removedTables = $removedTables;
  83. $this->fromSchema = $fromSchema;
  84. }
  85. /**
  86. * The to save sql mode ensures that the following things don't happen:
  87. *
  88. * 1. Tables are deleted
  89. * 2. Sequences are deleted
  90. * 3. Foreign Keys which reference tables that would otherwise be deleted.
  91. *
  92. * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
  93. *
  94. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  95. *
  96. * @return array
  97. */
  98. public function toSaveSql(AbstractPlatform $platform)
  99. {
  100. return $this->_toSql($platform, true);
  101. }
  102. /**
  103. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  104. *
  105. * @return array
  106. */
  107. public function toSql(AbstractPlatform $platform)
  108. {
  109. return $this->_toSql($platform, false);
  110. }
  111. /**
  112. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  113. * @param boolean $saveMode
  114. *
  115. * @return array
  116. */
  117. protected function _toSql(AbstractPlatform $platform, $saveMode = false)
  118. {
  119. $sql = array();
  120. if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
  121. foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
  122. $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName());
  123. }
  124. }
  125. if ($platform->supportsSequences() == true) {
  126. foreach ($this->changedSequences as $sequence) {
  127. $sql[] = $platform->getAlterSequenceSQL($sequence);
  128. }
  129. if ($saveMode === false) {
  130. foreach ($this->removedSequences as $sequence) {
  131. $sql[] = $platform->getDropSequenceSQL($sequence);
  132. }
  133. }
  134. foreach ($this->newSequences as $sequence) {
  135. $sql[] = $platform->getCreateSequenceSQL($sequence);
  136. }
  137. }
  138. $foreignKeySql = array();
  139. foreach ($this->newTables as $table) {
  140. $sql = array_merge(
  141. $sql,
  142. $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
  143. );
  144. if ($platform->supportsForeignKeyConstraints()) {
  145. foreach ($table->getForeignKeys() as $foreignKey) {
  146. $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
  147. }
  148. }
  149. }
  150. $sql = array_merge($sql, $foreignKeySql);
  151. if ($saveMode === false) {
  152. foreach ($this->removedTables as $table) {
  153. $sql[] = $platform->getDropTableSQL($table);
  154. }
  155. }
  156. foreach ($this->changedTables as $tableDiff) {
  157. $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
  158. }
  159. return $sql;
  160. }
  161. }