Skip to content

Commit

Permalink
Fix using literal type with add column (#2255)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndm2 authored Jan 20, 2024
1 parent c353796 commit f805b60
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,10 @@ public function getAttribute(int $attribute): mixed
* Get the definition for a `DEFAULT` statement.
*
* @param mixed $default Default value
* @param string|null $columnType column type added
* @param string|\Phinx\Util\Literal|null $columnType Column type
* @return string
*/
protected function getDefaultValueDefinition(mixed $default, ?string $columnType = null): string
protected function getDefaultValueDefinition(mixed $default, string|Literal|null $columnType = null): string
{
if ($default instanceof Literal) {
$default = (string)$default;
Expand All @@ -672,7 +672,7 @@ protected function getDefaultValueDefinition(mixed $default, ?string $columnType
$default = $this->getConnection()->quote($default);
} elseif (is_bool($default)) {
$default = $this->castToBool($default);
} elseif ($default !== null && $columnType === static::PHINX_TYPE_BOOLEAN) {
} elseif ($default !== null && (string)$columnType === static::PHINX_TYPE_BOOLEAN) {
$default = $this->castToBool((bool)$default);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,23 @@ public function testAddColumnWithDefaultLiteral()
$this->assertTrue($rows[1]['Default'] === 'CURRENT_TIMESTAMP' || $rows[1]['Default'] === 'current_timestamp()');
}

public function testAddColumnWithLiteralTypeAndDefault()
{
$table = new Table('table1', [], $this->adapter);
$table->save();

$table
->addColumn('checked', Literal::from('boolean'), ['default' => 0])
->save();

$column = $this->adapter->getColumns('table1')[1];

$this->assertSame('checked', $column->getName());
$this->assertSame('boolean', $column->getType());
$this->assertSame('0', $column->getDefault());
$this->assertTrue($column->getNull());
}

public function testAddColumnWithCustomType()
{
$this->adapter->setDataDomain([
Expand Down
17 changes: 17 additions & 0 deletions tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,23 @@ public function testAddColumnArrayType($column_name, $column_type)
$this->assertTrue($table->hasColumn($column_name));
}

public function testAddColumnWithLiteralTypeAndDefault()
{
$table = new Table('table1', [], $this->adapter);
$table->save();

$table
->addColumn('checked', Literal::from('boolean'), ['default' => 0])
->save();

$column = $this->adapter->getColumns('table1')[1];

$this->assertSame('checked', $column->getName());
$this->assertSame('boolean', $column->getType());
$this->assertSame('false', $column->getDefault());
$this->assertTrue($column->getNull());
}

public function testAddColumnWithCustomType()
{
$this->adapter->setDataDomain([
Expand Down
17 changes: 17 additions & 0 deletions tests/Phinx/Db/Adapter/SQLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,23 @@ public function testAddColumnWithDefaultEmptyString()
$this->assertEquals("''", $rows[1]['dflt_value']);
}

public function testAddColumnWithLiteralTypeAndDefault()
{
$table = new Table('table1', [], $this->adapter);
$table->save();

$table
->addColumn('checked', Literal::from('boolean'), ['default' => 0])
->save();

$column = $this->adapter->getColumns('table1')[1];

$this->assertSame('checked', $column->getName());
$this->assertSame('boolean', $column->getType());
$this->assertSame(false, $column->getDefault());
$this->assertTrue($column->getNull());
}

public function testAddColumnWithCustomType()
{
$this->adapter->setDataDomain([
Expand Down
17 changes: 17 additions & 0 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,23 @@ public function testAddColumnWithDefaultBool()
}
}

public function testAddColumnWithLiteralTypeAndDefault()
{
$table = new Table('table1', [], $this->adapter);
$table->save();

$table
->addColumn('checked', Literal::from('bit'), ['default' => 0])
->save();

$column = $this->adapter->getColumns('table1')['checked'];

$this->assertSame('checked', $column->getName());
$this->assertSame('boolean', $column->getType());
$this->assertSame(0, $column->getDefault());
$this->assertTrue($column->getNull());
}

public function testAddColumnWithCustomType()
{
$this->adapter->setDataDomain([
Expand Down

0 comments on commit f805b60

Please sign in to comment.