Skip to content

Commit 6558606

Browse files
authored
Merge pull request #1967 from Mau04/fix-on-update-timestamp
Support ON UPDATE CURRENT_TIMESTAMP in MySQL
2 parents 8117e54 + 9ddf3c9 commit 6558606

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

src/Propel/Generator/Platform/PgsqlPlatform.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Propel\Generator\Exception\EngineException;
1212
use Propel\Generator\Model\Column;
13+
use Propel\Generator\Model\ColumnDefaultValue;
1314
use Propel\Generator\Model\Database;
1415
use Propel\Generator\Model\Diff\ColumnDiff;
1516
use Propel\Generator\Model\Diff\TableDiff;
@@ -515,6 +516,16 @@ public function getColumnDDL(Column $col): string
515516
$ddl[] = $sqlType;
516517
}
517518

519+
if (
520+
$col->getDefaultValue()
521+
&& $col->getDefaultValue()->isExpression()
522+
&& $col->getDefaultValue()->getValue() === 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
523+
) {
524+
$col->setDefaultValue(
525+
new ColumnDefaultValue('CURRENT_TIMESTAMP', ColumnDefaultValue::TYPE_EXPR),
526+
);
527+
}
528+
518529
$default = $this->getColumnDefaultValueDDL($col);
519530
if ($default) {
520531
$ddl[] = $default;

src/Propel/Generator/Platform/SqlitePlatform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public function getColumnDDL(Column $col): string
492492
if (
493493
$col->getDefaultValue()
494494
&& $col->getDefaultValue()->isExpression()
495-
&& $col->getDefaultValue()->getValue() === 'CURRENT_TIMESTAMP'
495+
&& in_array($col->getDefaultValue()->getValue(), ['CURRENT_TIMESTAMP', 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'])
496496
) {
497497
//sqlite use CURRENT_TIMESTAMP different than mysql/pgsql etc
498498
//we set it to the more common behavior

src/Propel/Generator/Reverse/MysqlSchemaParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ public function getColumnFromRow(array $row, Table $table): Column
295295
}
296296
if (in_array($default, ['CURRENT_TIMESTAMP', 'current_timestamp()'], true)) {
297297
$default = 'CURRENT_TIMESTAMP';
298+
if (strpos(strtolower($row['Extra']), 'on update current_timestamp') !== false) {
299+
$default = 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
300+
}
298301
$type = ColumnDefaultValue::TYPE_EXPR;
299302
} else {
300303
$type = ColumnDefaultValue::TYPE_VALUE;

tests/Fixtures/bookstore/schema.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
<column name="enabled" type="BOOLEAN" default="true"/>
175175
<column name="not_enabled" type="BOOLEAN" default="false"/>
176176
<column name="created" type="TIMESTAMP" defaultExpr="CURRENT_TIMESTAMP"/>
177+
<column name="updated" type="TIMESTAMP" defaultExpr="CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"/>
177178
<column name="role_id" type="INTEGER" required="false" default="null"/>
178179
<column name="authenticator" type="VARCHAR" size="32" defaultExpr="'Password'"/>
179180
<foreign-key foreignTable="bookstore_employee" onDelete="cascade">

tests/Propel/Tests/Generator/Reverse/MysqlSchemaParserTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Propel\Tests\Generator\Reverse;
1010

1111
use PDO;
12+
use Propel\Generator\Model\ColumnDefaultValue;
1213
use Propel\Generator\Reverse\MysqlSchemaParser;
1314
use Propel\Tests\Bookstore\Map\BookTableMap;
1415

@@ -81,4 +82,15 @@ public function testDescriptionsAreImported(): void
8182
$this->assertEquals('Book Table', $bookTable->getDescription());
8283
$this->assertEquals('Book Title', $bookTable->getColumn('title')->getDescription());
8384
}
85+
86+
/**
87+
* @return void
88+
*/
89+
public function testOnUpdateIsImported(): void
90+
{
91+
$onUpdateTable = $this->parsedDatabase->getTable('bookstore_employee_account');
92+
$updatedAtColumn = $onUpdateTable->getColumn('updated');
93+
$this->assertEquals(ColumnDefaultValue::TYPE_EXPR, $updatedAtColumn->getDefaultValue()->getType());
94+
$this->assertEquals('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', $updatedAtColumn->getDefaultValue()->getValue());
95+
}
8496
}

0 commit comments

Comments
 (0)