Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Database/Models/PgSQL/PgSQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ private function getEnumPresetValues(): array
return [];
}

// Check if the constraint uses regex operators - if so, it's not an enum
if ($this->isRegexConstraint($definition)) {
return [];
}

$presetValues = Regex::getTextBetweenAll($definition, "'", "'::");

if ($presetValues === null) {
Expand All @@ -179,4 +184,28 @@ private function getEnumPresetValues(): array

return $presetValues;
}

/**
* Check if a constraint definition uses regex operators.
* PostgreSQL regex operators: ~ (match), ~* (case-insensitive match),
* !~ (not match), !~* (case-insensitive not match)
*/
private function isRegexConstraint(string $definition): bool
{
// Look for regex operators outside of quotes
$patterns = [
'/\s+~\s+/', // match operator
'/\s+~\*\s+/', // case-insensitive match operator
'/\s+!~\s+/', // not match operator
'/\s+!~\*\s+/', // case-insensitive not match operator
];

foreach ($patterns as $pattern) {
if (preg_match($pattern, $definition)) {
return true;
}
}

return false;
}
}
54 changes: 53 additions & 1 deletion tests/Unit/Database/Models/PgSQL/PgSQLColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PgSQLColumnTest extends TestCase
public function testSpatialTypeName(string $type): void
{
$this->mock(PgSQLRepository::class, static function (MockInterface $mock): void {
$mock->shouldReceive('getStoredDefinition');
$mock->shouldReceive('getCheckConstraintDefinition')->andReturn(null);
});

$column = new PgSQLColumn('table', [
Expand All @@ -35,6 +35,58 @@ public function testSpatialTypeName(string $type): void
$this->assertSame(4326, $column->getSpatialSrID());
}

public function testRegexCheckConstraintShouldNotBecomeEnum(): void
{
$this->mock(PgSQLRepository::class, static function (MockInterface $mock): void {
// Mock a regex-based check constraint using ~ operator
$mock->shouldReceive('getCheckConstraintDefinition')
->with('organisations', 'parent_id')
->andReturn("(parent_id IS NULL) OR ((parent_id)::text ~ '^O\\.[0-9]+$'::text)");
});

$column = new PgSQLColumn('organisations', [
'name' => 'parent_id',
'type_name' => 'character varying',
'type' => 'character varying',
'collation' => null,
'nullable' => true,
'default' => null,
'auto_increment' => false,
'comment' => null,
'generation' => null,
]);

// Should remain STRING type, not become ENUM
$this->assertSame(ColumnType::STRING, $column->getType());
$this->assertEmpty($column->getPresetValues());
}

public function testEnumCheckConstraintShouldBecomeEnum(): void
{
$this->mock(PgSQLRepository::class, static function (MockInterface $mock): void {
// Mock a real enum-style check constraint
$mock->shouldReceive('getCheckConstraintDefinition')
->with('test_table', 'status')
->andReturn("((status)::text = ANY ((ARRAY['pending'::character varying, 'active'::character varying, 'inactive'::character varying])::text[]))");
});

$column = new PgSQLColumn('test_table', [
'name' => 'status',
'type_name' => 'character varying',
'type' => 'character varying',
'collation' => null,
'nullable' => false,
'default' => null,
'auto_increment' => false,
'comment' => null,
'generation' => null,
]);

// This should become ENUM type
$this->assertSame(ColumnType::ENUM, $column->getType());
$this->assertSame(['pending', 'active', 'inactive'], $column->getPresetValues());
}

/**
* @return array<string, string[]>
*/
Expand Down
Loading