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