SchemaDiff.php 5.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Schema;

22 23
use \Doctrine\DBAL\Platforms\AbstractPlatform;

24 25 26
/**
 * Schema Diff
 *
Benjamin Eberlei's avatar
Benjamin Eberlei committed
27
 * 
28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * @link    www.doctrine-project.org
 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 * @since   2.0
 * @version $Revision$
 * @author  Benjamin Eberlei <kontakt@beberlei.de>
 */
class SchemaDiff
{
    /**
     * All added tables
     *
     * @var array(string=>ezcDbSchemaTable)
     */
42
    public $newTables = array();
43 44 45 46 47 48

    /**
     * All changed tables
     *
     * @var array(string=>ezcDbSchemaTableDiff)
     */
49
    public $changedTables = array();
50 51 52 53

    /**
     * All removed tables
     *
54
     * @var array(string=>Table)
55
     */
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    public $removedTables = array();

    /**
     * @var array
     */
    public $newSequences = array();

    /**
     * @var array
     */
    public $changedSequences = array();

    /**
     * @var array
     */
    public $removedSequences = array();
72

73 74 75 76 77
    /**
     * @var array
     */
    public $orphanedForeignKeys = array();

78 79 80 81 82
    /**
     * Constructs an SchemaDiff object.
     *
     * @param array(string=>Table)      $newTables
     * @param array(string=>TableDiff)  $changedTables
Eriksen Costa's avatar
Eriksen Costa committed
83
     * @param array(string=>bool)       $removedTables
84
     */
Eriksen Costa's avatar
Eriksen Costa committed
85
    public function __construct($newTables = array(), $changedTables = array(), $removedTables = array())
86 87 88 89 90
    {
        $this->newTables = $newTables;
        $this->changedTables = $changedTables;
        $this->removedTables = $removedTables;
    }
91 92

    /**
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
     * The to save sql mode ensures that the following things don't happen:
     *
     * 1. Tables are deleted
     * 2. Sequences are deleted
     * 3. Foreign Keys which reference tables that would otherwise be deleted.
     *
     * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
     *
     * @param AbstractPlatform $platform
     * @return array
     */
    public function toSaveSql(AbstractPlatform $platform)
    {
        return $this->_toSql($platform, true);
    }

    /**
110 111 112 113
     * @param AbstractPlatform $platform
     * @return array
     */
    public function toSql(AbstractPlatform $platform)
114 115 116 117 118 119 120 121 122 123
    {
        return $this->_toSql($platform, false);
    }

    /**
     * @param AbstractPlatform $platform
     * @param bool $saveMode
     * @return array
     */
    protected function _toSql(AbstractPlatform $platform, $saveMode = false)
124 125 126
    {
        $sql = array();

127
        if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
128
            foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
129
                $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName());
130 131 132
            }
        }

133
        if ($platform->supportsSequences() == true) {
134
            foreach ($this->changedSequences as $sequence) {
135
                $sql[] = $platform->getAlterSequenceSQL($sequence);
136 137
            }

138
            if ($saveMode === false) {
139
                foreach ($this->removedSequences as $sequence) {
140
                    $sql[] = $platform->getDropSequenceSQL($sequence);
141
                }
142 143
            }

144
            foreach ($this->newSequences as $sequence) {
145
                $sql[] = $platform->getCreateSequenceSQL($sequence);
146 147 148
            }
        }

149
        $foreignKeySql = array();
150
        foreach ($this->newTables as $table) {
151 152
            $sql = array_merge(
                $sql,
153
                $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
154
            );
155 156

            if ($platform->supportsForeignKeyConstraints()) {
157
                foreach ($table->getForeignKeys() as $foreignKey) {
158 159
                    $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
                }
160
            }
161
        }
162
        $sql = array_merge($sql, $foreignKeySql);
163

164
        if ($saveMode === false) {
165
            foreach ($this->removedTables as $table) {
166
                $sql[] = $platform->getDropTableSQL($table);
167
            }
168 169
        }

170
        foreach ($this->changedTables as $tableDiff) {
171
            $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
172 173 174 175
        }

        return $sql;
    }
176
}