Skip to content

Commit 74c4336

Browse files
authored
5.x Database column (#947)
* 5.x Use cakephp/database Column as a base class Align migrations with cakephp/database where possible. I've introduced some soft deprecations for the properties that have different names in migrations currently. Longer term, I'd like the internal data objects to be aligned and the compatibility shim is only used when constructing columns from arrays. * Make null parameter work better. * Remove remaining support for Literal column types * More pruning related to Literal types
1 parent 6469345 commit 74c4336

File tree

8 files changed

+90
-149
lines changed

8 files changed

+90
-149
lines changed

src/Db/Action/AddColumn.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Migrations\Db\Action;
1010

11-
use Migrations\Db\Literal;
1211
use Migrations\Db\Table\Column;
1312
use Migrations\Db\Table\TableMetadata;
1413

@@ -38,11 +37,11 @@ public function __construct(TableMetadata $table, Column $column)
3837
*
3938
* @param \Migrations\Db\Table\TableMetadata $table The table to add the column to
4039
* @param string $columnName The column name
41-
* @param string|\Migrations\Db\Literal $type The column type
40+
* @param string $type The column type
4241
* @param array<string, mixed> $options The column options
4342
* @return self
4443
*/
45-
public static function build(TableMetadata $table, string $columnName, string|Literal $type, array $options = []): self
44+
public static function build(TableMetadata $table, string $columnName, string $type, array $options = []): self
4645
{
4746
$column = new Column();
4847
$column->setName($columnName);

src/Db/Action/ChangeColumn.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Migrations\Db\Action;
1010

11-
use Migrations\Db\Literal;
1211
use Migrations\Db\Table\Column;
1312
use Migrations\Db\Table\TableMetadata;
1413

@@ -53,11 +52,11 @@ public function __construct(TableMetadata $table, string $columnName, Column $co
5352
*
5453
* @param \Migrations\Db\Table\TableMetadata $table The table to alter
5554
* @param string $columnName The name of the column to change
56-
* @param string|\Migrations\Db\Literal $type The type of the column
55+
* @param string $type The type of the column
5756
* @param array<string, mixed> $options Additional options for the column
5857
* @return self
5958
*/
60-
public static function build(TableMetadata $table, string $columnName, string|Literal $type, array $options = []): self
59+
public static function build(TableMetadata $table, string $columnName, string $type, array $options = []): self
6160
{
6261
$column = new Column();
6362
$column->setName($columnName);

src/Db/Adapter/AbstractAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public function getAdapterType(): string
391391
*/
392392
public function isValidColumnType(Column $column): bool
393393
{
394-
return $column->getType() instanceof Literal || in_array($column->getType(), $this->getColumnTypes(), true);
394+
return in_array($column->getType(), $this->getColumnTypes(), true);
395395
}
396396

397397
/**

src/Db/Adapter/MysqlAdapter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,7 @@ static function ($value) {
642642
$extra = ' ' . implode(' ', $extras);
643643

644644
if (($row['Default'] !== null)) {
645-
$columnType = $targetColumn->getType();
646-
// Column::getType() can return string|Literal, but getDefaultValueDefinition expects string|null
647-
$columnTypeName = is_string($columnType) ? $columnType : null;
648-
$extra .= $this->getDefaultValueDefinition($row['Default'], $columnTypeName);
645+
$extra .= $this->getDefaultValueDefinition($row['Default'], $targetColumn->getType());
649646
}
650647
$definition = $row['Type'] . ' ' . $null . $extra . $comment;
651648

src/Db/Table.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,16 @@ public function addPrimaryKey(string|array $columns)
325325
* Valid options can be: limit, default, null, precision or scale.
326326
*
327327
* @param string|\Migrations\Db\Table\Column $columnName Column Name
328-
* @param string|\Migrations\Db\Literal|null $type Column Type
328+
* @param string|null $type Column Type
329329
* @param array<string, mixed> $options Column Options
330330
* @throws \InvalidArgumentException
331331
* @return $this
332332
*/
333-
public function addColumn(string|Column $columnName, string|Literal|null $type = null, array $options = [])
333+
public function addColumn(string|Column $columnName, ?string $type = null, array $options = [])
334334
{
335335
assert($columnName instanceof Column || $type !== null);
336336
if ($columnName instanceof Column) {
337337
$action = new AddColumn($this->table, $columnName);
338-
} elseif ($type instanceof Literal) {
339-
$action = AddColumn::build($this->table, $columnName, $type, $options);
340338
} else {
341339
$action = new AddColumn($this->table, $this->getAdapter()->getColumnForType($columnName, $type, $options));
342340
}
@@ -388,11 +386,11 @@ public function renameColumn(string $oldName, string $newName)
388386
* Change a table column type.
389387
*
390388
* @param string $columnName Column Name
391-
* @param string|\Migrations\Db\Table\Column|\Migrations\Db\Literal $newColumnType New Column Type
389+
* @param string|\Migrations\Db\Table\Column $newColumnType New Column Type
392390
* @param array<string, mixed> $options Options
393391
* @return $this
394392
*/
395-
public function changeColumn(string $columnName, string|Column|Literal $newColumnType, array $options = [])
393+
public function changeColumn(string $columnName, string|Column $newColumnType, array $options = [])
396394
{
397395
if ($newColumnType instanceof Column) {
398396
$action = new ChangeColumn($this->table, $columnName, $newColumnType);

0 commit comments

Comments
 (0)