Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes the problem with variadic arguments in column type methods #45

Merged
merged 4 commits into from
Jan 3, 2023
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=8.1",
"cycle/database": "^2.2",
"cycle/database": "^2.3",
"spiral/core": "^3.0",
"spiral/files": "^3.0",
"spiral/tokenizer": "^3.0",
Expand Down
14 changes: 10 additions & 4 deletions src/Operation/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,27 @@ protected function declareColumn(AbstractTable $schema): AbstractColumn
//Type configuring
if (method_exists($column, $this->type)) {
$arguments = [];
$variadic = false;

$method = new \ReflectionMethod($column, $this->type);
foreach ($method->getParameters() as $parameter) {
if ($this->hasOption($parameter->getName())) {
$arguments[] = $this->getOption($parameter->getName());
$arguments[$parameter->getName()] = $this->getOption($parameter->getName());
} elseif (!$parameter->isOptional()) {
throw new ColumnException(
"Option '{$parameter->getName()}' are required to define column with type '{$this->type}'"
);
} else {
$arguments[] = $parameter->getDefaultValue();
} elseif ($parameter->isDefaultValueAvailable()) {
$arguments[$parameter->getName()] = $parameter->getDefaultValue();
} elseif ($parameter->isVariadic()) {
$variadic = true;
}
}

call_user_func_array([$column, $this->type], $arguments);
\call_user_func_array(
[$column, $this->type],
$variadic ? $arguments + $this->options + $column->getAttributes() : $arguments,
);
} else {
$column->type($this->type);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Migrations/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,18 @@ public function testCreateDatetimeNowColumn(): void
$this->migrator->configure();

$schema = $this->schema('sample');
$column = $schema->datetime('value');
$column = $schema->datetime('value', size: 2, foo: 'bar');
$column->defaultValue(new Fragment($column::DATETIME_NOW));

$this->atomize('migration1', [$schema]);

$this->migrator->run();

$this->assertTrue($this->db->hasTable('sample'));
$this->assertSame((string)$column->getDefaultValue(), (string)$this->schema('sample')->column('value')->getDefaultValue());
$this->assertSame(
(string)$column->getDefaultValue(),
(string)$this->schema('sample')->column('value')->getDefaultValue()
);

$this->migrator->rollback();
$this->assertFalse($this->db->hasTable('sample'));
Expand Down