Skip to content

Commit 828b5db

Browse files
authored
Replace name() with immutable withName() in ColumnInterface (#919)
1 parent 98eed39 commit 828b5db

File tree

8 files changed

+54
-25
lines changed

8 files changed

+54
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
- Enh #905: Use `AbstractColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov)
5454
- Enh #915: Remove `ColumnInterface` (@Tigrov)
5555
- Enh #917: Rename `ColumnSchemaInterface` to `ColumnInterface` (@Tigrov)
56+
- Enh #919: Replace `name()` with immutable `withName()` method in `ColumnInterface` interface (@Tigrov)
5657

5758
## 1.3.0 March 21, 2024
5859

UPGRADE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ and the following changes were made:
7575

7676
- `getName()` method can return `string` or `null`;
7777
- `getPhpType()` method must return `string` PHP type of the column which used for generating related model properties;
78-
- `name(string|null $name)` method is added;
78+
- `withName(string|null $name)` method is added;
7979
- `check(string|null $check)` method is added;
8080
- `getCheck()` method is added;
8181
- `reference(ForeignKeyConstraint|null $reference)` method is added;

src/QueryBuilder/AbstractDDLQueryBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,13 @@ public function createTable(string $table, array $columns, string $options = nul
171171
$cols[] = "\t"
172172
. $this->quoter->quoteColumnName($name)
173173
. ' '
174-
. $this->queryBuilder->buildColumnDefinition($type);
174+
. $this->queryBuilder->buildColumnDefinition(
175+
$type instanceof ColumnInterface
176+
? $type->withName($name)
177+
: $type
178+
);
175179
} else {
176-
/** @psalm-var string $type */
180+
/** @var string $type */
177181
$cols[] = "\t" . $type;
178182
}
179183
}

src/Schema/Column/AbstractColumn.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,7 @@ public function getExtra(): string|null
203203
return $this->extra;
204204
}
205205

206-
/**
207-
* @deprecated Will be removed in version 2.0.
208-
* @psalm-mutation-free
209-
*/
206+
/** @psalm-mutation-free */
210207
public function getName(): string|null
211208
{
212209
return $this->name;
@@ -372,4 +369,11 @@ public function unsigned(bool $unsigned = true): static
372369
$this->unsigned = $unsigned;
373370
return $this;
374371
}
372+
373+
public function withName(string|null $name): static
374+
{
375+
$new = clone $this;
376+
$new->name = $name;
377+
return $new;
378+
}
375379
}

src/Schema/Column/ColumnInterface.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ public function getExtra(): string|null;
198198
/**
199199
* @return string|null The name of the column.
200200
*
201-
* @deprecated Will be removed in version 2.0.
202201
* @psalm-mutation-free
203202
*/
204203
public function getName(): string|null;
@@ -319,19 +318,6 @@ public function isUnique(): bool;
319318
*/
320319
public function isUnsigned(): bool;
321320

322-
/**
323-
* Sets a name of the column.
324-
*
325-
* ```php
326-
* $columns = [
327-
* 'id' => ColumnBuilder::primaryKey()->name('id'),
328-
* ];
329-
* ```
330-
*
331-
* @deprecated Will be removed in version 2.0.
332-
*/
333-
public function name(string|null $name): static;
334-
335321
/**
336322
* Whether the column is not nullable.
337323
*
@@ -453,4 +439,15 @@ public function unique(bool $unique = true): static;
453439
* ```
454440
*/
455441
public function unsigned(bool $unsigned = true): static;
442+
443+
/**
444+
* Returns a new instance with the specified name of the column.
445+
*
446+
* ```php
447+
* $columns = [
448+
* 'id' => ColumnBuilder::primaryKey()->withName('id'),
449+
* ];
450+
* ```
451+
*/
452+
public function withName(string|null $name): static;
456453
}

tests/Db/Command/CommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Yiisoft\Db\Constant\ColumnType;
88
use Yiisoft\Db\Constant\PseudoType;
99
use Yiisoft\Db\Exception\NotSupportedException;
10+
use Yiisoft\Db\Schema\Column\ColumnBuilder;
1011
use Yiisoft\Db\Schema\Column\ColumnInterface;
1112
use Yiisoft\Db\Tests\AbstractCommandTest;
1213
use Yiisoft\Db\Tests\Support\Assert;
@@ -239,6 +240,7 @@ public function testCreateTable(): void
239240
\t[address] varchar(255) NOT NULL,
240241
\t[status] integer NOT NULL,
241242
\t[profile_id] integer NOT NULL,
243+
\t[data] json CHECK (json_valid([data])),
242244
\t[created_at] timestamp NOT NULL,
243245
\t[updated_at] timestamp NOT NULL
244246
) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB
@@ -250,6 +252,7 @@ public function testCreateTable(): void
250252
'address' => ColumnType::STRING . '(255) NOT NULL',
251253
'status' => ColumnType::INTEGER . ' NOT NULL',
252254
'profile_id' => ColumnType::INTEGER . ' NOT NULL',
255+
'data' => ColumnBuilder::json(),
253256
'created_at' => ColumnType::TIMESTAMP . ' NOT NULL',
254257
'updated_at' => ColumnType::TIMESTAMP . ' NOT NULL',
255258
];

tests/Db/Schema/Column/ColumnTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,15 @@ public function testName(): void
171171
$column = new Column();
172172

173173
$this->assertNull($column->getName());
174-
$this->assertSame($column, $column->name('test'));
175-
$this->assertSame('test', $column->getName());
176174

177-
$column->name('');
175+
$newColumn = $column->withName('test');
178176

179-
$this->assertSame('', $column->getName());
177+
$this->assertNotSame($column, $newColumn);
178+
$this->assertSame('test', $newColumn->getName());
179+
180+
$newColumn = $newColumn->withName('');
181+
182+
$this->assertSame('', $newColumn->getName());
180183
}
181184

182185
public function testNotNull(): void

tests/Support/Stub/ColumnDefinitionBuilder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
3636
'decimal',
3737
];
3838

39+
protected function buildCheck(ColumnInterface $column): string
40+
{
41+
$check = $column->getCheck();
42+
43+
if (empty($check)) {
44+
$columnName = $column->getName();
45+
46+
if (!empty($columnName) && $column->getType() === ColumnType::JSON) {
47+
return ' CHECK (json_valid(' . $this->queryBuilder->quoter()->quoteColumnName($columnName) . '))';
48+
}
49+
50+
return '';
51+
}
52+
53+
return " CHECK ($check)";
54+
}
55+
3956
protected function getDbType(ColumnInterface $column): string
4057
{
4158
return $column->getDbType() ?? match ($column->getType()) {

0 commit comments

Comments
 (0)