Skip to content

Commit

Permalink
Add AbstractColumnDefinitionBuilder::getDefaultUuidExpression() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Dec 11, 2024
1 parent b60e908 commit 5e06392
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/QueryBuilder/AbstractColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Check warning on line 307 in src/QueryBuilder/AbstractColumnDefinitionBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractColumnDefinitionBuilder.php#L307

Added line #L307 was not covered by tests
{
return '';

Check warning on line 309 in src/QueryBuilder/AbstractColumnDefinitionBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractColumnDefinitionBuilder.php#L309

Added line #L309 was not covered by tests
}
}
2 changes: 2 additions & 0 deletions tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down
7 changes: 5 additions & 2 deletions tests/Support/Stub/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -66,4 +64,9 @@ protected function getDbType(ColumnSchemaInterface $column): string
default => 'varchar',
};
}

protected function getDefaultUuidExpression(): string
{
return 'uuid()';
}
}

0 comments on commit 5e06392

Please sign in to comment.