Skip to content

Commit

Permalink
Merge branch '3.9.x' into 4.1.x
Browse files Browse the repository at this point in the history
* 3.9.x:
  fix #6198:  stop considering generated column definition as being its default value (#6199)
  Fix condition on Ascii String for SQL Server (#6389)
  Add DBTypes that are missing for TypeMapping (#6463)
  • Loading branch information
derrabus committed Aug 14, 2024
2 parents 6ef1518 + 5f43767 commit c4a415f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 10 deletions.
23 changes: 23 additions & 0 deletions src/Driver/AbstractPostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,39 @@
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\API\PostgreSQL\ExceptionConverter;
use Doctrine\DBAL\Platforms\Exception\InvalidPlatformVersion;
use Doctrine\DBAL\Platforms\PostgreSQL120Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\ServerVersionProvider;

use function preg_match;
use function version_compare;

/**
* Abstract base implementation of the {@see Driver} interface for PostgreSQL based drivers.
*/
abstract class AbstractPostgreSQLDriver implements Driver
{
public function getDatabasePlatform(ServerVersionProvider $versionProvider): PostgreSQLPlatform
{
$version = $versionProvider->getServerVersion();

if (preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts) === 0) {
throw InvalidPlatformVersion::new(
$version,
'<major_version>.<minor_version>.<patch_version>',
);

Check warning on line 31 in src/Driver/AbstractPostgreSQLDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/AbstractPostgreSQLDriver.php#L28-L31

Added lines #L28 - L31 were not covered by tests
}

$majorVersion = $versionParts['major'];
$minorVersion = $versionParts['minor'] ?? 0;
$patchVersion = $versionParts['patch'] ?? 0;
$version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;

if (version_compare($version, '12.0', '>=')) {
return new PostgreSQL120Platform();
}

return new PostgreSQLPlatform();
}

Expand Down
30 changes: 30 additions & 0 deletions src/Platforms/PostgreSQL120Platform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Platforms;

/**
* Provides the behavior, features and SQL dialect of the PostgreSQL 12.0 database platform.
*/
class PostgreSQL120Platform extends PostgreSQLPlatform
{
public function getDefaultColumnValueSQLSnippet(): string
{
// in case of GENERATED ALWAYS AS (foobar) STORED column (added in PostgreSQL 12.0)
// PostgreSQL's pg_get_expr(adbin, adrelid) will return the 'foobar' part
// which is not the 'default' value of the column but its 'definition'
// so in that case we force it to NULL as DBAL will use that column only for the
// 'default' value
return <<<'SQL'
SELECT
CASE
WHEN a.attgenerated = 's' THEN NULL
ELSE pg_get_expr(adbin, adrelid)
END
FROM pg_attrdef
WHERE c.oid = pg_attrdef.adrelid
AND pg_attrdef.adnum=a.attnum
SQL;
}
}
13 changes: 13 additions & 0 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,19 @@ public function getTruncateTableSQL(string $tableName, bool $cascade = false): s
return $sql;
}

/**
* Get the snippet used to retrieve the default value for a given column
*/
public function getDefaultColumnValueSQLSnippet(): string
{
return <<<'SQL'
SELECT pg_get_expr(adbin, adrelid)
FROM pg_attrdef
WHERE c.oid = pg_attrdef.adrelid
AND pg_attrdef.adnum=a.attnum
SQL;
}

protected function initializeDoctrineTypeMappings(): void
{
$this->doctrineTypeMapping = [
Expand Down
2 changes: 2 additions & 0 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,12 +1080,14 @@ protected function initializeDoctrineTypeMappings(): void
'smalldatetime' => Types::DATETIME_MUTABLE,
'smallint' => Types::SMALLINT,
'smallmoney' => Types::INTEGER,
'sysname' => Types::STRING,
'text' => Types::TEXT,
'time' => Types::TIME_MUTABLE,
'tinyint' => Types::SMALLINT,
'uniqueidentifier' => Types::GUID,
'varbinary' => Types::BINARY,
'varchar' => Types::STRING,
'xml' => Types::TEXT,
];
}

Expand Down
14 changes: 5 additions & 9 deletions src/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,13 @@ protected function selectTableNames(string $databaseName): Result

protected function selectTableColumns(string $databaseName, ?string $tableName = null): Result
{
$sql = 'SELECT';
$sql = 'SELECT ';

Check warning on line 414 in src/Schema/PostgreSQLSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/PostgreSQLSchemaManager.php#L414

Added line #L414 was not covered by tests

if ($tableName === null) {
$sql .= ' c.relname AS table_name, n.nspname AS schema_name,';
$sql .= 'c.relname AS table_name, n.nspname AS schema_name,';

Check warning on line 417 in src/Schema/PostgreSQLSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/PostgreSQLSchemaManager.php#L417

Added line #L417 was not covered by tests
}

$sql .= <<<'SQL'
$sql .= sprintf(<<<'SQL'

Check warning on line 420 in src/Schema/PostgreSQLSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/PostgreSQLSchemaManager.php#L420

Added line #L420 was not covered by tests
a.attnum,
quote_ident(a.attname) AS field,
t.typname AS type,
Expand All @@ -434,11 +434,7 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
AND pg_index.indkey[0] = a.attnum
AND pg_index.indisprimary = 't'
) AS pri,
(SELECT pg_get_expr(adbin, adrelid)
FROM pg_attrdef
WHERE c.oid = pg_attrdef.adrelid
AND pg_attrdef.adnum=a.attnum
) AS default,
(%s) AS default,
(SELECT pg_description.description
FROM pg_description WHERE pg_description.objoid = c.oid AND a.attnum = pg_description.objsubid
) AS comment
Expand All @@ -453,7 +449,7 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
ON d.objid = c.oid
AND d.deptype = 'e'
AND d.classid = (SELECT oid FROM pg_class WHERE relname = 'pg_class')
SQL;
SQL, $this->platform->getDefaultColumnValueSQLSnippet());

Check warning on line 452 in src/Schema/PostgreSQLSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/PostgreSQLSchemaManager.php#L452

Added line #L452 was not covered by tests

$conditions = array_merge([
'a.attnum > 0',
Expand Down
4 changes: 3 additions & 1 deletion tests/Driver/VersionAwarePlatformDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Platforms\MySQL84Platform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL120Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -69,7 +70,8 @@ public static function postgreSQLVersionProvider(): array
return [
['10.0', PostgreSQLPlatform::class],
['11.0', PostgreSQLPlatform::class],
['13.3', PostgreSQLPlatform::class],
['12.0', PostgreSQL120Platform::class],
['13.3', PostgreSQL120Platform::class],
];
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\DBAL\Tests\Functional\Schema;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL120Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Schema;
Expand Down Expand Up @@ -290,6 +291,31 @@ public function testBooleanDefault(): void
);
}

public function testGeneratedColumn(): void
{
if (! $this->connection->getDatabasePlatform() instanceof PostgreSQL120Platform) {
self::markTestSkipped('Generated columns are not supported in Postgres 11 and earlier');
}

$table = new Table('ddc6198_generated_always_as');
$table->addColumn('id', Types::INTEGER);
$table->addColumn(
'idIsOdd',
Types::BOOLEAN,
['columnDefinition' => 'boolean GENERATED ALWAYS AS (id % 2 = 1) STORED', 'notNull' => false],
);

$this->dropAndCreateTable($table);

$databaseTable = $this->schemaManager->introspectTable($table->getName());

self::assertTrue(
$this->schemaManager->createComparator()
->compareTables($table, $databaseTable)
->isEmpty(),
);
}

/**
* PostgreSQL stores BINARY columns as BLOB
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/Platforms/SQLServerPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ public function testGeneratesTypeDeclarationsForStrings(): void
);
}

public function testGeneratesTypeDeclarationsForAsciiStrings(): void
{
self::assertEquals(
'CHAR(10)',
$this->platform->getAsciiStringTypeDeclarationSQL(
['length' => 10, 'fixed' => true],
),
);
self::assertEquals(
'VARCHAR(50)',
$this->platform->getAsciiStringTypeDeclarationSQL(['length' => 50]),
);
self::assertEquals(
'VARCHAR(50)',
$this->platform->getAsciiStringTypeDeclarationSQL(
['length' => 50, 'fixed' => false],
),
);
}

public function testSupportsIdentityColumns(): void
{
self::assertTrue($this->platform->supportsIdentityColumns());
Expand Down Expand Up @@ -777,6 +797,12 @@ public function testInitializesDoctrineTypeMappings(): void

self::assertTrue($this->platform->hasDoctrineTypeMappingFor('uniqueidentifier'));
self::assertSame(Types::GUID, $this->platform->getDoctrineTypeMapping('uniqueidentifier'));

self::assertTrue($this->platform->hasDoctrineTypeMappingFor('sysname'));
self::assertSame(Types::STRING, $this->platform->getDoctrineTypeMapping('sysname'));

self::assertTrue($this->platform->hasDoctrineTypeMappingFor('xml'));
self::assertSame(Types::TEXT, $this->platform->getDoctrineTypeMapping('xml'));
}

protected function getExpectedFixedLengthStringTypeDeclarationSQLNoLength(): string
Expand Down

0 comments on commit c4a415f

Please sign in to comment.