From bb9a797b886d602d98800bcbf5d209104a5783e0 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 18 Oct 2024 18:09:26 +0700 Subject: [PATCH] Load column properties via constructor (#324) --- CHANGELOG.md | 2 ++ src/Column/ColumnBuilder.php | 3 +-- src/Column/ColumnFactory.php | 13 +++++++++---- src/Dsn.php | 2 +- tests/CommandTest.php | 2 +- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a99cb15e..a62bc393 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ - New #320: Realize `ColumnBuilder` class (@Tigrov) - Enh #321: Update according changes in `ColumnSchemaInterface` (@Tigrov) - New #322: Add `ColumnDefinitionBuilder` class (@Tigrov) +- Enh #323: Refactor `Dsn` class (@Tigrov) +- Enh #324: Use constructor to create columns and initialize properties (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php index 945c43f9..ace70ca9 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -11,7 +11,6 @@ final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder { public static function binary(int|null $size = null): ColumnSchemaInterface { - return (new BinaryColumnSchema(ColumnType::BINARY)) - ->size($size); + return new BinaryColumnSchema(ColumnType::BINARY, size: $size); } } diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index a09b02a9..4e3b5a65 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -9,6 +9,7 @@ use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; +use Yiisoft\Db\Schema\Column\StringColumnSchema; final class ColumnFactory extends AbstractColumnFactory { @@ -76,9 +77,12 @@ protected function getType(string $dbType, array $info = []): string public function fromPseudoType(string $pseudoType, array $info = []): ColumnSchemaInterface { if ($pseudoType === PseudoType::UUID_PK_SEQ) { - return ColumnBuilder::uuidPrimaryKey() - ->defaultValue(new Expression('newsequentialid()')) - ->load($info); + unset($info['type']); + $info['primaryKey'] = true; + $info['autoIncrement'] = true; + $info['defaultValue'] = new Expression('newsequentialid()'); + + return new StringColumnSchema(ColumnType::UUID, ...$info); } return parent::fromPseudoType($pseudoType, $info); @@ -87,7 +91,8 @@ public function fromPseudoType(string $pseudoType, array $info = []): ColumnSche public function fromType(string $type, array $info = []): ColumnSchemaInterface { if ($type === ColumnType::BINARY) { - return (new BinaryColumnSchema($type))->load($info); + unset($info['type']); + return new BinaryColumnSchema($type, ...$info); } return parent::fromType($type, $info); diff --git a/src/Dsn.php b/src/Dsn.php index 5050bc75..0330b872 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -18,7 +18,7 @@ final class Dsn extends AbstractDsn */ public function __construct( string $driver = 'sqlsrv', - string $host = 'localhost', + string $host = '127.0.0.1', string|null $databaseName = null, string|null $port = '1433', array $options = [] diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 560521e4..e3e50bfe 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -321,7 +321,7 @@ public function testShowDatabases(): void $command = $db->createCommand(); - $this->assertSame('sqlsrv:Server=localhost,1433;Encrypt=no', $db->getDriver()->getDsn()); + $this->assertSame('sqlsrv:Server=127.0.0.1,1433;Encrypt=no', $db->getDriver()->getDsn()); $this->assertSame(['yiitest'], $command->showDatabases()); }