Skip to content
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
3 changes: 2 additions & 1 deletion src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,8 @@ protected function getRenameColumnInstructions(string $tableName, string $column
$extra = ' ' . implode(' ', $extras);

if (($row['Default'] !== null)) {
$extra .= $this->getDefaultValueDefinition($row['Default']);
$phinxType = $this->getPhinxType($row['Type']);
$extra .= $this->getDefaultValueDefinition($row['Default'], $phinxType['name']);
}
$definition = $row['Type'] . ' ' . $null . $extra . $comment;

Expand Down
9 changes: 8 additions & 1 deletion src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,16 @@ public function getAttribute(int $attribute): mixed
*/
protected function getDefaultValueDefinition(mixed $default, string|Literal|null $columnType = null): string
{
$datetimeTypes = [
static::PHINX_TYPE_DATETIME,
static::PHINX_TYPE_TIMESTAMP,
static::PHINX_TYPE_TIME,
static::PHINX_TYPE_DATE,
];

if ($default instanceof Literal) {
$default = (string)$default;
} elseif (is_string($default) && stripos($default, 'CURRENT_TIMESTAMP') !== 0) {
} elseif (is_string($default) && (!in_array($columnType, $datetimeTypes) || !preg_match('/^(CURRENT_(TIMESTAMP|TIME|DATE))|(NOW)(\(\d*\))?$/i', $default))) {
// Ensure a defaults of CURRENT_TIMESTAMP(3) is not quoted.
$default = $this->getConnection()->quote($default);
} elseif (is_bool($default)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ protected function getChangeDefault(string $tableName, Column $newColumn): Alter
if ($default === null) {
$default = 'DEFAULT NULL';
} else {
$default = ltrim($this->getDefaultValueDefinition($default));
$default = ltrim($this->getDefaultValueDefinition($default, $newColumn->getType()));
}

if (empty($default)) {
Expand Down Expand Up @@ -1264,7 +1264,7 @@ protected function getColumnSqlDefinition(Column $column, bool $create = true):
if ($column->getDefault() === null && $column->isNull()) {
$buffer[] = ' DEFAULT NULL';
} else {
$buffer[] = $this->getDefaultValueDefinition($column->getDefault());
$buffer[] = $this->getDefaultValueDefinition($column->getDefault(), $column->getType());
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Phinx/Db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,9 @@ public function addTimestamps(string|false|null $createdAt = 'created_at', strin
throw new RuntimeException('Cannot set both created_at and updated_at columns to false');
}

$columnType = FeatureFlags::$addTimestampsUseDateTime ? 'datetime' : 'timestamp';
$columnType = FeatureFlags::$addTimestampsUseDateTime
? AdapterInterface::PHINX_TYPE_DATETIME
: AdapterInterface::PHINX_TYPE_TIMESTAMP;

if ($createdAt) {
$this->addColumn($createdAt, $columnType, [
Expand Down
43 changes: 43 additions & 0 deletions tests/Phinx/Db/Adapter/PdoAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PDO;
use PDOException;
use Phinx\Config\Config;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Util\Literal;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
Expand Down Expand Up @@ -261,4 +262,46 @@ public function testQuoteValueString($input, $expected): void
$method = new ReflectionMethod($this->adapter, 'quoteValue');
$this->assertSame($expected, $method->invoke($this->adapter, $input));
}

public function defaultValueDefinitionDataProvider(): array
{
return [
['some string', AdapterInterface::PHINX_TYPE_STRING, " DEFAULT 'some string'"],
[123, AdapterInterface::PHINX_TYPE_INTEGER, ' DEFAULT 123'],
[true, AdapterInterface::PHINX_TYPE_BOOLEAN, ' DEFAULT 1'],
[false, AdapterInterface::PHINX_TYPE_BOOLEAN, ' DEFAULT 0'],
[null, AdapterInterface::PHINX_TYPE_STRING, ''],
[Literal::from('foo'), AdapterInterface::PHINX_TYPE_STRING, ' DEFAULT foo'],
['CURRENT_TIMESTAMP', AdapterInterface::PHINX_TYPE_STRING, " DEFAULT 'CURRENT_TIMESTAMP'"],
['CURRENT_TIMESTAMP', AdapterInterface::PHINX_TYPE_DATETIME, ' DEFAULT CURRENT_TIMESTAMP'],
['CURRENT_TIMESTAMP(3)', AdapterInterface::PHINX_TYPE_DATETIME, ' DEFAULT CURRENT_TIMESTAMP(3)'],
['CURRENT_TIMESTAMP()', AdapterInterface::PHINX_TYPE_DATETIME, ' DEFAULT CURRENT_TIMESTAMP()'],
['CURRENT_TIMESTAMP', AdapterInterface::PHINX_TYPE_TIMESTAMP, ' DEFAULT CURRENT_TIMESTAMP'],
['CURRENT_TIME', AdapterInterface::PHINX_TYPE_TIME, ' DEFAULT CURRENT_TIME'],
['CURRENT_DATE', AdapterInterface::PHINX_TYPE_DATE, ' DEFAULT CURRENT_DATE'],
['NOW', AdapterInterface::PHINX_TYPE_DATETIME, ' DEFAULT NOW'],
];
}

/**
* @dataProvider defaultValueDefinitionDataProvider
*/
public function testGetDefaultValueDefinition($input, $columnType, $expected): void
{
/** @var \PDO&\PHPUnit\Framework\MockObject\MockObject $pdo */
$pdo = $this->getMockBuilder(PDO::class)
->disableOriginalConstructor()
->onlyMethods(['quote'])
->getMock();

$pdo->method('quote')
->willReturnCallback(function (string $input) {
return "'$input'";
});

$this->adapter->setConnection($pdo);

$method = new ReflectionMethod($this->adapter, 'getDefaultValueDefinition');
$this->assertSame($expected, $method->invoke($this->adapter, $input, $columnType));
}
}