Skip to content

Commit

Permalink
Merge pull request #721 from cakephp/feat-timestamps-datetime
Browse files Browse the repository at this point in the history
Add datetime column support to addTimestamps()
  • Loading branch information
markstory authored Jun 4, 2024
2 parents c3eef97 + 4635532 commit 266c086
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
6 changes: 4 additions & 2 deletions docs/en/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,10 @@ Feature Flags
Migrations uses Phinx, which has some feature flags that are disabled by default for now, but
can enabled if you want them to:

* ``unsigned_primary_keys``: Should Phinx create primary keys as unsigned integers? (default: ``false``)
* ``column_null_default``: Should Phinx create columns as null by default? (default: ``false``)
* ``unsigned_primary_keys``: Should Migrations create primary keys as unsigned integers? (default: ``false``)
* ``column_null_default``: Should Migrations create columns as null by default? (default: ``false``)
* ``add_timestamps_use_datetime``: Should Migrations use ``DATETIME`` type
columns for the columns added by ``addTimestamps()``.

Set them via Configure to enable (e.g. in ``config/app.php``)::

Expand Down
3 changes: 3 additions & 0 deletions src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ public function getColumns(string $tableName): array
if ($columnInfo['Extra'] === 'auto_increment') {
$column->setIdentity(true);
}
if ($columnInfo['Extra'] === 'on update CURRENT_TIMESTAMP') {
$column->setUpdate('CURRENT_TIMESTAMP');
}

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

namespace Migrations\Db;

use Cake\Core\Configure;
use InvalidArgumentException;
use Migrations\Db\Action\AddColumn;
use Migrations\Db\Action\AddForeignKey;
Expand Down Expand Up @@ -35,6 +36,8 @@
*
* TODO(mark) Having both Migrations\Db\Table and Migrations\Db\Table\Table seems redundant.
* The table models should be joined together so that we have a simpler API exposed.
*
* @internal
*/
class Table
{
Expand Down Expand Up @@ -543,17 +546,22 @@ public function addTimestamps(string|false|null $createdAt = 'created_at', strin
if (!$createdAt && !$updatedAt) {
throw new RuntimeException('Cannot set both created_at and updated_at columns to false');
}
$timestampConfig = (bool)Configure::read('Migrations.add_timestamps_use_datetime');
$timestampType = 'timestamp';
if ($timestampConfig === true) {
$timestampType = 'datetime';
}

if ($createdAt) {
$this->addColumn($createdAt, 'timestamp', [
$this->addColumn($createdAt, $timestampType, [
'null' => false,
'default' => 'CURRENT_TIMESTAMP',
'update' => '',
'timezone' => $withTimezone,
]);
}
if ($updatedAt) {
$this->addColumn($updatedAt, 'timestamp', [
$this->addColumn($updatedAt, $timestampType, [
'null' => true,
'default' => null,
'update' => 'CURRENT_TIMESTAMP',
Expand Down
3 changes: 3 additions & 0 deletions src/Db/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

use InvalidArgumentException;

/**
* @internal
*/
class Table
{
/**
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,36 @@ public function testUnsignedPksFeatureFlag()
$this->assertTrue($columns[0]->getSigned());
}

/**
* @runInSeparateProcess
*/
public function testAddTimestampsFeatureFlag()
{
Configure::write('Migrations.add_timestamps_use_datetime', true);
$this->adapter->connect();

$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('', $columns[1]->getUpdate());
$this->assertFalse($columns[1]->isNull());
$this->assertEquals('CURRENT_TIMESTAMP', $columns[1]->getDefault());

$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 266c086

Please sign in to comment.