SingleDatabaseSynchronizer.php 4.93 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

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
26
use function count;
27 28

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

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

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

Benjamin Morel's avatar
Benjamin Morel committed
57

58
    /**
Benjamin Morel's avatar
Benjamin Morel committed
59
     * {@inheritdoc}
60 61 62 63 64 65 66 67 68 69 70 71
     */
    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);
        }
72 73

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
77
     * {@inheritdoc}
78 79 80 81 82 83 84 85 86
     */
    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
87
            if ($dropSchema->hasTable($table->getName())) {
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 131
                $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
132
     * {@inheritdoc}
133 134 135 136
     */
    public function getDropAllSchema()
    {
        $sm      = $this->conn->getSchemaManager();
137
        $visitor = new DropSchemaSqlCollector($this->platform);
138 139 140 141 142 143 144 145 146

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

        return $visitor->getQueries();
    }

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

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

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

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