From a64d7481a7307eb39c8521198aa581b411d7acf5 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Mon, 18 Jun 2018 21:45:17 -0700 Subject: [PATCH] Deprecated usage of binary fields whose length exceeds the platform maximum Closes #3187. --- UPGRADE.md | 5 ++++ .../DBAL/Platforms/AbstractPlatform.php | 14 ++++++++++- .../AbstractMySQLPlatformTestCase.php | 23 ++++++++++++++----- .../Platforms/AbstractPlatformTestCase.php | 5 ++++ .../AbstractSQLServerPlatformTestCase.php | 12 ++++++++-- .../Tests/DBAL/Platforms/DB2PlatformTest.php | 10 +++++++- .../DBAL/Platforms/OraclePlatformTest.php | 12 ++++++++-- .../Platforms/SQLAnywherePlatformTest.php | 12 ++++++++-- 8 files changed, 79 insertions(+), 14 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index d718cf657f1..dce497856fd 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,10 @@ # Upgrade to 2.8 +## Deprecated usage of binary fields whose length exceeds the platform maximum + +- The usage of binary fields whose length exceeds the maximum field size on a given platform is deprecated. + Use binary fields of a size which fits all target platforms, or use blob explicitly instead. + ## Removed dependency on doctrine/common The dependency on doctrine/common package has been removed. diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index 402c92cc258..a4b1ea434f3 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -42,6 +42,7 @@ use Doctrine\DBAL\TransactionIsolationLevel; use Doctrine\DBAL\Types; use Doctrine\DBAL\Types\Type; +use const E_USER_DEPRECATED; use function addcslashes; use function array_map; use function array_merge; @@ -68,6 +69,7 @@ use function strpos; use function strtolower; use function strtoupper; +use function trigger_error; /** * Base class for all DatabasePlatforms. The DatabasePlatforms are the central @@ -325,7 +327,17 @@ public function getBinaryTypeDeclarationSQL(array $field) $fixed = $field['fixed'] ?? false; - if ($field['length'] > $this->getBinaryMaxLength()) { + $maxLength = $this->getBinaryMaxLength(); + + if ($field['length'] > $maxLength) { + if ($maxLength > 0) { + @trigger_error(sprintf( + 'Binary field length %d is greater than supported by the platform (%d)', + $field['length'], + $maxLength + ), E_USER_DEPRECATED); + } + return $this->getBlobTypeDeclarationSQL($field); } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php index 0c1fdcbee7c..59a74474865 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php @@ -519,16 +519,27 @@ public function testReturnsBinaryTypeDeclarationSQL() self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array())); self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0))); self::assertSame('VARBINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 65535))); - self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 65536))); - self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 16777215))); - self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 16777216))); self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true))); self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0))); self::assertSame('BINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 65535))); - self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 65536))); - self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 16777215))); - self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 16777216))); + } + + /** + * @group legacy + * @expectedDeprecation Binary field length 65536 is greater than supported by the platform (65535) + * @expectedDeprecation Binary field length 16777215 is greater than supported by the platform (65535) + * @expectedDeprecation Binary field length 16777216 is greater than supported by the platform (65535) + */ + public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL() + { + self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 65536])); + self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 16777215])); + self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 16777216])); + + self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 65536])); + self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 16777215])); + self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 16777216])); } public function testDoesNotPropagateForeignKeyCreationForNonSupportingEngines() diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php index f8d13034c9f..a0b0ea33741 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php @@ -830,6 +830,11 @@ public function testReturnsBinaryTypeDeclarationSQL() $this->_platform->getBinaryTypeDeclarationSQL(array()); } + public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL() + { + $this->markTestSkipped('Not applicable to the platform'); + } + /** * @group DBAL-553 */ diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php index 206c571828b..c7dc9b52413 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php @@ -981,12 +981,20 @@ public function testReturnsBinaryTypeDeclarationSQL() self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array())); self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0))); self::assertSame('VARBINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 8000))); - self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 8001))); self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true))); self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0))); self::assertSame('BINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8000))); - self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8001))); + } + + /** + * @group legacy + * @expectedDeprecation Binary field length 8001 is greater than supported by the platform (8000) + */ + public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL() + { + self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 8001])); + self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 8001])); } /** diff --git a/tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php index 52902f8de94..fa68335d088 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php @@ -422,10 +422,18 @@ public function testReturnsBinaryTypeDeclarationSQL() self::assertSame('VARCHAR(1) FOR BIT DATA', $this->_platform->getBinaryTypeDeclarationSQL([])); self::assertSame('VARCHAR(255) FOR BIT DATA', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 0])); self::assertSame('VARCHAR(32704) FOR BIT DATA', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 32704])); - self::assertSame('BLOB(1M)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 32705])); self::assertSame('CHAR(1) FOR BIT DATA', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true])); self::assertSame('CHAR(254) FOR BIT DATA', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 0])); + } + + /** + * @group legacy + * @expectedDeprecation Binary field length 32705 is greater than supported by the platform (32704) + */ + public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL() + { + self::assertSame('BLOB(1M)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 32705])); self::assertSame('BLOB(1M)', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 32705])); } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php index 91c1f33250d..c5c6429f812 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php @@ -446,12 +446,20 @@ public function testReturnsBinaryTypeDeclarationSQL() self::assertSame('RAW(255)', $this->_platform->getBinaryTypeDeclarationSQL(array())); self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0))); self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 2000))); - self::assertSame('BLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 2001))); self::assertSame('RAW(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true))); self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0))); self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 2000))); - self::assertSame('BLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 2001))); + } + + /** + * @group legacy + * @expectedDeprecation Binary field length 2001 is greater than supported by the platform (2000) + */ + public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL() + { + self::assertSame('BLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 2001])); + self::assertSame('BLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 2001])); } public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType() diff --git a/tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php index 29af76826ae..f35e85b3117 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php @@ -791,12 +791,20 @@ public function testReturnsBinaryTypeDeclarationSQL() self::assertSame('VARBINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(array())); self::assertSame('VARBINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0))); self::assertSame('VARBINARY(32767)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 32767))); - self::assertSame('LONG BINARY', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 32768))); self::assertSame('BINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true))); self::assertSame('BINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0))); self::assertSame('BINARY(32767)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 32767))); - self::assertSame('LONG BINARY', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 32768))); + } + + /** + * @group legacy + * @expectedDeprecation Binary field length 32768 is greater than supported by the platform (32767) + */ + public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL() + { + self::assertSame('LONG BINARY', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 32768])); + self::assertSame('LONG BINARY', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 32768])); } /**