Skip to content

Commit

Permalink
Update according changes in ColumnSchemaInterface (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 1, 2024
1 parent eb4e5ef commit a7288c5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 60 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
COMPOSER_ROOT_VERSION: 1.0.0
EXTENSIONS: pdo, pdo_sqlsrv-5.10.1

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

strategy:
matrix:
Expand All @@ -42,7 +42,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 @@ -13,6 +13,7 @@
- Enh #316: Implement `ColumnFactory` class (@Tigrov)
- Enh #319: Separate column type constants (@Tigrov)
- Enh #320: Realize `ColumnBuilder` class (@Tigrov)
- Enh #321: Update according changes in `ColumnSchemaInterface` (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
4 changes: 2 additions & 2 deletions src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function insertWithReturningPks(string $table, QueryInterface|array $colu
if (in_array($dbType, ['char', 'varchar', 'nchar', 'nvarchar', 'binary', 'varbinary'], true)) {
$dbType .= '(MAX)';
} elseif ($dbType === 'timestamp') {
$dbType = $returnColumn->isAllowNull() ? 'varbinary(8)' : 'binary(8)';
$dbType = $returnColumn->isNotNull() ? 'binary(8)' : 'varbinary(8)';
}

$quotedName = $this->quoter->quoteColumnName($columnName);
$createdCols[] = $quotedName . ' ' . (string) $dbType . ' ' . ($returnColumn->isAllowNull() ? 'NULL' : '');
$createdCols[] = $quotedName . ' ' . (string) $dbType . ' ' . ($returnColumn->isNotNull() ? '' : 'NULL');
$insertedCols[] = 'INSERTED.' . $quotedName;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
* is_computed: string,
* comment: null|string,
* size?: int,
* precision?: int,
* scale?: int,
* }
* @psalm-type ConstraintArray = array<
Expand Down Expand Up @@ -364,11 +363,10 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
$dbType = $info['data_type'];
/** @psalm-var ColumnArray $info */
$column = $columnFactory->fromDefinition($dbType);
/** @psalm-suppress DeprecatedMethod */
$column->name($info['column_name']);
$column->allowNull($info['is_nullable'] === 'YES');
$column->notNull($info['is_nullable'] !== 'YES');
$column->dbType($dbType);
$column->enumValues([]); // MSSQL has only vague equivalents to enum.
$column->primaryKey(false); // The primary key will be determined in the `findColumns()` method.
$column->autoIncrement($info['is_identity'] === '1');
$column->computed($info['is_computed'] === '1');
$column->comment($info['comment'] ?? '');
Expand Down
2 changes: 1 addition & 1 deletion tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public function testAlterColumnWithDefaultNull()

$fieldCol = $db->getTableSchema('column_with_constraint', true)->getColumn('field');

$this->assertFalse($fieldCol->isAllowNull());
$this->assertTrue($fieldCol->isNotNull());
$this->assertNull($fieldCol->getDefaultValue());
$this->assertSame('nvarchar(40)', $fieldCol->getDbType());

Expand Down
80 changes: 32 additions & 48 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ public static function columns(): array
'dbType' => 'int',
'phpType' => 'int',
'primaryKey' => false,
'allowNull' => false,
'notNull' => true,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => null,
],
Expand All @@ -32,11 +31,10 @@ public static function columns(): array
'dbType' => 'int',
'phpType' => 'int',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => 1,
],
Expand All @@ -45,11 +43,10 @@ public static function columns(): array
'dbType' => 'tinyint',
'phpType' => 'int',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => 1,
],
Expand All @@ -58,11 +55,10 @@ public static function columns(): array
'dbType' => 'smallint',
'phpType' => 'int',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => 1,
],
Expand All @@ -71,11 +67,10 @@ public static function columns(): array
'dbType' => 'char(100)',
'phpType' => 'string',
'primaryKey' => false,
'allowNull' => false,
'notNull' => true,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => 100,
'precision' => 100,
'scale' => null,
'defaultValue' => null,
],
Expand All @@ -84,11 +79,10 @@ public static function columns(): array
'dbType' => 'varchar(100)',
'phpType' => 'string',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => 100,
'precision' => 100,
'scale' => null,
'defaultValue' => 'something',
],
Expand All @@ -97,11 +91,10 @@ public static function columns(): array
'dbType' => 'text',
'phpType' => 'string',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => null,
],
Expand All @@ -110,11 +103,10 @@ public static function columns(): array
'dbType' => 'decimal(4,3)',
'phpType' => 'float',
'primaryKey' => false,
'allowNull' => false,
'notNull' => true,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => 4,
'precision' => 4,
'scale' => 3,
'defaultValue' => null,
],
Expand All @@ -123,11 +115,10 @@ public static function columns(): array
'dbType' => 'float',
'phpType' => 'float',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => 1.23,
],
Expand All @@ -136,11 +127,10 @@ public static function columns(): array
'dbType' => 'varbinary',
'phpType' => 'mixed',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => null,
],
Expand All @@ -149,11 +139,10 @@ public static function columns(): array
'dbType' => 'decimal(5,2)',
'phpType' => 'float',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => 5,
'precision' => 5,
'scale' => 2,
'defaultValue' => 33.22,
],
Expand All @@ -162,11 +151,10 @@ public static function columns(): array
'dbType' => 'datetime',
'phpType' => 'string',
'primaryKey' => false,
'allowNull' => false,
'notNull' => true,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => '2002-01-01 00:00:00',
],
Expand All @@ -175,11 +163,10 @@ public static function columns(): array
'dbType' => 'bit',
'phpType' => 'bool',
'primaryKey' => false,
'allowNull' => false,
'notNull' => true,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => null,
],
Expand All @@ -188,11 +175,10 @@ public static function columns(): array
'dbType' => 'bit',
'phpType' => 'bool',
'primaryKey' => false,
'allowNull' => true,
'notNull' => false,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => true,
],
Expand All @@ -206,11 +192,10 @@ public static function columns(): array
'dbType' => 'int',
'phpType' => 'int',
'primaryKey' => true,
'allowNull' => false,
'notNull' => true,
'autoIncrement' => true,
'enumValues' => [],
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => null,
],
Expand All @@ -219,11 +204,10 @@ public static function columns(): array
'dbType' => 'varchar(255)',
'phpType' => 'string',
'primaryKey' => false,
'allowNull' => false,
'notNull' => true,
'autoIncrement' => false,
'enumValues' => [],
'enumValues' => null,
'size' => 255,
'precision' => 255,
'scale' => null,
'defaultValue' => null,
],
Expand Down
6 changes: 3 additions & 3 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ public function testAlterColumnOnDb(): void
$schema = $db->getTableSchema('[foo1]', true);

$this->assertSame('varchar(255)', $schema?->getColumn('bar')->getDbType());
$this->assertSame(true, $schema?->getColumn('bar')->isAllowNull());
$this->assertFalse($schema?->getColumn('bar')->isNotNull());

$sql = $db->getQueryBuilder()->alterColumn(
'foo1',
Expand All @@ -858,7 +858,7 @@ public function testAlterColumnOnDb(): void
$schema = $db->getTableSchema('[foo1]', true);

$this->assertSame('nvarchar(128)', $schema?->getColumn('bar')->getDbType());
$this->assertSame(false, $schema?->getColumn('bar')->isAllowNull());
$this->assertTrue($schema?->getColumn('bar')->isNotNull());

$sql = $db->getQueryBuilder()->alterColumn(
'foo1',
Expand Down Expand Up @@ -888,7 +888,7 @@ public function testAlterColumnWithCheckConstraintOnDb(): void
$db->createCommand($sql)->execute();
$schema = $db->getTableSchema('[foo1]', true);
$this->assertEquals('nvarchar(128)', $schema?->getColumn('bar')->getDbType());
$this->assertEquals(true, $schema?->getColumn('bar')->isAllowNull());
$this->assertFalse($schema?->getColumn('bar')->isNotNull());

$sql = "INSERT INTO [foo1]([bar]) values('abcdef')";
$this->assertEquals(1, $db->createCommand($sql)->execute());
Expand Down

0 comments on commit a7288c5

Please sign in to comment.