SingleDatabaseSynchronizer.php 4.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?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
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */
Benjamin Morel's avatar
Benjamin Morel committed
19

20
namespace Doctrine\DBAL\Schema\Synchronizer;
21 22 23 24 25 26 27

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;

/**
Benjamin Morel's avatar
Benjamin Morel committed
28
 * Schema Synchronizer for Default DBAL Connection.
29 30 31
 *
 * @author Benjamin Eberlei <kontakt@beberlei.de>
 */
32
class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
33 34
{
    /**
Konstantin Kuklin's avatar
Konstantin Kuklin committed
35
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
36 37 38
     */
    private $platform;

Benjamin Morel's avatar
Benjamin Morel committed
39 40 41
    /**
     * @param \Doctrine\DBAL\Connection $conn
     */
42 43
    public function __construct(Connection $conn)
    {
44
        parent::__construct($conn);
45 46 47 48
        $this->platform = $conn->getDatabasePlatform();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
49
     * {@inheritdoc}
50 51 52 53 54 55
     */
    public function getCreateSchema(Schema $createSchema)
    {
        return $createSchema->toSql($this->platform);
    }

Benjamin Morel's avatar
Benjamin Morel committed
56

57
    /**
Benjamin Morel's avatar
Benjamin Morel committed
58
     * {@inheritdoc}
59 60 61 62 63 64 65 66 67 68 69 70
     */
    public function getUpdateSchema(Schema $toSchema, $noDrops = false)
    {
        $comparator = new Comparator();
        $sm         = $this->conn->getSchemaManager();

        $fromSchema = $sm->createSchema();
        $schemaDiff = $comparator->compare($fromSchema, $toSchema);

        if ($noDrops) {
            return $schemaDiff->toSaveSql($this->platform);
        }
71 72

        return $schemaDiff->toSql($this->platform);
73 74 75
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
76
     * {@inheritdoc}
77 78 79 80 81 82 83 84 85
     */
    public function getDropSchema(Schema $dropSchema)
    {
        $visitor    = new DropSchemaSqlCollector($this->platform);
        $sm         = $this->conn->getSchemaManager();

        $fullSchema = $sm->createSchema();

        foreach ($fullSchema->getTables() as $table) {
Steve Müller's avatar
Steve Müller committed
86
            if ($dropSchema->hasTable($table->getName())) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
                $visitor->acceptTable($table);
            }

            foreach ($table->getForeignKeys() as $foreignKey) {
                if ( ! $dropSchema->hasTable($table->getName())) {
                    continue;
                }

                if ( ! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
                    continue;
                }

                $visitor->acceptForeignKey($table, $foreignKey);
            }
        }

        if ( ! $this->platform->supportsSequences()) {
            return $visitor->getQueries();
        }

        foreach ($dropSchema->getSequences() as $sequence) {
            $visitor->acceptSequence($sequence);
        }

        foreach ($dropSchema->getTables() as $table) {
            if ( ! $table->hasPrimaryKey()) {
                continue;
            }

            $columns = $table->getPrimaryKey()->getColumns();
            if (count($columns) > 1) {
                continue;
            }

            $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
            if ($fullSchema->hasSequence($checkSequence)) {
                $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
            }
        }

        return $visitor->getQueries();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
131
     * {@inheritdoc}
132 133 134 135
     */
    public function getDropAllSchema()
    {
        $sm      = $this->conn->getSchemaManager();
136
        $visitor = new DropSchemaSqlCollector($this->platform);
137 138 139 140 141 142 143 144 145

        /* @var $schema \Doctrine\DBAL\Schema\Schema */
        $schema  = $sm->createSchema();
        $schema->visit($visitor);

        return $visitor->getQueries();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
146
     * {@inheritdoc}
147 148 149 150 151 152 153
     */
    public function createSchema(Schema $createSchema)
    {
        $this->processSql($this->getCreateSchema($createSchema));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
154
     * {@inheritdoc}
155 156 157 158 159 160 161
     */
    public function updateSchema(Schema $toSchema, $noDrops = false)
    {
        $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
162
     * {@inheritdoc}
163 164 165 166 167 168 169
     */
    public function dropSchema(Schema $dropSchema)
    {
        $this->processSqlSafely($this->getDropSchema($dropSchema));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
170
     * {@inheritdoc}
171 172 173 174 175 176
     */
    public function dropAllSchema()
    {
        $this->processSql($this->getDropAllSchema());
    }
}