diff --git a/src/Column/ColumnDefinitionBuilder.php b/src/Column/ColumnDefinitionBuilder.php index 627faa77..f0ba4a2b 100644 --- a/src/Column/ColumnDefinitionBuilder.php +++ b/src/Column/ColumnDefinitionBuilder.php @@ -8,10 +8,10 @@ use Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; +use function version_compare; + final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder { - protected const GENERATE_UUID_EXPRESSION = 'gen_random_uuid()'; - protected const TYPES_WITH_SIZE = [ 'bit', 'bit varying', @@ -90,4 +90,16 @@ protected function getDbType(ColumnSchemaInterface $column): string default => 'varchar', }; } + + protected function getDefaultUuidExpression(): string + { + $serverVersion = $this->queryBuilder->getServerInfo()->getVersion(); + + if (version_compare($serverVersion, '12', '<')) { + return "uuid_in(overlay(overlay(md5(now()::text || random()::text) placing '4' from 13) placing" + . ' to_hex(floor(4 * random() + 8)::int)::text from 17)::cstring)'; + } + + return 'gen_random_uuid()'; + } } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 078ffb8c..ff9d8904 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -16,6 +16,7 @@ use Yiisoft\Db\Tests\Support\TraversableObject; use function array_replace; +use function version_compare; final class QueryBuilderProvider extends \Yiisoft\Db\Tests\Provider\QueryBuilderProvider { @@ -622,6 +623,19 @@ public static function buildColumnDefinition(): array $values['scale(2)'][0] = 'numeric(10,2)'; $values['integer(8)->scale(2)'][0] = 'integer'; + $db = self::getDb(); + $serverVersion = self::getDb()->getServerInfo()->getVersion(); + $db->close(); + + if (version_compare($serverVersion, '12', '<')) { + $uuidExpression = "uuid_in(overlay(overlay(md5(now()::text || random()::text) placing '4' from 13) placing" + . ' to_hex(floor(4 * random() + 8)::int)::text from 17)::cstring)'; + + $values[PseudoType::UUID_PK][0] = "uuid PRIMARY KEY DEFAULT $uuidExpression"; + $values[PseudoType::UUID_PK_SEQ][0] = "uuid PRIMARY KEY DEFAULT $uuidExpression"; + $values['uuidPrimaryKey()'][0] = "uuid PRIMARY KEY DEFAULT $uuidExpression"; + } + return $values; }