SingleDatabaseSynchronizer.php 3.86 KB
Newer Older
1
<?php
Benjamin Morel's avatar
Benjamin Morel committed
2

3
namespace Doctrine\DBAL\Schema\Synchronizer;
4 5

use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Schema\Comparator;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
10
use function count;
11 12

/**
Benjamin Morel's avatar
Benjamin Morel committed
13
 * Schema Synchronizer for Default DBAL Connection.
14
 */
15
class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
16
{
17
    /** @var AbstractPlatform */
18 19 20 21
    private $platform;

    public function __construct(Connection $conn)
    {
22
        parent::__construct($conn);
23 24 25 26
        $this->platform = $conn->getDatabasePlatform();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
27
     * {@inheritdoc}
28 29 30 31 32 33
     */
    public function getCreateSchema(Schema $createSchema)
    {
        return $createSchema->toSql($this->platform);
    }

Benjamin Morel's avatar
Benjamin Morel committed
34

35
    /**
Benjamin Morel's avatar
Benjamin Morel committed
36
     * {@inheritdoc}
37 38 39 40 41 42 43 44 45 46 47 48
     */
    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);
        }
49 50

        return $schemaDiff->toSql($this->platform);
51 52 53
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
54
     * {@inheritdoc}
55 56 57
     */
    public function getDropSchema(Schema $dropSchema)
    {
58 59
        $visitor = new DropSchemaSqlCollector($this->platform);
        $sm      = $this->conn->getSchemaManager();
60 61 62 63

        $fullSchema = $sm->createSchema();

        foreach ($fullSchema->getTables() as $table) {
Steve Müller's avatar
Steve Müller committed
64
            if ($dropSchema->hasTable($table->getName())) {
65 66 67 68
                $visitor->acceptTable($table);
            }

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

73
                if (! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
74 75 76 77 78 79 80
                    continue;
                }

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

81
        if (! $this->platform->supportsSequences()) {
82 83 84 85 86 87 88 89
            return $visitor->getQueries();
        }

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

        foreach ($dropSchema->getTables() as $table) {
90
            if (! $table->hasPrimaryKey()) {
91 92 93 94 95 96 97 98
                continue;
            }

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

99 100 101
            $checkSequence = $table->getName() . '_' . $columns[0] . '_seq';
            if (! $fullSchema->hasSequence($checkSequence)) {
                continue;
102
            }
103 104

            $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
105 106 107 108 109 110
        }

        return $visitor->getQueries();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
111
     * {@inheritdoc}
112 113 114 115
     */
    public function getDropAllSchema()
    {
        $sm      = $this->conn->getSchemaManager();
116
        $visitor = new DropSchemaSqlCollector($this->platform);
117

118 119
        /** @var Schema $schema */
        $schema = $sm->createSchema();
120 121 122 123 124 125
        $schema->visit($visitor);

        return $visitor->getQueries();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
126
     * {@inheritdoc}
127 128 129 130 131 132 133
     */
    public function createSchema(Schema $createSchema)
    {
        $this->processSql($this->getCreateSchema($createSchema));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
134
     * {@inheritdoc}
135 136 137 138 139 140 141
     */
    public function updateSchema(Schema $toSchema, $noDrops = false)
    {
        $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
142
     * {@inheritdoc}
143 144 145 146 147 148 149
     */
    public function dropSchema(Schema $dropSchema)
    {
        $this->processSqlSafely($this->getDropSchema($dropSchema));
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
150
     * {@inheritdoc}
151 152 153 154 155 156
     */
    public function dropAllSchema()
    {
        $this->processSql($this->getDropAllSchema());
    }
}