|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MspPack\DDSGenerate\DatabaseUpdate; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Schema\Column; |
| 6 | +use Doctrine\DBAL\Schema\SchemaException; |
| 7 | +use Doctrine\DBAL\Schema\TableDiff; |
| 8 | +use MspPack\DDSGenerate\DatabaseUpdate\Schema\SchemaManager; |
| 9 | +use MspPack\DDSGenerate\DatabaseUpdate\Schema\Table; |
| 10 | +use MspPack\DDSGenerate\DatabaseUpdate\Types\Type; |
| 11 | + |
| 12 | +class DatabaseUpdater |
| 13 | +{ |
| 14 | + protected $tableArr; |
| 15 | + protected $table; |
| 16 | + protected $originalTable; |
| 17 | + |
| 18 | + public function __construct(array $tableArr) |
| 19 | + { |
| 20 | + Type::registerCustomPlatformTypes(); |
| 21 | + |
| 22 | + $this->table = Table::make($tableArr); |
| 23 | + $this->tableArr = $tableArr; |
| 24 | + $this->originalTable = SchemaManager::listTableDetails($tableArr['oldName']); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Update the table. |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public static function update($table) |
| 33 | + { |
| 34 | + if (!is_array($table)) { |
| 35 | + $table = json_decode($table, true); |
| 36 | + } |
| 37 | + |
| 38 | + if (!SchemaManager::tableExists($table['oldName'])) { |
| 39 | + throw SchemaException::tableDoesNotExist($table['oldName']); |
| 40 | + } |
| 41 | + |
| 42 | + $updater = new self($table); |
| 43 | + |
| 44 | + $updater->updateTable(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Updates the table. |
| 49 | + * |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function updateTable() |
| 53 | + { |
| 54 | + // Get table new name |
| 55 | + if (($newName = $this->table->getName()) != $this->originalTable->getName()) { |
| 56 | + // Make sure the new name doesn't already exist |
| 57 | + if (SchemaManager::tableExists($newName)) { |
| 58 | + throw SchemaException::tableAlreadyExists($newName); |
| 59 | + } |
| 60 | + } else { |
| 61 | + $newName = false; |
| 62 | + } |
| 63 | + |
| 64 | + // Rename columns |
| 65 | + if ($renamedColumnsDiff = $this->getRenamedColumnsDiff()) { |
| 66 | + SchemaManager::alterTable($renamedColumnsDiff); |
| 67 | + |
| 68 | + // Refresh original table after renaming the columns |
| 69 | + $this->originalTable = SchemaManager::listTableDetails($this->tableArr['oldName']); |
| 70 | + } |
| 71 | + |
| 72 | + $tableDiff = $this->originalTable->diff($this->table); |
| 73 | + |
| 74 | + // Add new table name to tableDiff |
| 75 | + if ($newName) { |
| 76 | + if (!$tableDiff) { |
| 77 | + $tableDiff = new TableDiff($this->tableArr['oldName']); |
| 78 | + $tableDiff->fromTable = $this->originalTable; |
| 79 | + } |
| 80 | + |
| 81 | + $tableDiff->newName = $newName; |
| 82 | + } |
| 83 | + |
| 84 | + // Update the table |
| 85 | + if ($tableDiff) { |
| 86 | + SchemaManager::alterTable($tableDiff); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the table diff to rename columns. |
| 92 | + * |
| 93 | + * @return \Doctrine\DBAL\Schema\TableDiff |
| 94 | + */ |
| 95 | + protected function getRenamedColumnsDiff() |
| 96 | + { |
| 97 | + $renamedColumns = $this->getRenamedColumns(); |
| 98 | + |
| 99 | + if (empty($renamedColumns)) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + $renamedColumnsDiff = new TableDiff($this->tableArr['oldName']); |
| 104 | + $renamedColumnsDiff->fromTable = $this->originalTable; |
| 105 | + |
| 106 | + foreach ($renamedColumns as $oldName => $newName) { |
| 107 | + $renamedColumnsDiff->renamedColumns[$oldName] = $this->table->getColumn($newName); |
| 108 | + } |
| 109 | + |
| 110 | + return $renamedColumnsDiff; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get the table diff to rename columns and indexes. |
| 115 | + * |
| 116 | + * @return \Doctrine\DBAL\Schema\TableDiff |
| 117 | + */ |
| 118 | + protected function getRenamedDiff() |
| 119 | + { |
| 120 | + $renamedColumns = $this->getRenamedColumns(); |
| 121 | + $renamedIndexes = $this->getRenamedIndexes(); |
| 122 | + |
| 123 | + if (empty($renamedColumns) && empty($renamedIndexes)) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + $renamedDiff = new TableDiff($this->tableArr['oldName']); |
| 128 | + $renamedDiff->fromTable = $this->originalTable; |
| 129 | + |
| 130 | + foreach ($renamedColumns as $oldName => $newName) { |
| 131 | + $renamedDiff->renamedColumns[$oldName] = $this->table->getColumn($newName); |
| 132 | + } |
| 133 | + |
| 134 | + foreach ($renamedIndexes as $oldName => $newName) { |
| 135 | + $renamedDiff->renamedIndexes[$oldName] = $this->table->getIndex($newName); |
| 136 | + } |
| 137 | + |
| 138 | + return $renamedDiff; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Get columns that were renamed. |
| 143 | + * |
| 144 | + * @return array |
| 145 | + */ |
| 146 | + protected function getRenamedColumns() |
| 147 | + { |
| 148 | + $renamedColumns = []; |
| 149 | + |
| 150 | + foreach ($this->tableArr['columns'] as $column) { |
| 151 | + $oldName = $column['oldName']; |
| 152 | + |
| 153 | + // make sure this is an existing column and not a new one |
| 154 | + if ($this->originalTable->hasColumn($oldName)) { |
| 155 | + $name = $column['name']; |
| 156 | + |
| 157 | + if ($name != $oldName) { |
| 158 | + $renamedColumns[$oldName] = $name; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return $renamedColumns; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Get indexes that were renamed. |
| 168 | + * |
| 169 | + * @return array |
| 170 | + */ |
| 171 | + protected function getRenamedIndexes() |
| 172 | + { |
| 173 | + $renamedIndexes = []; |
| 174 | + |
| 175 | + foreach ($this->tableArr['indexes'] as $index) { |
| 176 | + $oldName = $index['oldName']; |
| 177 | + |
| 178 | + // make sure this is an existing index and not a new one |
| 179 | + if ($this->originalTable->hasIndex($oldName)) { |
| 180 | + $name = $index['name']; |
| 181 | + |
| 182 | + if ($name != $oldName) { |
| 183 | + $renamedIndexes[$oldName] = $name; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return $renamedIndexes; |
| 189 | + } |
| 190 | +} |
0 commit comments