From 5e06392c8ae0fd87eac7b56532a9a58586059d53 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 11 Dec 2024 17:31:50 +0700 Subject: [PATCH] Add `AbstractColumnDefinitionBuilder::getDefaultUuidExpression()` method --- .../AbstractColumnDefinitionBuilder.php | 19 ++++++++++++------- tests/AbstractQueryBuilderTest.php | 2 ++ .../Support/Stub/ColumnDefinitionBuilder.php | 7 +++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/QueryBuilder/AbstractColumnDefinitionBuilder.php b/src/QueryBuilder/AbstractColumnDefinitionBuilder.php index 790bf0937..b614a2e6c 100644 --- a/src/QueryBuilder/AbstractColumnDefinitionBuilder.php +++ b/src/QueryBuilder/AbstractColumnDefinitionBuilder.php @@ -21,11 +21,6 @@ abstract class AbstractColumnDefinitionBuilder implements ColumnDefinitionBuilde */ protected const AUTO_INCREMENT_KEYWORD = ''; - /** - * @var string The expression used to generate a UUID value. - */ - protected const GENERATE_UUID_EXPRESSION = ''; - /** * @var string[] The list of database column types (in lower case) that allow size specification. */ @@ -119,12 +114,14 @@ protected function buildComment(ColumnSchemaInterface $column): string */ protected function buildDefault(ColumnSchemaInterface $column): string { - if (!empty(static::GENERATE_UUID_EXPRESSION) + $uuidExpression = $this->getDefaultUuidExpression(); + + if (!empty($uuidExpression) && $column->getType() === ColumnType::UUID && $column->isAutoIncrement() && $column->getDefaultValue() === null ) { - return ' DEFAULT ' . static::GENERATE_UUID_EXPRESSION; + return " DEFAULT $uuidExpression"; } if ($column->isAutoIncrement() && $column->getType() !== ColumnType::UUID @@ -303,4 +300,12 @@ protected function buildUnsigned(ColumnSchemaInterface $column): string { return $column->isUnsigned() ? ' UNSIGNED' : ''; } + + /** + * Get the expression used to generate a UUID as a default value. + */ + protected function getDefaultUuidExpression(): string + { + return ''; + } } diff --git a/tests/AbstractQueryBuilderTest.php b/tests/AbstractQueryBuilderTest.php index 8bf784832..6bf0e4160 100644 --- a/tests/AbstractQueryBuilderTest.php +++ b/tests/AbstractQueryBuilderTest.php @@ -2403,6 +2403,8 @@ public function testBuildColumnDefinition(string $expected, ColumnSchemaInterfac $qb = $db->getQueryBuilder(); $this->assertSame($expected, $qb->buildColumnDefinition($column)); + + $db->close(); } #[DataProviderExternal(QueryBuilderProvider::class, 'prepareParam')] diff --git a/tests/Support/Stub/ColumnDefinitionBuilder.php b/tests/Support/Stub/ColumnDefinitionBuilder.php index 07d137477..bb79f985c 100644 --- a/tests/Support/Stub/ColumnDefinitionBuilder.php +++ b/tests/Support/Stub/ColumnDefinitionBuilder.php @@ -12,8 +12,6 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder { protected const AUTO_INCREMENT_KEYWORD = 'AUTOINCREMENT'; - protected const GENERATE_UUID_EXPRESSION = 'uuid()'; - protected const TYPES_WITH_SIZE = [ 'bit', 'tinyint', @@ -66,4 +64,9 @@ protected function getDbType(ColumnSchemaInterface $column): string default => 'varchar', }; } + + protected function getDefaultUuidExpression(): string + { + return 'uuid()'; + } }