Skip to content

Commit e8ec084

Browse files
authored
Merge pull request #38: Registry modifier big integer
2 parents da112a6 + 690aa10 commit e8ec084

4 files changed

Lines changed: 93 additions & 1 deletion

File tree

psalm-baseline.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="6.8.8@1361cd33008feb3ae2b4a93f1860e14e538ec8c2">
2+
<files psalm-version="6.12.1@e71404b0465be25cf7f8a631b298c01c5ddd864f">
33
<file src="src/CreatedAt.php">
44
<PropertyNotSetInConstructor>
55
<code><![CDATA[CreatedAt]]></code>
@@ -113,6 +113,9 @@
113113
<code><![CDATA[$columnName]]></code>
114114
<code><![CDATA[$columnName]]></code>
115115
<code><![CDATA[$columnName]]></code>
116+
<code><![CDATA[$columnName]]></code>
117+
<code><![CDATA[$columnName]]></code>
118+
<code><![CDATA[$columnName]]></code>
116119
<code><![CDATA[$rule]]></code>
117120
</ArgumentTypeCoercion>
118121
<ClassMustBeFinal>

src/Schema/RegistryModifier.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ class RegistryModifier
3232
'smallInteger',
3333
'bigInteger',
3434
];
35+
protected const BIG_INTEGER_TYPES = [
36+
'bigint',
37+
'bigInteger',
38+
];
3539
protected const DATETIME_TYPES = ['datetime', 'datetime2'];
3640
protected const INT_COLUMN = AbstractColumn::INT;
3741
protected const STRING_COLUMN = AbstractColumn::STRING;
42+
protected const BIG_INTEGER_COLUMN = 'bigInteger';
3843
protected const DATETIME_COLUMN = 'datetime';
3944
protected const UUID_COLUMN = 'uuid';
4045

@@ -58,6 +63,13 @@ public static function isIntegerType(string $type): bool
5863
return \in_array($matches['type'], self::INTEGER_TYPES, true);
5964
}
6065

66+
public static function isBigIntegerType(string $type): bool
67+
{
68+
\preg_match(self::DEFINITION, $type, $matches);
69+
70+
return \in_array($matches['type'], self::BIG_INTEGER_TYPES, true);
71+
}
72+
6173
public static function isDatetimeType(string $type): bool
6274
{
6375
\preg_match(self::DEFINITION, $type, $matches);
@@ -123,6 +135,32 @@ public function addIntegerColumn(string $columnName, string $fieldName, int|null
123135
return $this->table->column($columnName)->type(self::INT_COLUMN);
124136
}
125137

138+
public function addBigIntegerColumn(
139+
string $columnName,
140+
string $fieldName,
141+
int|null $generated = null,
142+
): AbstractColumn {
143+
if ($this->fields->has($fieldName)) {
144+
if (! static::isBigIntegerType($this->fields->get($fieldName)->getType())) {
145+
throw new BehaviorCompilationException(\sprintf('Field %s must be of type big integer.', $fieldName));
146+
}
147+
$this->validateColumnName($fieldName, $columnName);
148+
$this->fields->get($fieldName)->setGenerated($generated);
149+
150+
return $this->table->column($columnName);
151+
}
152+
153+
$field = (new Field())
154+
->setColumn($columnName)
155+
->setType(self::BIG_INTEGER_COLUMN)
156+
->setTypecast('int')
157+
->setGenerated($generated);
158+
159+
$this->fields->set($fieldName, $field);
160+
161+
return $this->table->column($columnName)->type(self::BIG_INTEGER_COLUMN);
162+
}
163+
126164
public function addStringColumn(string $columnName, string $fieldName, int|null $generated = null): AbstractColumn
127165
{
128166
if ($this->fields->has($fieldName)) {

tests/Behavior/Functional/Driver/Common/Schema/RegistryModifierTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\Schema;
66

77
use Cycle\Database\ColumnInterface;
8+
use Cycle\ORM\Entity\Behavior\Exception\BehaviorCompilationException;
89
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier;
910
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\CustomTypecast;
1011
use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\BaseTest;
@@ -57,6 +58,28 @@ public function testAddIntegerField(): void
5758
$this->assertSame('version_int', $fields->get('version')->getColumn());
5859
}
5960

61+
public function testAddBigIntegerField(): void
62+
{
63+
$this->modifier->addBigIntegerColumn('snowflake_column', 'snowflake');
64+
65+
$entity = $this->registry->getEntity(self::ROLE_TEST);
66+
$fields = $entity->getFields();
67+
68+
$this->assertTrue($fields->has('snowflake'));
69+
$this->assertSame('bigInteger', $fields->get('snowflake')->getType());
70+
$this->assertSame('snowflake_column', $fields->get('snowflake')->getColumn());
71+
}
72+
73+
public function testAddBigIntegerFieldThrowsException(): void
74+
{
75+
$this->modifier->addIntegerColumn('snowflake_column', 'snowflake');
76+
77+
$this->expectException(BehaviorCompilationException::class);
78+
$this->expectExceptionMessage('Field snowflake must be of type big integer.');
79+
80+
$this->modifier->addBigIntegerColumn('snowflake_column', 'snowflake');
81+
}
82+
6083
public function testAddUuidField(): void
6184
{
6285
$this->modifier->addUuidColumn('uuid_column', 'uuid');
@@ -73,15 +96,19 @@ public function testAddTypecast(): void
7396
{
7497
$this->modifier->addUuidColumn('uuid_column', 'uuid');
7598
$this->modifier->addIntegerColumn('counter_column', 'counter');
99+
$this->modifier->addBigIntegerColumn('snowflake_column', 'snowflake');
76100
$field1 = $this->registry->getEntity(self::ROLE_TEST)->getFields()->get('uuid');
77101
$field2 = $this->registry->getEntity(self::ROLE_TEST)->getFields()->get('counter');
102+
$field3 = $this->registry->getEntity(self::ROLE_TEST)->getFields()->get('snowflake');
78103

79104
$this->modifier->setTypecast($field1, [Uuid::class, 'fromString']);
80105
$this->modifier->setTypecast($field2, 'int', CustomTypecast::class);
106+
$this->modifier->setTypecast($field3, 'int', CustomTypecast::class);
81107

82108
// field has custom UUID typecast
83109
$this->assertSame([Uuid::class, 'fromString'], $field1->getTypecast());
84110
$this->assertSame('int', $field2->getTypecast());
111+
$this->assertSame('int', $field3->getTypecast());
85112

86113
// entity has default typecast
87114
$this->assertSame(

tests/Behavior/Unit/Schema/RegistryModifierTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public static function integerDataProvider(): \Traversable
2222
yield ['integer(4)'];
2323
}
2424

25+
public static function bigIntegerDataProvider(): \Traversable
26+
{
27+
yield ['bigint'];
28+
yield ['bigInteger'];
29+
}
30+
2531
public static function datetimeDataProvider(): \Traversable
2632
{
2733
yield ['datetime'];
@@ -61,6 +67,24 @@ public function testIsIntegerTypeFalse(mixed $type): void
6167
$this->assertFalse(RegistryModifier::isIntegerType($type));
6268
}
6369

70+
/**
71+
* @dataProvider bigIntegerDataProvider
72+
*/
73+
public function testIsBigIntegerTypeTrue(mixed $type): void
74+
{
75+
$this->assertTrue(RegistryModifier::isBigIntegerType($type));
76+
}
77+
78+
/**
79+
* @dataProvider datetimeDataProvider
80+
* @dataProvider stringDataProvider
81+
* @dataProvider invalidDataProvider
82+
*/
83+
public function testIsBigIntegerTypeFalse(mixed $type): void
84+
{
85+
$this->assertFalse(RegistryModifier::isBigIntegerType($type));
86+
}
87+
6488
/**
6589
* @dataProvider datetimeDataProvider
6690
*/

0 commit comments

Comments
 (0)