Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ON UPDATE CURRENT_TIMESTAMP in MySQL #1967

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@ -517,6 +518,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
5 changes: 5 additions & 0 deletions src/Propel/Generator/Reverse/MysqlSchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ public function getColumnFromRow(array $row, Table $table): Column
// BLOBs can't have any default values in MySQL
$default = preg_match('~blob|text~', $nativeType) ? null : $row['Default'];

$extra = $row['Extra'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this is necessary here? When I look at the code, it does not look like $row is changed between here and line 300, where you use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to take it out of the row just once in case it is used multiple times so that there is only one index access for Extra. But it turned out to be used only once, so I could move it to line 300 as well. However, it might be easier to read the way it is now. Please let me know what you prefer and I'll adjust it accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Personally, I'd prefer it in line 300, as there is no benefit to moving it further up at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. I changed it accordingly.


$propelType = $this->getMappedPropelType($nativeType);
if (!$propelType) {
$propelType = Column::DEFAULT_TYPE;
Expand Down Expand Up @@ -295,6 +297,9 @@ public function getColumnFromRow(array $row, Table $table): Column
}
if (in_array($default, ['CURRENT_TIMESTAMP', 'current_timestamp()'], true)) {
$default = 'CURRENT_TIMESTAMP';
if (strpos(strtolower($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());
}
}