Skip to content

Commit

Permalink
Deprecated usage of DB-generated UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jul 11, 2018
1 parent 29f1799 commit 8396913
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 9 deletions.
8 changes: 8 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Upgrade to 2.8

## Deprecated usage of DB-generated UUIDs

The format of DB-generated UUIDs is inconsistent across supported platforms and therefore is not portable. Some of the platforms produce UUIDv1, some produce UUIDv4, some produce the values which are not even UUID.

Unless UUIDs are used in stored procedures which DBAL doesn't support, there's no real benefit of DB-generated UUIDs comparing to the application-generated ones.

Use a PHP library (e.g. [ramsey/uuid](https://packagist.org/packages/ramsey/uuid)) to generate UUIDs on the application side.

## 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.
Expand Down
18 changes: 10 additions & 8 deletions docs/en/reference/sharding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ platforms:
<?php
use Doctrine\DBAL\DriverManager;
use Ramsey\Uuid\Uuid;
$conn = DriverManager::getConnection(/**..**/);
$guid = $conn->fetchColumn('SELECT ' . $conn->getDatabasePlatform()->getGuidExpression());
$guid = Uuid::uuid1();
$conn->insert("my_table", array("id" => $guid, "foo" => "bar"));
$conn->insert('my_table', [
'id' => $guid->toString(),
'foo' => 'bar',
]);
In your application you should hide this details in Id-Generation services:

Expand All @@ -113,15 +117,13 @@ In your application you should hide this details in Id-Generation services:
<?php
namespace MyApplication;
use Ramsey\Uuid\Uuid;
class IdGenerationService
{
private $conn;
public function generateCustomerId()
public function generateCustomerId() : Uuid
{
return $this->conn->fetchColumn('SELECT ' .
$this->conn->getDatabasePlatform()->getGuidExpression()
);
return Uuid::uuid1();
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,13 @@ public function getRegexpExpression()
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*
* @deprecated Use application-generated UUIDs instead
*/
public function getGuidExpression()
{
@trigger_error(__METHOD__ . '() is deprecated, use application-generated UUIDs instead.', E_USER_DEPRECATED);

throw DBALException::notSupported(__METHOD__);
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/DBAL/Platforms/DrizzlePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BinaryType;
use const E_USER_DEPRECATED;
use function array_merge;
use function array_unique;
use function array_values;
Expand All @@ -36,6 +37,7 @@
use function is_string;
use function join;
use function sprintf;
use function trigger_error;
use function trim;

/**
Expand Down Expand Up @@ -621,9 +623,13 @@ public function getLocateExpression($str, $substr, $startPos = false)

/**
* {@inheritDoc}
*
* @deprecated Use application-generated UUIDs instead
*/
public function getGuidExpression()
{
@trigger_error(__METHOD__ . '() is deprecated, use application-generated UUIDs instead.', E_USER_DEPRECATED);

return 'UUID()';
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\TextType;
use const E_USER_DEPRECATED;
use function array_diff_key;
use function array_merge;
use function array_unique;
Expand All @@ -40,6 +41,7 @@
use function sprintf;
use function str_replace;
use function strtoupper;
use function trigger_error;
use function trim;

/**
Expand Down Expand Up @@ -99,9 +101,13 @@ public function getRegexpExpression()

/**
* {@inheritDoc}
*
* @deprecated Use application-generated UUIDs instead
*/
public function getGuidExpression()
{
@trigger_error(__METHOD__ . '() is deprecated, use application-generated UUIDs instead.', E_USER_DEPRECATED);

return 'UUID()';
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\BinaryType;
use const E_USER_DEPRECATED;
use function array_merge;
use function count;
use function explode;
Expand All @@ -41,6 +42,7 @@
use function strpos;
use function strtoupper;
use function substr;
use function trigger_error;

/**
* OraclePlatform.
Expand Down Expand Up @@ -108,9 +110,13 @@ public function getLocateExpression($str, $substr, $startPos = false)

/**
* {@inheritDoc}
*
* @deprecated Use application-generated UUIDs instead
*/
public function getGuidExpression()
{
@trigger_error(__METHOD__ . '() is deprecated, use application-generated UUIDs instead.', E_USER_DEPRECATED);

return 'SYS_GUID()';
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Types\Type;
use const E_USER_DEPRECATED;
use function array_diff;
use function array_merge;
use function array_unique;
Expand All @@ -45,6 +46,7 @@
use function str_replace;
use function strpos;
use function strtolower;
use function trigger_error;
use function trim;

/**
Expand Down Expand Up @@ -1016,9 +1018,13 @@ public function getTimeTypeDeclarationSQL(array $fieldDeclaration)

/**
* {@inheritDoc}
*
* @deprecated Use application-generated UUIDs instead
*/
public function getGuidExpression()
{
@trigger_error(__METHOD__ . '() is deprecated, use application-generated UUIDs instead.', E_USER_DEPRECATED);

return 'UUID_GENERATE_V4()';
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use const E_USER_DEPRECATED;
use function array_merge;
use function array_unique;
use function array_values;
Expand All @@ -44,6 +45,7 @@
use function strpos;
use function strtoupper;
use function substr;
use function trigger_error;

/**
* The SQLAnywherePlatform provides the behavior, features and SQL dialect of the
Expand Down Expand Up @@ -690,9 +692,13 @@ public function getForUpdateSQL()

/**
* {@inheritdoc}
*
* @deprecated Use application-generated UUIDs instead
*/
public function getGuidExpression()
{
@trigger_error(__METHOD__ . '() is deprecated, use application-generated UUIDs instead.', E_USER_DEPRECATED);

return 'NEWID()';
}

Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types;
use const E_USER_DEPRECATED;
use function array_merge;
use function array_unique;
use function array_values;
Expand All @@ -39,7 +40,6 @@
use function implode;
use function is_array;
use function is_bool;
use function is_null;
use function is_numeric;
use function is_string;
use function preg_match;
Expand All @@ -53,6 +53,7 @@
use function strtoupper;
use function substr;
use function substr_count;
use function trigger_error;

/**
* The SQLServerPlatform provides the behavior, features and SQL dialect of the
Expand Down Expand Up @@ -1026,9 +1027,13 @@ public function getDropViewSQL($name)

/**
* {@inheritDoc}
*
* @deprecated Use application-generated UUIDs instead
*/
public function getGuidExpression()
{
@trigger_error(__METHOD__ . '() is deprecated, use application-generated UUIDs instead.', E_USER_DEPRECATED);

return 'NEWID()';
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types;
use const E_USER_DEPRECATED;
use function array_merge;
use function array_unique;
use function array_values;
Expand All @@ -40,6 +41,7 @@
use function strlen;
use function strpos;
use function strtolower;
use function trigger_error;

/**
* The SqlitePlatform class describes the specifics and dialects of the SQLite
Expand All @@ -63,9 +65,13 @@ public function getRegexpExpression()

/**
* {@inheritDoc}
*
* @deprecated Use application-generated UUIDs instead
*/
public function getGuidExpression()
{
@trigger_error(__METHOD__ . '() is deprecated, use application-generated UUIDs instead.', E_USER_DEPRECATED);

return "HEX(RANDOMBLOB(4)) || '-' || HEX(RANDOMBLOB(2)) || '-4' || "
. "SUBSTR(HEX(RANDOMBLOB(2)), 2) || '-' || "
. "SUBSTR('89AB', 1 + (ABS(RANDOM()) % 4), 1) || "
Expand Down
7 changes: 7 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL421Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ protected function setUp()
}
}

/**
* @group legacy
* @expectedDeprecation %s::getGuidExpression() is deprecated, use application-generated UUIDs instead.
*/
public function testGuidShouldMatchPattern()
{
$guid = $this->_conn->query($this->getSelectGuidSql())->fetchColumn();
Expand All @@ -29,6 +33,9 @@ public function testGuidShouldMatchPattern()
/**
* This test does (of course) not proof that all generated GUIDs are
* random, it should however provide some basic confidence.
*
* @group legacy
* @expectedDeprecation %s::getGuidExpression() is deprecated, use application-generated UUIDs instead.
*/
public function testGuidShouldBeRandom()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ public function testCannotGenerateDropIndexSQLWithInvalidTableParameter()
$this->_platform->getDropIndexSQL('index', array('table'));
}

/**
* @group legacy
* @expectedDeprecation Doctrine\DBAL\Platforms\SQLAnywherePlatform::getGuidExpression() is deprecated, use application-generated UUIDs instead.
*/
public function testGeneratesSQLSnippets()
{
self::assertEquals('STRING(column1, "string1", column2, "string2")', $this->_platform->getConcatExpression(
Expand Down

0 comments on commit 8396913

Please sign in to comment.