Skip to content

Commit

Permalink
Merge branch '3.8.x' into 4.0.x
Browse files Browse the repository at this point in the history
* 3.8.x:
  Deprecate AbstractMySQLPlatform::getColumnTypeSQLSnippets() (doctrine#6213)
  Fix MariaDB list columns performance (doctrine#6202)
  • Loading branch information
derrabus committed Nov 5, 2023
2 parents 6b97ee3 + 39fe3b4 commit b8c6007
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,11 @@ The following methods have been removed.

# Upgrade to 3.8

## Deprecated `AbstractMySQLPlatform::getColumnTypeSQLSnippets()`

`AbstractMySQLPlatform::getColumnTypeSQLSnippets()` has been deprecated.
Use `AbstractMySQLPlatform::getColumnTypeSQLSnippet()` instead.

## Deprecated reset methods from `QueryBuilder`

`QueryBuilder::resetQueryParts()` has been deprecated.
Expand Down
5 changes: 5 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
</ConflictingReferenceConstraint>
<DeprecatedMethod>
<errorLevel type="suppress">
<!--
See https://github.com/doctrine/dbal/pull/6202
TODO: remove in 4.0.0
-->
<referencedMethod name="Doctrine\DBAL\Platforms\AbstractMySQLPlatform::getColumnTypeSQLSnippets" />
<!-- TODO for PHPUnit 11 -->
<referencedMethod name="PHPUnit\Framework\TestCase::iniSet"/>
</errorLevel>
Expand Down
23 changes: 22 additions & 1 deletion src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\Types;
use Doctrine\Deprecations\Deprecation;

use function array_merge;
use function array_unique;
use function array_values;
use function count;
use function func_get_args;
use function implode;
use function in_array;
use function is_numeric;
Expand Down Expand Up @@ -208,6 +210,8 @@ public function supportsColumnCollation(): bool
}

/**
* @deprecated Use {@see getColumnTypeSQLSnippet()} instead.
*
* The SQL snippets required to elucidate a column type
*
* Returns an array of the form [column type SELECT snippet, additional JOIN statement snippet]
Expand All @@ -216,7 +220,24 @@ public function supportsColumnCollation(): bool
*/
public function getColumnTypeSQLSnippets(string $tableAlias = 'c'): array
{
return [$tableAlias . '.COLUMN_TYPE', ''];
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/6202',
'AbstractMySQLPlatform::getColumnTypeSQLSnippets() is deprecated. '
. 'Use AbstractMySQLPlatform::getColumnTypeSQLSnippet() instead.',
);

return [$this->getColumnTypeSQLSnippet(...func_get_args()), ''];
}

/**
* The SQL snippet required to elucidate a column type
*
* Returns a column type SELECT snippet string
*/
public function getColumnTypeSQLSnippet(string $tableAlias = 'c', ?string $databaseName = null): string
{
return $tableAlias . '.COLUMN_TYPE';
}

/**
Expand Down
41 changes: 23 additions & 18 deletions src/Platforms/MariaDBPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,39 @@ public function getJsonTypeDeclarationSQL(array $column): string
* is valid json. This function generates the SQL snippets which reverse this aliasing i.e. report a column
* as JSON where it was originally specified as such instead of LONGTEXT.
*
* The CHECK constraints are stored in information_schema.CHECK_CONSTRAINTS so JOIN that table.
*
* @return array{string, string}
* The CHECK constraints are stored in information_schema.CHECK_CONSTRAINTS so query that table.
*/
public function getColumnTypeSQLSnippets(string $tableAlias = 'c'): array
public function getColumnTypeSQLSnippet(string $tableAlias = 'c', ?string $databaseName = null): string
{
if ($this->getJsonTypeDeclarationSQL([]) !== 'JSON') {
return parent::getColumnTypeSQLSnippets($tableAlias);
}
$databaseName = $this->getDatabaseNameSQL($databaseName);

$columnTypeSQL = <<<SQL
// The check for `CONSTRAINT_SCHEMA = $databaseName` is mandatory here to prevent performance issues
return <<<SQL
IF(
x.CHECK_CLAUSE IS NOT NULL AND $tableAlias.COLUMN_TYPE = 'longtext',
$tableAlias.COLUMN_TYPE = 'longtext'
AND EXISTS(
SELECT * from information_schema.CHECK_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = $databaseName
AND TABLE_NAME = $tableAlias.TABLE_NAME
AND CHECK_CLAUSE = CONCAT(
'json_valid(`',
$tableAlias.COLUMN_NAME,
'`)'
)
),
'json',
$tableAlias.COLUMN_TYPE
)
SQL;
}

$joinCheckConstraintSQL = <<<SQL
LEFT JOIN information_schema.CHECK_CONSTRAINTS x
ON (
$tableAlias.TABLE_SCHEMA = x.CONSTRAINT_SCHEMA
AND $tableAlias.TABLE_NAME = x.TABLE_NAME
AND x.CHECK_CLAUSE = CONCAT('json_valid(`', $tableAlias.COLUMN_NAME , '`)')
)
SQL;
private function getDatabaseNameSQL(?string $databaseName): string
{
if ($databaseName !== null) {
return $this->quoteStringLiteral($databaseName);
}

return [$columnTypeSQL, $joinCheckConstraintSQL];
return $this->getCurrentDatabaseExpression();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Schema/MySQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ protected function selectTableNames(string $databaseName): Result

protected function selectTableColumns(string $databaseName, ?string $tableName = null): Result
{
[$columnTypeSQL, $joinCheckConstraintSQL] = $this->platform->getColumnTypeSQLSnippets();
// @todo 4.0 - call getColumnTypeSQLSnippet() instead
[$columnTypeSQL, $joinCheckConstraintSQL] = $this->platform->getColumnTypeSQLSnippets('c', $databaseName);

$sql = 'SELECT';

Expand Down
12 changes: 12 additions & 0 deletions tests/Functional/Schema/MySQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\DBAL\Tests\Functional\Schema\MySQL\PointType;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\JsonType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;

Expand Down Expand Up @@ -387,6 +388,17 @@ public function testListFloatTypeColumns(): void
self::assertTrue($columns['col_unsigned']->getUnsigned());
}

public function testJsonColumnType(): void
{
$table = new Table('test_mysql_json');
$table->addColumn('col_json', Types::JSON);
$this->dropAndCreateTable($table);

$columns = $this->schemaManager->listTableColumns('test_mysql_json');

self::assertInstanceOf(JsonType::class, $columns['col_json']->getType());
}

public function testColumnDefaultCurrentTimestamp(): void
{
$platform = $this->connection->getDatabasePlatform();
Expand Down

0 comments on commit b8c6007

Please sign in to comment.