AddColumn.php
5 years ago
AddColumns.php
5 years ago
AddIndex.php
5 years ago
AddPrimaryKey.php
5 years ago
AddUniqueKey.php
5 years ago
BatchInsert.php
5 years ago
BoundSql.php
5 years ago
ChangeColumn.php
5 years ago
ChangeColumnType.php
5 years ago
ChangeColumnTypes.php
5 years ago
CreateTable.php
5 years ago
DropColumn.php
5 years ago
DropColumns.php
5 years ago
DropIndex.php
5 years ago
DropPrimaryKey.php
5 years ago
DropTable.php
5 years ago
Factory.php
5 years ago
Insert.php
5 years ago
Sql.php
5 years ago
AddColumns.php
49 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | namespace Piwik\Updater\Migration\Db; |
| 9 | |
| 10 | use Piwik\DataAccess\TableMetadata; |
| 11 | |
| 12 | /** |
| 13 | * @see Factory::addColumns() |
| 14 | * @ignore |
| 15 | */ |
| 16 | class AddColumns extends Sql |
| 17 | { |
| 18 | public function __construct($table, $columns, $placeColumnAfter) |
| 19 | { |
| 20 | $tableMetadata = new TableMetadata(); |
| 21 | try { |
| 22 | $existingColumns = $tableMetadata->getColumns($table); |
| 23 | } catch (\Exception $ex) { |
| 24 | $existingColumns = []; |
| 25 | } |
| 26 | |
| 27 | $changes = array(); |
| 28 | foreach ($columns as $columnName => $columnType) { |
| 29 | if (in_array($columnName, $existingColumns)) { |
| 30 | continue; |
| 31 | } |
| 32 | |
| 33 | $part = sprintf("ADD COLUMN `%s` %s", $columnName, $columnType); |
| 34 | |
| 35 | if (!empty($placeColumnAfter)) { |
| 36 | $part .= sprintf(' AFTER `%s`', $placeColumnAfter); |
| 37 | $placeColumnAfter = $columnName; |
| 38 | } |
| 39 | |
| 40 | $changes[] = $part; |
| 41 | } |
| 42 | |
| 43 | $sql = sprintf("ALTER TABLE `%s` %s", $table, implode(', ', $changes)); |
| 44 | |
| 45 | parent::__construct($sql, static::ERROR_CODE_DUPLICATE_COLUMN); |
| 46 | } |
| 47 | |
| 48 | } |
| 49 |