Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new column definition builder #293

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

/**
* Implements a (Data Definition Language) SQL statements for Oracle Server.
Expand Down Expand Up @@ -45,14 +46,13 @@ public function addForeignKey(
return $sql;
}

public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
{
/** @psalm-suppress DeprecatedMethod */
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' MODIFY '
. $this->quoter->quoteColumnName($column)
. ' ' . $this->queryBuilder->getColumnType($type);
. ' ' . $this->queryBuilder->buildColumnDefinition($type);
}

public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
Expand Down
1 change: 1 addition & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function __construct(protected ConnectionInterface $db, SchemaCache $sche
/** @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\Oracle\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['integer'][0] = '"column" NUMBER(10)';

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

$types['uuid with default'][0] = '"column" RAW(16) DEFAULT HEXTORAW(REGEXP_REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\', \'-\', \'\'))';
$types['uuid with default'][3] = [['defaultExpression', 'HEXTORAW(REGEXP_REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\', \'-\', \'\'))']];

$types['uuid pk'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY';
$types['uuid pk not null'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY NOT NULL';
$types['uuid pk not null with default'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY NOT NULL';
$types['uuid pk not null with default'][3] = [['notNull']];

return $types;
}
}
8 changes: 8 additions & 0 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Oracle\Tests\Provider;

use Exception;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Expression\Expression;
Expand Down Expand Up @@ -44,6 +45,13 @@ public static function addForeignKey(): array
return $addForeingKey;
}

public static function alterColumn(): array
{
return [
[ColumnType::STRING, 'ALTER TABLE "foo1" MODIFY "bar" varchar2(255)'],
];
}

public static function batchInsert(): array
{
$batchInsert = parent::batchInsert();
Expand Down
36 changes: 13 additions & 23 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Throwable;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand All @@ -28,6 +27,11 @@ final class QueryBuilderTest extends CommonQueryBuilderTest
{
use TestTrait;

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

/**
* @throws Exception
* @throws InvalidConfigException
Expand Down Expand Up @@ -99,24 +103,10 @@ public function testAddUnique(string $name, string $table, array|string $columns
parent::testAddUnique($name, $table, $columns, $expected);
}

/**
* @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->assertSame(
<<<SQL
ALTER TABLE "customer" MODIFY "email" VARCHAR2(255)
SQL,
$qb->alterColumn('customer', 'email', ColumnType::STRING),
);

$db->close();
parent::testAlterColumn($type, $expected);
}

/**
Expand Down Expand Up @@ -312,11 +302,11 @@ public function testCreateTable(): void
$this->assertSame(
<<<SQL
CREATE TABLE "test" (
\t"id" NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
\t"name" VARCHAR2(255) NOT NULL,
\t"email" VARCHAR2(255) NOT NULL,
\t"status" NUMBER(10) NOT NULL,
\t"created_at" TIMESTAMP(0) NOT NULL
\t"id" number(10) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
\t"name" varchar2(255) NOT NULL,
\t"email" varchar2(255) NOT NULL,
\t"status" number(10) NOT NULL,
\t"created_at" timestamp NOT NULL
)
SQL,
$qb->createTable(
Expand Down
Loading