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

Refactor ColumnSchemaInterface #881

Merged
merged 3 commits into from
Oct 1, 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
6 changes: 4 additions & 2 deletions .github/workflows/db-mssql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
CURRENT_PACKAGE: db-mssql
EXTENSIONS: pdo, pdo_sqlsrv-5.10.1

runs-on: ubuntu-latest
runs-on: ${{ matrix.mssql.os || 'ubuntu-latest' }}

strategy:
matrix:
Expand All @@ -43,7 +43,9 @@ jobs:

include:
- php: 8.3
mssql: { server: 2017-latest }
mssql:
server: 2017-latest
os: ubuntu-20.04
- php: 8.3
mssql:
server: 2019-latest
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- Enh #875: Ignore "Packets out of order..." warnings in `AbstractPdoCommand::internalExecute()` method (@Tigrov)
- Enh #877: Separate column type constants (@Tigrov)
- Enh #878: Realize `ColumnBuilder` class (@Tigrov)
- Enh #881: Refactor `ColumnSchemaInterface` and `AbstractColumnSchema` (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
8 changes: 8 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ and the following changes were made:
- `getName()` method can return `string` or `null`;
- `getPhpType()` method must return `string` PHP type of the column which used for generating related model properties;
- `name(string|null $name)` method is added;
- `check(string|null $check)` method is added;
- `getCheck()` method is added;
- `reference(ForeignKeyConstraint|null $reference)` method is added;
- `getReference()` method is added;
- `notNull(bool $notNull = true)` method is added;
- `isNotNull()` method is added;
- `unique(bool $unique = true)` method is added;
- `isUnique()` method is added;
- `load(array $info)` method is added;
- constructor of `AbstractColumnSchema` class is changed to `__construct(string $type, string|null $phpType = null)`;
- added method chaining.
Expand Down
95 changes: 82 additions & 13 deletions src/Schema/Column/AbstractColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PhpType;

use Yiisoft\Db\Constraint\ForeignKeyConstraint;

use function is_array;

/**
* Represents the metadata of a column in a database table.
*
* It provides information about the column's name, type, size, precision, and other details.
* It provides information about the column's type, size, scale, and other details.
*
* The `ColumnSchema` class is used to store and retrieve metadata about a column in a database table.
*
Expand All @@ -25,19 +27,18 @@
* use Yiisoft\Db\Schema\ColumnSchema;
*
* $column = (new IntegerColumnSchema())
* ->name('id')
* ->allowNull(false)
* ->dbType('int(11)')
* ->type('integer')
* ->notNull()
* ->dbType('int')
* ->size(11)
* ->defaultValue(0)
* ->autoIncrement()
* ->primaryKey();
* ```
*/
abstract class AbstractColumnSchema implements ColumnSchemaInterface
{
private bool $allowNull = false;
private bool $autoIncrement = false;
private string|null $check = null;
private string|null $comment = null;
private bool $computed = false;
private string|null $dbType = null;
Expand All @@ -46,9 +47,11 @@ abstract class AbstractColumnSchema implements ColumnSchemaInterface
private string|null $extra = null;
private bool $isPrimaryKey = false;
private string|null $name = null;
private int|null $precision = null;
private bool $notNull = false;
private ForeignKeyConstraint|null $reference = null;
private int|null $scale = null;
private int|null $size = null;
private bool $unique = false;
private bool $unsigned = false;

/**
Expand All @@ -59,9 +62,12 @@ public function __construct(
) {
}

/**
* @deprecated Use {@see notNull()} instead. Will be removed in version 2.0.
*/
public function allowNull(bool $allowNull = true): static
{
$this->allowNull = $allowNull;
$this->notNull(!$allowNull);
return $this;
}

Expand All @@ -71,6 +77,12 @@ public function autoIncrement(bool $autoIncrement = true): static
return $this;
}

public function check(string|null $check): static
{
$this->check = $check;
return $this;
}

public function comment(string|null $comment): static
{
$this->comment = $comment;
Expand Down Expand Up @@ -107,6 +119,11 @@ public function extra(string|null $extra): static
return $this;
}

public function getCheck(): string|null
{
return $this->check;
}

public function getComment(): string|null
{
return $this->comment;
Expand All @@ -132,21 +149,32 @@ public function getExtra(): string|null
return $this->extra;
}

/**
* @deprecated Will be removed in version 2.0.
*/
public function getName(): string|null
{
return $this->name;
}

/**
* @deprecated Use {@see getSize()} instead. Will be removed in version 2.0.
*/
public function getPrecision(): int|null
{
return $this->precision;
return $this->getSize();
}

public function getPhpType(): string
{
return PhpType::MIXED;
}

public function getReference(): ForeignKeyConstraint|null
{
return $this->reference;
}

public function getScale(): int|null
{
return $this->scale;
Expand All @@ -162,9 +190,12 @@ public function getType(): string
return $this->type;
}

/**
* @deprecated Use {@see isNotNull()} instead. Will be removed in version 2.0.
*/
public function isAllowNull(): bool
{
return $this->allowNull;
return !$this->isNotNull();
}

public function isAutoIncrement(): bool
Expand All @@ -177,11 +208,21 @@ public function isComputed(): bool
return $this->computed;
}

public function isNotNull(): bool
{
return $this->notNull;
}

public function isPrimaryKey(): bool
{
return $this->isPrimaryKey;
}

public function isUnique(): bool
{
return $this->unique;
}

public function isUnsigned(): bool
{
return $this->unsigned;
Expand All @@ -192,22 +233,27 @@ public function load(array $info): static
foreach ($info as $key => $value) {
/**
* @psalm-suppress PossiblyInvalidCast
* @psalm-suppress RiskyCast
* @psalm-suppress InvalidCast
* @psalm-suppress DeprecatedMethod
*/
match ($key) {
'allow_null' => $this->allowNull((bool) $value),
'auto_increment' => $this->autoIncrement((bool) $value),
'check' => $this->check($value !== null ? (string) $value : null),
'comment' => $this->comment($value !== null ? (string) $value : null),
'computed' => $this->computed((bool) $value),
'db_type' => $this->dbType($value !== null ? (string) $value : null),
'default_value' => $this->defaultValue($value),
'enum_values' => $this->enumValues(is_array($value) ? $value : null),
'extra' => $this->extra($value !== null ? (string) $value : null),
'name' => $this->name($value !== null ? (string) $value : null),
'not_null' => $this->notNull((bool) $value),
'primary_key' => $this->primaryKey((bool) $value),
'precision' => $this->precision($value !== null ? (int) $value : null),
'reference' => $this->reference($value instanceof ForeignKeyConstraint ? $value : null),
'scale' => $this->scale($value !== null ? (int) $value : null),
'size' => $this->size($value !== null ? (int) $value : null),
'unique' => $this->unique((bool) $value),
'unsigned' => $this->unsigned((bool) $value),
default => null,
};
Expand All @@ -216,24 +262,41 @@ public function load(array $info): static
return $this;
}

/**
* @deprecated Will be removed in version 2.0.
*/
public function name(string|null $name): static
{
$this->name = $name;
return $this;
}

public function precision(int|null $precision): static
public function notNull(bool $notNull = true): static
{
$this->precision = $precision;
$this->notNull = $notNull;
return $this;
}

/**
* @deprecated Use {@see size()} instead. Will be removed in version 2.0.
*/
public function precision(int|null $precision): static
{
return $this->size($precision);
}

public function primaryKey(bool $isPrimaryKey = true): static
{
$this->isPrimaryKey = $isPrimaryKey;
return $this;
}

public function reference(ForeignKeyConstraint|null $reference): static
{
$this->reference = $reference;
return $this;
}

public function scale(int|null $scale): static
{
$this->scale = $scale;
Expand All @@ -252,6 +315,12 @@ public function type(string $type): static
return $this;
}

public function unique(bool $unique = true): static
{
$this->unique = $unique;
return $this;
}

public function unsigned(bool $unsigned = true): static
{
$this->unsigned = $unsigned;
Expand Down
12 changes: 4 additions & 8 deletions src/Schema/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public static function primaryKey(bool $autoIncrement = true): ColumnSchemaInter
{
return static::integer()
->primaryKey()
->autoIncrement($autoIncrement)
->allowNull(false);
->autoIncrement($autoIncrement);
}

/**
Expand All @@ -32,8 +31,7 @@ public static function smallPrimaryKey(bool $autoIncrement = true): ColumnSchema
{
return static::smallint()
->primaryKey()
->autoIncrement($autoIncrement)
->allowNull(false);
->autoIncrement($autoIncrement);
}

/**
Expand All @@ -43,8 +41,7 @@ public static function bigPrimaryKey(bool $autoIncrement = true): ColumnSchemaIn
{
return static::bigint()
->primaryKey()
->autoIncrement($autoIncrement)
->allowNull(false);
->autoIncrement($autoIncrement);
}

/**
Expand All @@ -54,8 +51,7 @@ public static function uuidPrimaryKey(bool $autoIncrement = false): ColumnSchema
{
return static::uuid()
->primaryKey()
->autoIncrement($autoIncrement)
->allowNull(false);
->autoIncrement($autoIncrement);
}

// Abstract type column builders
Expand Down
Loading