SchemaDiff.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 namespaces.
  38. *
  39. * @var string[]
  40. */
  41. public $newNamespaces = array();
  42. /**
  43. * All removed namespaces.
  44. *
  45. * @var string[]
  46. */
  47. public $removedNamespaces = array();
  48. /**
  49. * All added tables.
  50. *
  51. * @var \Doctrine\DBAL\Schema\Table[]
  52. */
  53. public $newTables = array();
  54. /**
  55. * All changed tables.
  56. *
  57. * @var \Doctrine\DBAL\Schema\TableDiff[]
  58. */
  59. public $changedTables = array();
  60. /**
  61. * All removed tables.
  62. *
  63. * @var \Doctrine\DBAL\Schema\Table[]
  64. */
  65. public $removedTables = array();
  66. /**
  67. * @var \Doctrine\DBAL\Schema\Sequence[]
  68. */
  69. public $newSequences = array();
  70. /**
  71. * @var \Doctrine\DBAL\Schema\Sequence[]
  72. */
  73. public $changedSequences = array();
  74. /**
  75. * @var \Doctrine\DBAL\Schema\Sequence[]
  76. */
  77. public $removedSequences = array();
  78. /**
  79. * @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
  80. */
  81. public $orphanedForeignKeys = array();
  82. /**
  83. * Constructs an SchemaDiff object.
  84. *
  85. * @param \Doctrine\DBAL\Schema\Table[] $newTables
  86. * @param \Doctrine\DBAL\Schema\TableDiff[] $changedTables
  87. * @param \Doctrine\DBAL\Schema\Table[] $removedTables
  88. * @param \Doctrine\DBAL\Schema\Schema|null $fromSchema
  89. */
  90. public function __construct($newTables = array(), $changedTables = array(), $removedTables = array(), Schema $fromSchema = null)
  91. {
  92. $this->newTables = $newTables;
  93. $this->changedTables = $changedTables;
  94. $this->removedTables = $removedTables;
  95. $this->fromSchema = $fromSchema;
  96. }
  97. /**
  98. * The to save sql mode ensures that the following things don't happen:
  99. *
  100. * 1. Tables are deleted
  101. * 2. Sequences are deleted
  102. * 3. Foreign Keys which reference tables that would otherwise be deleted.
  103. *
  104. * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
  105. *
  106. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  107. *
  108. * @return array
  109. */
  110. public function toSaveSql(AbstractPlatform $platform)
  111. {
  112. return $this->_toSql($platform, true);
  113. }
  114. /**
  115. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  116. *
  117. * @return array
  118. */
  119. public function toSql(AbstractPlatform $platform)
  120. {
  121. return $this->_toSql($platform, false);
  122. }
  123. /**
  124. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  125. * @param boolean $saveMode
  126. *
  127. * @return array
  128. */
  129. protected function _toSql(AbstractPlatform $platform, $saveMode = false)
  130. {
  131. $sql = array();
  132. if ($platform->supportsSchemas()) {
  133. foreach ($this->newNamespaces as $newNamespace) {
  134. $sql[] = $platform->getCreateSchemaSQL($newNamespace);
  135. }
  136. }
  137. if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
  138. foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
  139. $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTable());
  140. }
  141. }
  142. if ($platform->supportsSequences() == true) {
  143. foreach ($this->changedSequences as $sequence) {
  144. $sql[] = $platform->getAlterSequenceSQL($sequence);
  145. }
  146. if ($saveMode === false) {
  147. foreach ($this->removedSequences as $sequence) {
  148. $sql[] = $platform->getDropSequenceSQL($sequence);
  149. }
  150. }
  151. foreach ($this->newSequences as $sequence) {
  152. $sql[] = $platform->getCreateSequenceSQL($sequence);
  153. }
  154. }
  155. $foreignKeySql = array();
  156. foreach ($this->newTables as $table) {
  157. $sql = array_merge(
  158. $sql,
  159. $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
  160. );
  161. if ($platform->supportsForeignKeyConstraints()) {
  162. foreach ($table->getForeignKeys() as $foreignKey) {
  163. $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
  164. }
  165. }
  166. }
  167. $sql = array_merge($sql, $foreignKeySql);
  168. if ($saveMode === false) {
  169. foreach ($this->removedTables as $table) {
  170. $sql[] = $platform->getDropTableSQL($table);
  171. }
  172. }
  173. foreach ($this->changedTables as $tableDiff) {
  174. $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
  175. }
  176. return $sql;
  177. }
  178. }