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

Load column properties via constructor #324

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 9 additions & 4 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down