TableDiff.php 3.12 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema;

5 6
use Doctrine\DBAL\Platforms\AbstractPlatform;

7
/**
Benjamin Morel's avatar
Benjamin Morel committed
8
 * Table Diff.
9 10 11
 */
class TableDiff
{
12
    /** @var string */
13 14
    public $name = null;

Sergei Morozov's avatar
Sergei Morozov committed
15
    /** @var string|false */
16 17
    public $newName = false;

18
    /**
Benjamin Morel's avatar
Benjamin Morel committed
19
     * All added fields.
20
     *
21
     * @var Column[]
22
     */
23
    public $addedColumns;
24 25

    /**
Benjamin Morel's avatar
Benjamin Morel committed
26
     * All changed fields.
27
     *
28
     * @var ColumnDiff[]
29
     */
30
    public $changedColumns = [];
31 32

    /**
Benjamin Morel's avatar
Benjamin Morel committed
33
     * All removed fields.
34
     *
35
     * @var Column[]
36
     */
37
    public $removedColumns = [];
38

39 40 41
    /**
     * Columns that are only renamed from key to column instance name.
     *
42
     * @var Column[]
43
     */
44
    public $renamedColumns = [];
45

46
    /**
Benjamin Morel's avatar
Benjamin Morel committed
47
     * All added indexes.
48
     *
49
     * @var Index[]
50
     */
51
    public $addedIndexes = [];
52 53

    /**
Benjamin Morel's avatar
Benjamin Morel committed
54
     * All changed indexes.
55
     *
56
     * @var Index[]
57
     */
58
    public $changedIndexes = [];
59 60 61 62

    /**
     * All removed indexes
     *
63
     * @var Index[]
64
     */
65
    public $removedIndexes = [];
66 67 68 69

    /**
     * Indexes that are only renamed but are identical otherwise.
     *
70
     * @var Index[]
71
     */
72
    public $renamedIndexes = [];
73 74 75

    /**
     * All added foreign key definitions
76
     *
77
     * @var ForeignKeyConstraint[]
78
     */
79
    public $addedForeignKeys = [];
80 81 82 83

    /**
     * All changed foreign keys
     *
84
     * @var ForeignKeyConstraint[]
85
     */
86
    public $changedForeignKeys = [];
87 88 89 90

    /**
     * All removed foreign keys
     *
91
     * @var ForeignKeyConstraint[]|string[]
92
     */
93
    public $removedForeignKeys = [];
94

Sergei Morozov's avatar
Sergei Morozov committed
95
    /** @var Table|null */
96 97
    public $fromTable;

98 99 100
    /**
     * Constructs an TableDiff object.
     *
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
     * @param string       $tableName
     * @param Column[]     $addedColumns
     * @param ColumnDiff[] $changedColumns
     * @param Column[]     $removedColumns
     * @param Index[]      $addedIndexes
     * @param Index[]      $changedIndexes
     * @param Index[]      $removedIndexes
     */
    public function __construct(
        $tableName,
        $addedColumns = [],
        $changedColumns = [],
        $removedColumns = [],
        $addedIndexes = [],
        $changedIndexes = [],
        $removedIndexes = [],
        ?Table $fromTable = null
    ) {
        $this->name           = $tableName;
        $this->addedColumns   = $addedColumns;
121 122
        $this->changedColumns = $changedColumns;
        $this->removedColumns = $removedColumns;
123
        $this->addedIndexes   = $addedIndexes;
124 125
        $this->changedIndexes = $changedIndexes;
        $this->removedIndexes = $removedIndexes;
126
        $this->fromTable      = $fromTable;
127
    }
128

129
    /**
130 131
     * @param AbstractPlatform $platform The platform to use for retrieving this table diff's name.
     *
132
     * @return Identifier
133
     */
134
    public function getName(AbstractPlatform $platform)
135
    {
136 137 138
        return new Identifier(
            $this->fromTable instanceof Table ? $this->fromTable->getQuotedName($platform) : $this->name
        );
139 140
    }

141
    /**
Sergei Morozov's avatar
Sergei Morozov committed
142
     * @return Identifier|false
143
     */
144 145
    public function getNewName()
    {
Sergei Morozov's avatar
Sergei Morozov committed
146 147 148 149 150
        if ($this->newName === false) {
            return false;
        }

        return new Identifier($this->newName);
151
    }
152
}