Skip to content

Commit

Permalink
Replace name() with immutable withName() in ColumnInterface (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jan 12, 2025
1 parent 98eed39 commit 828b5db
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- Enh #905: Use `AbstractColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov)
- Enh #915: Remove `ColumnInterface` (@Tigrov)
- Enh #917: Rename `ColumnSchemaInterface` to `ColumnInterface` (@Tigrov)
- Enh #919: Replace `name()` with immutable `withName()` method in `ColumnInterface` interface (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ and the following changes were made:

- `getName()` method can return `string` or `null`;
- `getPhpType()` method must return `string` PHP type of the column which used for generating related model properties;
- `name(string|null $name)` method is added;
- `withName(string|null $name)` method is added;
- `check(string|null $check)` method is added;
- `getCheck()` method is added;
- `reference(ForeignKeyConstraint|null $reference)` method is added;
Expand Down
8 changes: 6 additions & 2 deletions src/QueryBuilder/AbstractDDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ public function createTable(string $table, array $columns, string $options = nul
$cols[] = "\t"
. $this->quoter->quoteColumnName($name)
. ' '
. $this->queryBuilder->buildColumnDefinition($type);
. $this->queryBuilder->buildColumnDefinition(
$type instanceof ColumnInterface
? $type->withName($name)
: $type
);
} else {
/** @psalm-var string $type */
/** @var string $type */
$cols[] = "\t" . $type;
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/Schema/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,7 @@ public function getExtra(): string|null
return $this->extra;
}

/**
* @deprecated Will be removed in version 2.0.
* @psalm-mutation-free
*/
/** @psalm-mutation-free */
public function getName(): string|null
{
return $this->name;
Expand Down Expand Up @@ -372,4 +369,11 @@ public function unsigned(bool $unsigned = true): static
$this->unsigned = $unsigned;
return $this;
}

public function withName(string|null $name): static
{
$new = clone $this;
$new->name = $name;
return $new;
}
}
25 changes: 11 additions & 14 deletions src/Schema/Column/ColumnInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ public function getExtra(): string|null;
/**
* @return string|null The name of the column.
*
* @deprecated Will be removed in version 2.0.
* @psalm-mutation-free
*/
public function getName(): string|null;
Expand Down Expand Up @@ -319,19 +318,6 @@ public function isUnique(): bool;
*/
public function isUnsigned(): bool;

/**
* Sets a name of the column.
*
* ```php
* $columns = [
* 'id' => ColumnBuilder::primaryKey()->name('id'),
* ];
* ```
*
* @deprecated Will be removed in version 2.0.
*/
public function name(string|null $name): static;

/**
* Whether the column is not nullable.
*
Expand Down Expand Up @@ -453,4 +439,15 @@ public function unique(bool $unique = true): static;
* ```
*/
public function unsigned(bool $unsigned = true): static;

/**
* Returns a new instance with the specified name of the column.
*
* ```php
* $columns = [
* 'id' => ColumnBuilder::primaryKey()->withName('id'),
* ];
* ```
*/
public function withName(string|null $name): static;
}
3 changes: 3 additions & 0 deletions tests/Db/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\Column\ColumnBuilder;
use Yiisoft\Db\Schema\Column\ColumnInterface;
use Yiisoft\Db\Tests\AbstractCommandTest;
use Yiisoft\Db\Tests\Support\Assert;
Expand Down Expand Up @@ -239,6 +240,7 @@ public function testCreateTable(): void
\t[address] varchar(255) NOT NULL,
\t[status] integer NOT NULL,
\t[profile_id] integer NOT NULL,
\t[data] json CHECK (json_valid([data])),
\t[created_at] timestamp NOT NULL,
\t[updated_at] timestamp NOT NULL
) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB
Expand All @@ -250,6 +252,7 @@ public function testCreateTable(): void
'address' => ColumnType::STRING . '(255) NOT NULL',
'status' => ColumnType::INTEGER . ' NOT NULL',
'profile_id' => ColumnType::INTEGER . ' NOT NULL',
'data' => ColumnBuilder::json(),
'created_at' => ColumnType::TIMESTAMP . ' NOT NULL',
'updated_at' => ColumnType::TIMESTAMP . ' NOT NULL',
];
Expand Down
11 changes: 7 additions & 4 deletions tests/Db/Schema/Column/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,15 @@ public function testName(): void
$column = new Column();

$this->assertNull($column->getName());
$this->assertSame($column, $column->name('test'));
$this->assertSame('test', $column->getName());

$column->name('');
$newColumn = $column->withName('test');

$this->assertSame('', $column->getName());
$this->assertNotSame($column, $newColumn);
$this->assertSame('test', $newColumn->getName());

$newColumn = $newColumn->withName('');

$this->assertSame('', $newColumn->getName());
}

public function testNotNull(): void
Expand Down
17 changes: 17 additions & 0 deletions tests/Support/Stub/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
'decimal',
];

protected function buildCheck(ColumnInterface $column): string
{
$check = $column->getCheck();

if (empty($check)) {
$columnName = $column->getName();

if (!empty($columnName) && $column->getType() === ColumnType::JSON) {
return ' CHECK (json_valid(' . $this->queryBuilder->quoter()->quoteColumnName($columnName) . '))';
}

return '';
}

return " CHECK ($check)";
}

protected function getDbType(ColumnInterface $column): string
{
return $column->getDbType() ?? match ($column->getType()) {
Expand Down

0 comments on commit 828b5db

Please sign in to comment.