From aaa827bf4614a7f8b0b77d7475a5c13ed77143d8 Mon Sep 17 00:00:00 2001 From: Maurice Breit Date: Tue, 23 May 2023 08:10:25 +0200 Subject: [PATCH 1/4] mysql: read ON UPDATE CURRENT_TIMESTAMP default value correctly --- src/Propel/Generator/Reverse/MysqlSchemaParser.php | 5 +++++ tests/Fixtures/bookstore/schema.xml | 1 + .../Generator/Reverse/MysqlSchemaParserTest.php | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/Propel/Generator/Reverse/MysqlSchemaParser.php b/src/Propel/Generator/Reverse/MysqlSchemaParser.php index f7e1796b9d..cfdcf91b40 100644 --- a/src/Propel/Generator/Reverse/MysqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/MysqlSchemaParser.php @@ -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']; + $propelType = $this->getMappedPropelType($nativeType); if (!$propelType) { $propelType = Column::DEFAULT_TYPE; @@ -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; diff --git a/tests/Fixtures/bookstore/schema.xml b/tests/Fixtures/bookstore/schema.xml index e9798e9399..d1fd6e58d4 100644 --- a/tests/Fixtures/bookstore/schema.xml +++ b/tests/Fixtures/bookstore/schema.xml @@ -174,6 +174,7 @@ + diff --git a/tests/Propel/Tests/Generator/Reverse/MysqlSchemaParserTest.php b/tests/Propel/Tests/Generator/Reverse/MysqlSchemaParserTest.php index 607a51f9f6..cb3e42e6ad 100644 --- a/tests/Propel/Tests/Generator/Reverse/MysqlSchemaParserTest.php +++ b/tests/Propel/Tests/Generator/Reverse/MysqlSchemaParserTest.php @@ -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; @@ -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()); + } } From 341baa44218ee681a3783fd35ce00a0695816ef8 Mon Sep 17 00:00:00 2001 From: Maurice Breit Date: Tue, 23 May 2023 09:10:17 +0200 Subject: [PATCH 2/4] adjust for pgsql & sqlite --- src/Propel/Generator/Platform/PgsqlPlatform.php | 11 +++++++++++ src/Propel/Generator/Platform/SqlitePlatform.php | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Propel/Generator/Platform/PgsqlPlatform.php b/src/Propel/Generator/Platform/PgsqlPlatform.php index be987eb659..408c039329 100755 --- a/src/Propel/Generator/Platform/PgsqlPlatform.php +++ b/src/Propel/Generator/Platform/PgsqlPlatform.php @@ -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; @@ -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; diff --git a/src/Propel/Generator/Platform/SqlitePlatform.php b/src/Propel/Generator/Platform/SqlitePlatform.php index 5484c7abdc..0edb834f41 100644 --- a/src/Propel/Generator/Platform/SqlitePlatform.php +++ b/src/Propel/Generator/Platform/SqlitePlatform.php @@ -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 From b7297428b5b48daeacf41df929eaf8c86aa9d906 Mon Sep 17 00:00:00 2001 From: Maurice Breit Date: Tue, 23 May 2023 09:21:20 +0200 Subject: [PATCH 3/4] handle mariadb & mysql --- src/Propel/Generator/Reverse/MysqlSchemaParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Propel/Generator/Reverse/MysqlSchemaParser.php b/src/Propel/Generator/Reverse/MysqlSchemaParser.php index cfdcf91b40..ab416116d5 100644 --- a/src/Propel/Generator/Reverse/MysqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/MysqlSchemaParser.php @@ -297,7 +297,7 @@ 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) { + if (strpos(strtolower($extra), 'on update current_timestamp') !== false) { $default = 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'; } $type = ColumnDefaultValue::TYPE_EXPR; From 9a64308e3970bf41f928aeb948d40e0219632101 Mon Sep 17 00:00:00 2001 From: Maurice Breit Date: Thu, 25 May 2023 09:22:34 +0200 Subject: [PATCH 4/4] remove extra variable --- src/Propel/Generator/Reverse/MysqlSchemaParser.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Propel/Generator/Reverse/MysqlSchemaParser.php b/src/Propel/Generator/Reverse/MysqlSchemaParser.php index ab416116d5..0e1350739b 100644 --- a/src/Propel/Generator/Reverse/MysqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/MysqlSchemaParser.php @@ -264,8 +264,6 @@ 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']; - $propelType = $this->getMappedPropelType($nativeType); if (!$propelType) { $propelType = Column::DEFAULT_TYPE; @@ -297,7 +295,7 @@ 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) { + if (strpos(strtolower($row['Extra']), 'on update current_timestamp') !== false) { $default = 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'; } $type = ColumnDefaultValue::TYPE_EXPR;