Skip to content

Commit 0dd9f7c

Browse files
authored
UUID PK support (#222)
* UUID PK support * styleci * styleci * fixes
1 parent e003359 commit 0dd9f7c

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/Column.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function asString(): string
3939
$format = match ($this->getTypeCategory()) {
4040
self::CATEGORY_PK => '{type}{check}{append}',
4141
self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{unique}{check}{default}{append}',
42+
self::CATEGORY_UUID => '{type}{notnull}{unique}{default}{check}{comment}{append}',
43+
self::CATEGORY_UUID_PK => '{type}{notnull}{default}{check}{comment}{append}',
4244
default => '{type}{length}{notnull}{unique}{check}{default}{append}',
4345
};
4446

src/QueryBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ final class QueryBuilder extends AbstractQueryBuilder
4040
SchemaInterface::TYPE_BINARY => 'blob',
4141
SchemaInterface::TYPE_BOOLEAN => 'boolean',
4242
SchemaInterface::TYPE_MONEY => 'decimal(19,4)',
43+
SchemaInterface::TYPE_UUID => 'blob(16)',
44+
SchemaInterface::TYPE_UUID_PK => 'blob(16) PRIMARY KEY',
4345
];
4446

4547
public function __construct(QuoterInterface $quoter, SchemaInterface $schema)

tests/ColumnSchemaBuilderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ public function testCustomTypes(string $expected, string $type, int|null $length
2323
{
2424
$this->checkBuildString($expected, $type, $length, $calls);
2525
}
26+
27+
/**
28+
* @dataProvider \Yiisoft\Db\Sqlite\Tests\Provider\ColumnSchemaBuilderProvider::createColumnTypes
29+
*/
30+
public function testCreateColumnTypes(string $expected, string $type, ?int $length, array $calls): void
31+
{
32+
parent::testCreateColumnTypes($expected, $type, $length, $calls);
33+
}
2634
}

tests/Provider/ColumnSchemaBuilderProvider.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class ColumnSchemaBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaBuilderProvider
1010
{
11+
protected static string $driverName = 'sqlite';
12+
1113
public static function types(): array
1214
{
1315
$types = parent::types();
@@ -22,4 +24,23 @@ public static function types(): array
2224
],
2325
);
2426
}
27+
28+
public static function createColumnTypes(): array
29+
{
30+
$types = parent::createColumnTypes();
31+
32+
$types['uuid'][0] = '`column` blob(16)';
33+
$types['uuid not null'][0] = '`column` blob(16) NOT NULL';
34+
35+
$types['uuid with default'][0] = '`column` blob(16) DEFAULT (UNHEX(REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\',\'-\',\'\')))';
36+
$types['uuid with default'][3] = [['defaultExpression', '(UNHEX(REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\',\'-\',\'\')))']];
37+
38+
$types['uuid pk'][0] = '`column` blob(16) PRIMARY KEY';
39+
$types['uuid pk not null'][0] = '`column` blob(16) PRIMARY KEY NOT NULL';
40+
41+
$types['uuid pk not null with default'][0] = '`column` blob(16) PRIMARY KEY NOT NULL DEFAULT (RANDOMBLOB(16))';
42+
$types['uuid pk not null with default'][3] = [['notNull'],['defaultExpression', '(RANDOMBLOB(16))']];
43+
44+
return $types;
45+
}
2546
}

0 commit comments

Comments
 (0)