Skip to content

Commit

Permalink
Adding the ability to define custom column types (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored May 19, 2023
1 parent ff384c1 commit b60830d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ public function render(AbstractColumn $column): void
$column->nullable($this->isNullable());

try {
// bypassing call to AbstractColumn->__call method (or specialized column method)
call_user_func_array([$column, $this->type], $this->typeOptions);
// bypassing call to AbstractColumn->type method (or specialized column method)
if (\method_exists($column, $this->type)) {
call_user_func_array([$column, $this->type], $this->typeOptions);
} else {
call_user_func_array([$column, 'type'], \array_merge([$this->type], $this->typeOptions));
}
} catch (\Throwable $e) {
throw new ColumnException(
"Invalid column type definition in '{$column->getTable()}'.'{$column->getName()}'",
Expand Down
15 changes: 15 additions & 0 deletions tests/Schema/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,21 @@ public function testCastDefaultDatetime(): void
$this->assertInstanceOf(\DateTimeInterface::class, $table->column('name')->getDefaultValue());
}

public function testRenderWithCustomType(): void
{
$field = new Field();
$field->setType('ltree');
$field->setColumn('name');

$table = $this->getStub();
$column = Column::parse($field);

$column->render($table->column('name'));

$this->assertTrue($table->hasColumn('name'));
$this->assertSame('ltree', $table->column('name')->getDeclaredType());
}

/**
* @return AbstractTable
*/
Expand Down

0 comments on commit b60830d

Please sign in to comment.