Skip to content

Commit f8b3a87

Browse files
feat: add ULID column type (#233)
1 parent 02808e1 commit f8b3a87

File tree

9 files changed

+135
-4
lines changed

9 files changed

+135
-4
lines changed

psalm-baseline.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
<code><![CDATA[$param]]></code>
297297
<code><![CDATA[$param]]></code>
298298
<code><![CDATA[$param]]></code>
299+
<code><![CDATA[$tokens['alias']]]></code>
299300
<code><![CDATA[$tokens['columns']]]></code>
300301
<code><![CDATA[$tokens['columns']]]></code>
301302
<code><![CDATA[$tokens['distinct']]]></code>
@@ -379,6 +380,7 @@
379380
<code><![CDATA[$identifier]]></code>
380381
</MoreSpecificImplementedParamType>
381382
<PossiblyUndefinedStringArrayOffset>
383+
<code><![CDATA[$tokens['alias']]]></code>
382384
<code><![CDATA[$tokens['columns']]]></code>
383385
<code><![CDATA[$tokens['columns']]]></code>
384386
<code><![CDATA[$tokens['distinct']]]></code>
@@ -901,19 +903,20 @@
901903
</MixedAssignment>
902904
<PossiblyUndefinedStringArrayOffset>
903905
<code><![CDATA[$matches['type']]]></code>
906+
<code><![CDATA[$schema['Comment']]]></code>
904907
<code><![CDATA[$schema['Default']]]></code>
905908
<code><![CDATA[$schema['Extra']]]></code>
906909
<code><![CDATA[$schema['Field']]]></code>
907910
<code><![CDATA[$schema['Null']]]></code>
908911
<code><![CDATA[$schema['Type']]]></code>
909-
<code><![CDATA[$schema['Comment']]]></code>
910912
</PossiblyUndefinedStringArrayOffset>
911913
<RiskyTruthyFalsyComparison>
912914
<code><![CDATA[empty($matches['attr'])]]></code>
913915
<code><![CDATA[empty($matches['options'])]]></code>
914916
</RiskyTruthyFalsyComparison>
915917
<UndefinedInterfaceMethod>
916918
<code><![CDATA[identifier]]></code>
919+
<code><![CDATA[identifier]]></code>
917920
</UndefinedInterfaceMethod>
918921
</file>
919922
<file src="src/Driver/MySQL/Schema/MySQLForeignKey.php">
@@ -1303,10 +1306,10 @@
13031306
<code><![CDATA[$identifier]]></code>
13041307
<code><![CDATA[$identifier]]></code>
13051308
<code><![CDATA[$range]]></code>
1309+
<code><![CDATA[$tableName]]></code>
13061310
<code><![CDATA[$value]]></code>
13071311
<code><![CDATA[$value]]></code>
13081312
<code><![CDATA[$value]]></code>
1309-
<code><![CDATA[$tableName]]></code>
13101313
</MixedAssignment>
13111314
<MixedOperand>
13121315
<code><![CDATA[$schema['dtd_identifier']]]></code>
@@ -1321,6 +1324,7 @@
13211324
<code><![CDATA[$schema['column_name']]]></code>
13221325
<code><![CDATA[$schema['data_type']]]></code>
13231326
<code><![CDATA[$schema['datetime_precision']]]></code>
1327+
<code><![CDATA[$schema['description']]]></code>
13241328
<code><![CDATA[$schema['dtd_identifier']]]></code>
13251329
<code><![CDATA[$schema['interval_type']]]></code>
13261330
<code><![CDATA[$schema['is_nullable']]]></code>
@@ -1330,7 +1334,6 @@
13301334
<code><![CDATA[$schema['tableOID']]]></code>
13311335
<code><![CDATA[$schema['typname']]]></code>
13321336
<code><![CDATA[$schema['typtype']]]></code>
1333-
<code><![CDATA[$schema['description']]]></code>
13341337
</PossiblyUndefinedStringArrayOffset>
13351338
<RedundantCast>
13361339
<code><![CDATA[(bool) (
@@ -2424,6 +2427,14 @@
24242427
<code><![CDATA[$value]]></code>
24252428
</PropertyNotSetInConstructor>
24262429
</file>
2430+
<file src="src/Injection/SubQuery.php">
2431+
<ClassMustBeFinal>
2432+
<code><![CDATA[SubQuery]]></code>
2433+
</ClassMustBeFinal>
2434+
<MixedPropertyTypeCoercion>
2435+
<code><![CDATA[$parameters->getParameters()]]></code>
2436+
</MixedPropertyTypeCoercion>
2437+
</file>
24272438
<file src="src/Query/ActiveQuery.php">
24282439
<PossiblyNullArgument>
24292440
<code><![CDATA[$this->prefix]]></code>

src/Driver/Jsoner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function toJson(mixed $value, bool $encode = true, bool $validate
3232

3333
$result = (string) $value;
3434

35-
if ($validate && !json_validate($result)) {
35+
if ($validate && !\json_validate($result)) {
3636
throw new BuilderException('Invalid JSON value.');
3737
}
3838

src/Driver/MySQL/Schema/MySQLColumn.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class MySQLColumn extends AbstractColumn
112112

113113
//Additional types
114114
'json' => 'json',
115+
'ulid' => ['type' => 'varchar', 'size' => 26],
115116
'uuid' => ['type' => 'varchar', 'size' => 36],
116117
];
117118
protected array $reverseMapping = [

src/Driver/Postgres/Schema/PostgresColumn.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class PostgresColumn extends AbstractColumn
181181
//Additional types
182182
'json' => 'json',
183183
'jsonb' => 'jsonb',
184+
'ulid' => ['type' => 'character varying', 'size' => 26],
184185
'uuid' => 'uuid',
185186
'point' => 'point',
186187
'line' => 'line',

src/Driver/SQLServer/Schema/SQLServerColumn.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class SQLServerColumn extends AbstractColumn
9898

9999
//Additional types
100100
'json' => ['type' => 'varchar', 'size' => 0],
101+
'ulid' => ['type' => 'varchar', 'size' => 26],
101102
'uuid' => ['type' => 'varchar', 'size' => 36],
102103
];
103104
protected array $reverseMapping = [

src/Driver/SQLite/Schema/SQLiteColumn.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class SQLiteColumn extends AbstractColumn
8787

8888
//Additional types
8989
'json' => 'text',
90+
'ulid' => ['type' => 'varchar', 'size' => 26],
9091
'uuid' => ['type' => 'varchar', 'size' => 36],
9192
];
9293
protected array $reverseMapping = [

src/Schema/AbstractColumn.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @method $this|AbstractColumn tinyBinary()
5151
* @method $this|AbstractColumn longBinary()
5252
* @method $this|AbstractColumn json()
53+
* @method $this|AbstractColumn ulid()
5354
* @method $this|AbstractColumn uuid()
5455
*/
5556
abstract class AbstractColumn implements ColumnInterface, ElementInterface

src/Schema/AbstractTable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @method AbstractColumn binary($column)
5252
* @method AbstractColumn tinyBinary($column)
5353
* @method AbstractColumn longBinary($column)
54+
* @method AbstractColumn ulid($column)
5455
* @method AbstractColumn uuid($column)
5556
*/
5657
abstract class AbstractTable implements TableInterface, ElementInterface

tests/Database/Functional/Driver/Common/Schema/ConsistencyTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,92 @@ public function testTime(): void
360360
$this->assertTrue($schema->column('target')->compare($column));
361361
}
362362

363+
public function testUlid(): void
364+
{
365+
$schema = $this->schema('table');
366+
$this->assertFalse($schema->exists());
367+
368+
$column = $schema->ulid('target');
369+
370+
$schema->save();
371+
372+
$schema = $this->schema('table');
373+
$this->assertTrue($schema->exists());
374+
$this->assertTrue($schema->column('target')->compare($column));
375+
$this->assertSame('string', $schema->column('target')->getType());
376+
377+
$this->database->table('table')->insertOne(
378+
[
379+
'target' => '0GWWXY2G84DFMRVWQNJ1SRYCMC',
380+
],
381+
);
382+
383+
$this->assertEquals(
384+
[
385+
'target' => '0GWWXY2G84DFMRVWQNJ1SRYCMC',
386+
],
387+
$this->database->table('table')->select()->fetchAll()[0],
388+
);
389+
}
390+
391+
public function testUlidCallingColumnMethod(): void
392+
{
393+
$schema = $this->schema('table');
394+
$this->assertFalse($schema->exists());
395+
396+
$column = $schema->column('target')->ulid();
397+
398+
$schema->save();
399+
400+
$schema = $this->schema('table');
401+
$this->assertTrue($schema->exists());
402+
$this->assertTrue($schema->column('target')->compare($column));
403+
$this->assertSame('string', $schema->column('target')->getType());
404+
405+
$this->database->table('table')->insertOne(
406+
[
407+
'target' => '0GWWXY2G84DFMRVWQNJ1SRYCMC',
408+
],
409+
);
410+
411+
$this->assertEquals(
412+
[
413+
'target' => '0GWWXY2G84DFMRVWQNJ1SRYCMC',
414+
],
415+
$this->database->table('table')->select()->fetchAll()[0],
416+
);
417+
}
418+
419+
public function testUlidPrimary(): void
420+
{
421+
$schema = $this->schema('table');
422+
$this->assertFalse($schema->exists());
423+
424+
$column = $schema->ulid('target')->nullable(false);
425+
$schema->setPrimaryKeys(['target']);
426+
$schema->save();
427+
428+
$schema = $this->schema('table');
429+
$this->assertTrue($schema->exists());
430+
431+
$this->assertTrue($schema->column('target')->compare($column));
432+
$this->assertSame('string', $schema->column('target')->getType());
433+
$this->assertSame(['target'], $schema->getPrimaryKeys());
434+
435+
$this->database->table('table')->insertOne(
436+
[
437+
'target' => '0GWWXY2G84DFMRVWQNJ1SRYCMC',
438+
],
439+
);
440+
441+
$this->assertEquals(
442+
[
443+
'target' => '0GWWXY2G84DFMRVWQNJ1SRYCMC',
444+
],
445+
$this->database->table('table')->select()->fetchAll()[0],
446+
);
447+
}
448+
363449
public function testUuid(): void
364450
{
365451
$schema = $this->schema('table');
@@ -388,6 +474,34 @@ public function testUuid(): void
388474
);
389475
}
390476

477+
public function testUuidCallingColumnMethod(): void
478+
{
479+
$schema = $this->schema('table');
480+
$this->assertFalse($schema->exists());
481+
482+
$column = $schema->column('target')->uuid();
483+
484+
$schema->save();
485+
486+
$schema = $this->schema('table');
487+
$this->assertTrue($schema->exists());
488+
$this->assertTrue($schema->column('target')->compare($column));
489+
$this->assertSame('string', $schema->column('target')->getType());
490+
491+
$this->database->table('table')->insertOne(
492+
[
493+
'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
494+
],
495+
);
496+
497+
$this->assertEquals(
498+
[
499+
'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
500+
],
501+
$this->database->table('table')->select()->fetchAll()[0],
502+
);
503+
}
504+
391505
public function testUuidPrimary(): void
392506
{
393507
$schema = $this->schema('table');

0 commit comments

Comments
 (0)