Skip to content

Commit

Permalink
Option for addTimeStamps() to use datetime (#2273)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbmarshall authored Jun 3, 2024
1 parent cba2eac commit 1302418
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/en/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@ For some breaking changes, Phinx offers a way to opt-out of new behavior. The fo
* ``unsigned_primary_keys``: Should Phinx create primary keys as unsigned integers? (default: ``true``)
* ``column_null_default``: Should Phinx create columns as null by default? (default: ``true``)

Since MySQL ``TIMESTAMP`` fields do not support dates past 2038-01-19, you have the option to use ``DATETIME`` field
types for fields created by the ``addTimestamps()`` function:

* ``add_timestamps_use_datetime``: Should Phinx create created_at and updated_at fields as datetime? (default: ``false``)

.. code-block:: yaml
feature_flags:
Expand Down
7 changes: 7 additions & 0 deletions src/Phinx/Config/FeatureFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class FeatureFlags
* @var bool Should Phinx create columns NULL by default?
*/
public static bool $columnNullDefault = true;
/**
* @var bool Should Phinx create datetime columns for addTimestamps instead of timestamp?
*/
public static bool $addTimestampsUseDateTime = false;

/**
* Set the feature flags from the `feature_flags` section of the overall
Expand All @@ -37,5 +41,8 @@ public static function setFlagsFromConfig(array $config): void
if (isset($config['column_null_default'])) {
self::$columnNullDefault = (bool)$config['column_null_default'];
}
if (isset($config['add_timestamps_use_datetime'])) {
self::$addTimestampsUseDateTime = (bool)$config['add_timestamps_use_datetime'];
}
}
}
4 changes: 4 additions & 0 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ public function getColumns(string $tableName): array
$column->setIdentity(true);
}

if ($columnInfo['Extra'] === 'on update CURRENT_TIMESTAMP') {
$column->setUpdate('CURRENT_TIMESTAMP');
}

if (isset($phinxType['values'])) {
$column->setValues($phinxType['values']);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Phinx/Db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Phinx\Db;

use InvalidArgumentException;
use Phinx\Config\FeatureFlags;
use Phinx\Db\Action\AddColumn;
use Phinx\Db\Action\AddForeignKey;
use Phinx\Db\Action\AddIndex;
Expand Down Expand Up @@ -542,16 +543,18 @@ 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';

if ($createdAt) {
$this->addColumn($createdAt, 'timestamp', [
$this->addColumn($createdAt, $columnType, [
'null' => false,
'default' => 'CURRENT_TIMESTAMP',
'update' => '',
'timezone' => $withTimezone,
]);
}
if ($updatedAt) {
$this->addColumn($updatedAt, 'timestamp', [
$this->addColumn($updatedAt, $columnType, [
'null' => true,
'default' => null,
'update' => 'CURRENT_TIMESTAMP',
Expand Down
3 changes: 3 additions & 0 deletions tests/Phinx/Config/FeatureFlagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public function testSetFlagsFromConfig(): void
$config = [
'unsigned_primary_keys' => false,
'column_null_default' => false,
'add_timestamps_use_datetime' => true,
];
$this->assertTrue(FeatureFlags::$unsignedPrimaryKeys);
$this->assertTrue(FeatureFlags::$columnNullDefault);
$this->assertFalse(FeatureFlags::$addTimestampsUseDateTime);
FeatureFlags::setFlagsFromConfig($config);
$this->assertFalse(FeatureFlags::$unsignedPrimaryKeys);
$this->assertFalse(FeatureFlags::$columnNullDefault);
$this->assertTrue(FeatureFlags::$addTimestampsUseDateTime);
}
}
30 changes: 30 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,36 @@ public function testUnsignedPksFeatureFlag()
$this->assertTrue($columns[0]->getSigned());
}

/**
* @runInSeparateProcess
*/
public function testAddTimestampsFeatureFlag()
{
$this->adapter->connect();

FeatureFlags::$addTimestampsUseDateTime = true;

$table = new Table('table1', [], $this->adapter);
$table->addTimestamps();
$table->create();

$columns = $this->adapter->getColumns('table1');

$this->assertCount(3, $columns);
$this->assertSame('id', $columns[0]->getName());

$this->assertEquals('created_at', $columns[1]->getName());
$this->assertEquals('datetime', $columns[1]->getType());
$this->assertEquals('CURRENT_TIMESTAMP', $columns[1]->getDefault());
$this->assertEquals('', $columns[1]->getUpdate());

$this->assertEquals('updated_at', $columns[2]->getName());
$this->assertEquals('datetime', $columns[2]->getType());
$this->assertEquals('CURRENT_TIMESTAMP', $columns[2]->getUpdate());
$this->assertTrue($columns[2]->isNull());
$this->assertNull($columns[2]->getDefault());
}

public function testCreateTableWithLimitPK()
{
$table = new Table('ntable', ['id' => 'id', 'limit' => 4], $this->adapter);
Expand Down

0 comments on commit 1302418

Please sign in to comment.