Skip to content

Commit

Permalink
Merge pull request #1967 from Mau04/fix-on-update-timestamp
Browse files Browse the repository at this point in the history
Support ON UPDATE CURRENT_TIMESTAMP in MySQL
  • Loading branch information
gechetspr authored Dec 7, 2023
2 parents 8117e54 + 9ddf3c9 commit 6558606
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Propel/Generator/Platform/PgsqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Propel\Generator\Exception\EngineException;
use Propel\Generator\Model\Column;
use Propel\Generator\Model\ColumnDefaultValue;
use Propel\Generator\Model\Database;
use Propel\Generator\Model\Diff\ColumnDiff;
use Propel\Generator\Model\Diff\TableDiff;
Expand Down Expand Up @@ -515,6 +516,16 @@ public function getColumnDDL(Column $col): string
$ddl[] = $sqlType;
}

if (
$col->getDefaultValue()
&& $col->getDefaultValue()->isExpression()
&& $col->getDefaultValue()->getValue() === 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
) {
$col->setDefaultValue(
new ColumnDefaultValue('CURRENT_TIMESTAMP', ColumnDefaultValue::TYPE_EXPR),
);
}

$default = $this->getColumnDefaultValueDDL($col);
if ($default) {
$ddl[] = $default;
Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Platform/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public function getColumnDDL(Column $col): string
if (
$col->getDefaultValue()
&& $col->getDefaultValue()->isExpression()
&& $col->getDefaultValue()->getValue() === 'CURRENT_TIMESTAMP'
&& in_array($col->getDefaultValue()->getValue(), ['CURRENT_TIMESTAMP', 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'])
) {
//sqlite use CURRENT_TIMESTAMP different than mysql/pgsql etc
//we set it to the more common behavior
Expand Down
3 changes: 3 additions & 0 deletions src/Propel/Generator/Reverse/MysqlSchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ public function getColumnFromRow(array $row, Table $table): Column
}
if (in_array($default, ['CURRENT_TIMESTAMP', 'current_timestamp()'], true)) {
$default = 'CURRENT_TIMESTAMP';
if (strpos(strtolower($row['Extra']), 'on update current_timestamp') !== false) {
$default = 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
}
$type = ColumnDefaultValue::TYPE_EXPR;
} else {
$type = ColumnDefaultValue::TYPE_VALUE;
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/bookstore/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
<column name="enabled" type="BOOLEAN" default="true"/>
<column name="not_enabled" type="BOOLEAN" default="false"/>
<column name="created" type="TIMESTAMP" defaultExpr="CURRENT_TIMESTAMP"/>
<column name="updated" type="TIMESTAMP" defaultExpr="CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"/>
<column name="role_id" type="INTEGER" required="false" default="null"/>
<column name="authenticator" type="VARCHAR" size="32" defaultExpr="'Password'"/>
<foreign-key foreignTable="bookstore_employee" onDelete="cascade">
Expand Down
12 changes: 12 additions & 0 deletions tests/Propel/Tests/Generator/Reverse/MysqlSchemaParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Propel\Tests\Generator\Reverse;

use PDO;
use Propel\Generator\Model\ColumnDefaultValue;
use Propel\Generator\Reverse\MysqlSchemaParser;
use Propel\Tests\Bookstore\Map\BookTableMap;

Expand Down Expand Up @@ -81,4 +82,15 @@ public function testDescriptionsAreImported(): void
$this->assertEquals('Book Table', $bookTable->getDescription());
$this->assertEquals('Book Title', $bookTable->getColumn('title')->getDescription());
}

/**
* @return void
*/
public function testOnUpdateIsImported(): void
{
$onUpdateTable = $this->parsedDatabase->getTable('bookstore_employee_account');
$updatedAtColumn = $onUpdateTable->getColumn('updated');
$this->assertEquals(ColumnDefaultValue::TYPE_EXPR, $updatedAtColumn->getDefaultValue()->getType());
$this->assertEquals('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', $updatedAtColumn->getDefaultValue()->getValue());
}
}

0 comments on commit 6558606

Please sign in to comment.