Skip to content

Commit

Permalink
Use new column definition builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Nov 25, 2024
1 parent 84486b5 commit ba9a538
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 43 deletions.
4 changes: 4 additions & 0 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
*
* Provides a fluent interface, which means that the methods can be chained together to create a column schema with
* many properties in a single line of code.
*
* @psalm-suppress DeprecatedClass
*
* @deprecated Use {@see StringColumnSchema} or other column classes instead. Will be removed in 2.0.0.
*/
final class Column extends AbstractColumn
{
Expand Down
3 changes: 2 additions & 1 deletion src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

use function count;

Expand Down Expand Up @@ -81,7 +82,7 @@ public function addUnique(string $table, string $name, array|string $columns): s
/**
* @throws NotSupportedException SQLite doesn't support this method.
*/
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
{
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
}
Expand Down
1 change: 1 addition & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ final class Schema extends AbstractPdoSchema
/** @deprecated Use {@see ColumnBuilder} instead. Will be removed in 2.0. */
public function createColumn(string $type, array|int|string $length = null): ColumnInterface
{
/** @psalm-suppress DeprecatedClass */
return new Column($type, $length);
}

Expand Down
8 changes: 0 additions & 8 deletions tests/ColumnSchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,4 @@ public function testCustomTypes(string $expected, string $type, int|null $length
{
$this->checkBuildString($expected, $type, $length, $calls);
}

/**
* @dataProvider \Yiisoft\Db\Sqlite\Tests\Provider\ColumnSchemaBuilderProvider::createColumnTypes
*/
public function testCreateColumnTypes(string $expected, string $type, ?int $length, array $calls): void
{
parent::testCreateColumnTypes($expected, $type, $length, $calls);
}
}
19 changes: 0 additions & 19 deletions tests/Provider/ColumnSchemaBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,4 @@ public static function types(): array
['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]],
];
}

public static function createColumnTypes(): array
{
$types = parent::createColumnTypes();

$types['uuid'][0] = '`column` blob(16)';
$types['uuid not null'][0] = '`column` blob(16) NOT NULL';

$types['uuid with default'][0] = '`column` blob(16) DEFAULT (UNHEX(REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\',\'-\',\'\')))';
$types['uuid with default'][3] = [['defaultExpression', '(UNHEX(REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\',\'-\',\'\')))']];

$types['uuid pk'][0] = '`column` blob(16) PRIMARY KEY';
$types['uuid pk not null'][0] = '`column` blob(16) PRIMARY KEY NOT NULL';

$types['uuid pk not null with default'][0] = '`column` blob(16) PRIMARY KEY NOT NULL DEFAULT (RANDOMBLOB(16))';
$types['uuid pk not null with default'][3] = [['notNull'],['defaultExpression', '(RANDOMBLOB(16))']];

return $types;
}
}
27 changes: 12 additions & 15 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use JsonException;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand All @@ -18,7 +17,7 @@
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Sqlite\Column;
use Yiisoft\Db\Sqlite\Column\ColumnBuilder;
use Yiisoft\Db\Sqlite\Tests\Provider\QueryBuilderProvider;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest;
Expand All @@ -32,6 +31,11 @@ final class QueryBuilderTest extends CommonQueryBuilderTest
{
use TestTrait;

public function getBuildColumnDefinitionProvider(): array
{
return QueryBuilderProvider::buildColumnDefinition();
}

/**
* @throws Exception
* @throws InvalidConfigException
Expand Down Expand Up @@ -162,20 +166,13 @@ public function testAddUnique(string $name, string $table, array|string $columns
$qb->addUnique($table, $name, $columns);
}

/**
* @throws Exception
* @throws InvalidConfigException
*/
public function testAlterColumn(): void
#[DataProviderExternal(QueryBuilderProvider::class, 'alterColumn')]
public function testAlterColumn(string|ColumnSchemaInterface $type, string $expected): void
{
$db = $this->getConnection();

$qb = $db->getQueryBuilder();

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('Yiisoft\Db\Sqlite\DDLQueryBuilder::alterColumn is not supported by SQLite.');

$qb->alterColumn('customer', 'email', ColumnType::STRING);
parent::testAlterColumn($type, $expected);
}

/**
Expand Down Expand Up @@ -753,16 +750,16 @@ public function testUpsertExecute(
public function testJsonColumn()
{
$qb = $this->getConnection()->getQueryBuilder();
$columnSchemaBuilder = new Column(ColumnType::JSON);
$column = ColumnBuilder::json();

$this->assertSame(
'ALTER TABLE `json_table` ADD `json_col` json',
$qb->addColumn('json_table', 'json_col', $columnSchemaBuilder->asString()),
$qb->addColumn('json_table', 'json_col', $column),
);

$this->assertSame(
"CREATE TABLE `json_table` (\n\t`json_col` json\n)",
$qb->createTable('json_table', ['json_col' => $columnSchemaBuilder]),
$qb->createTable('json_table', ['json_col' => $column]),
);

$this->assertSame(
Expand Down

0 comments on commit ba9a538

Please sign in to comment.