From 4714b79ff7d7cccd8b2c25c01ab1c3f1cb68535b Mon Sep 17 00:00:00 2001 From: mringler Date: Sun, 11 Dec 2022 15:46:08 +0100 Subject: [PATCH 1/4] re-enable agnostic tests --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bb7b9e59b..f95fe018f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -117,8 +117,7 @@ jobs: if: matrix.db-type != 'agnostic' run: tests/bin/setup.${{ matrix.db-type }}.sh - - name: Run database tests - if: matrix.db-type != 'agnostic' + - name: Run tests shell: 'script -q -e -c "bash {0}"' run: | if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.symfony-version == '5-max' }} ]]; then From ce19ceae7312e36630c5640b3d8edef2d13c0854 Mon Sep 17 00:00:00 2001 From: mringler Date: Sat, 10 Dec 2022 19:40:03 +0100 Subject: [PATCH 2/4] Split up platform initialization --- src/Propel/Generator/Platform/DefaultPlatform.php | 10 +++++++++- src/Propel/Generator/Platform/MysqlPlatform.php | 4 ++-- src/Propel/Generator/Platform/OraclePlatform.php | 4 ++-- src/Propel/Generator/Platform/PgsqlPlatform.php | 4 ++-- src/Propel/Generator/Platform/SqlitePlatform.php | 12 ++++++++++-- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Propel/Generator/Platform/DefaultPlatform.php b/src/Propel/Generator/Platform/DefaultPlatform.php index 06fcc53c3..70c75f41a 100644 --- a/src/Propel/Generator/Platform/DefaultPlatform.php +++ b/src/Propel/Generator/Platform/DefaultPlatform.php @@ -128,12 +128,20 @@ public function setGeneratorConfig(GeneratorConfigInterface $generatorConfig): v { } + /** + * @return void + */ + protected function initialize(): void + { + $this->initializeTypeMap(); + } + /** * Initialize the type -> Domain mapping. * * @return void */ - protected function initialize(): void + protected function initializeTypeMap(): void { $this->schemaDomainMap = []; foreach (PropelTypes::getPropelTypes() as $type) { diff --git a/src/Propel/Generator/Platform/MysqlPlatform.php b/src/Propel/Generator/Platform/MysqlPlatform.php index 901e33fca..91470ead8 100644 --- a/src/Propel/Generator/Platform/MysqlPlatform.php +++ b/src/Propel/Generator/Platform/MysqlPlatform.php @@ -51,9 +51,9 @@ class MysqlPlatform extends DefaultPlatform * * @return void */ - protected function initialize(): void + protected function initializeTypeMap(): void { - parent::initialize(); + parent::initializeTypeMap(); $this->setSchemaDomainMapping(new Domain(PropelTypes::BOOLEAN, 'TINYINT', 1)); $this->setSchemaDomainMapping(new Domain(PropelTypes::NUMERIC, 'DECIMAL')); $this->setSchemaDomainMapping(new Domain(PropelTypes::LONGVARCHAR, 'TEXT')); diff --git a/src/Propel/Generator/Platform/OraclePlatform.php b/src/Propel/Generator/Platform/OraclePlatform.php index 1862dbacc..81f0af288 100644 --- a/src/Propel/Generator/Platform/OraclePlatform.php +++ b/src/Propel/Generator/Platform/OraclePlatform.php @@ -33,9 +33,9 @@ class OraclePlatform extends DefaultPlatform * * @return void */ - protected function initialize(): void + protected function initializeTypeMap(): void { - parent::initialize(); + parent::initializeTypeMap(); $this->schemaDomainMap[PropelTypes::BOOLEAN] = new Domain(PropelTypes::BOOLEAN_EMU, 'NUMBER', 1, 0); $this->schemaDomainMap[PropelTypes::CLOB] = new Domain(PropelTypes::CLOB_EMU, 'CLOB'); $this->schemaDomainMap[PropelTypes::CLOB_EMU] = $this->schemaDomainMap[PropelTypes::CLOB]; diff --git a/src/Propel/Generator/Platform/PgsqlPlatform.php b/src/Propel/Generator/Platform/PgsqlPlatform.php index e5db9cf8b..ccc10aa02 100755 --- a/src/Propel/Generator/Platform/PgsqlPlatform.php +++ b/src/Propel/Generator/Platform/PgsqlPlatform.php @@ -40,9 +40,9 @@ class PgsqlPlatform extends DefaultPlatform * * @return void */ - protected function initialize(): void + protected function initializeTypeMap(): void { - parent::initialize(); + parent::initializeTypeMap(); $this->setSchemaDomainMapping(new Domain(PropelTypes::BOOLEAN, 'BOOLEAN')); $this->setSchemaDomainMapping(new Domain(PropelTypes::TINYINT, 'INT2')); $this->setSchemaDomainMapping(new Domain(PropelTypes::SMALLINT, 'INT2')); diff --git a/src/Propel/Generator/Platform/SqlitePlatform.php b/src/Propel/Generator/Platform/SqlitePlatform.php index 91a152562..ea5a9be2d 100644 --- a/src/Propel/Generator/Platform/SqlitePlatform.php +++ b/src/Propel/Generator/Platform/SqlitePlatform.php @@ -47,8 +47,6 @@ class SqlitePlatform extends DefaultPlatform protected $tableAlteringWorkaround = true; /** - * Initializes db specific domain mapping. - * * @return void */ protected function initialize(): void @@ -58,6 +56,16 @@ protected function initialize(): void $version = $this->getVersion(); $this->foreignKeySupport = version_compare($version, '3.6.19') >= 0; + } + + /** + * Initializes db specific domain mapping. + * + * @return void + */ + protected function initializeTypeMap(): void + { + parent::initializeTypeMap(); $this->setSchemaDomainMapping(new Domain(PropelTypes::NUMERIC, 'DECIMAL')); $this->setSchemaDomainMapping(new Domain(PropelTypes::LONGVARCHAR, 'MEDIUMTEXT')); From c4fa30fc535fca875348d642fbc59714cc87f427 Mon Sep 17 00:00:00 2001 From: mringler Date: Sat, 10 Dec 2022 19:43:08 +0100 Subject: [PATCH 3/4] cleanup --- src/Propel/Generator/Model/Column.php | 65 +++++++++---------- src/Propel/Generator/Model/Table.php | 44 ++++++------- .../Generator/Platform/MysqlPlatform.php | 8 --- .../Propel/Tests/Helpers/CheckMysql8Trait.php | 2 +- 4 files changed, 52 insertions(+), 67 deletions(-) diff --git a/src/Propel/Generator/Model/Column.php b/src/Propel/Generator/Model/Column.php index 5fe7c0c71..aba5005ca 100644 --- a/src/Propel/Generator/Model/Column.php +++ b/src/Propel/Generator/Model/Column.php @@ -260,6 +260,27 @@ public function setTypeHint(?string $typeHint): void $this->typeHint = $typeHint; } + /** + * @param \Propel\Generator\Platform\PlatformInterface|null $platform + * + * @return \Propel\Generator\Model\Domain + */ + protected function getDomainFromAttributes(?PlatformInterface $platform): Domain + { + $domainName = $this->getAttribute('domain'); + if ($domainName) { + return $this->getDatabase()->getDomain($domainName); + } + $type = $this->getAttribute('type', static::DEFAULT_TYPE); + $type = strtoupper($type); + if ($platform) { + return $platform->getDomainForType($type); + } + + // no platform - probably during tests + return new Domain($type); + } + /** * @throws \Propel\Generator\Exception\EngineException * @@ -269,34 +290,11 @@ protected function setupObject(): void { try { $database = $this->getDatabase(); - $domain = $this->getDomain(); - - $platform = null; - if ($this->hasPlatform()) { - $platform = $this->getPlatform(); - } + $platform = ($this->hasPlatform()) ? $this->getPlatform() : null; - $dom = $this->getAttribute('domain'); - if ($dom) { - $domain->copy($database->getDomain($dom)); - } else { - if ($this->getAttribute('type')) { - $type = strtoupper($this->getAttribute('type')); - if ($platform) { - $domain->copy($platform->getDomainForType($type)); - } else { - // no platform - probably during tests - $this->setDomain(new Domain($type)); - } - } else { - if ($platform) { - $domain->copy($platform->getDomainForType(self::DEFAULT_TYPE)); - } else { - // no platform - probably during tests - $this->setDomain(new Domain(self::DEFAULT_TYPE)); - } - } - } + $domain = $this->getDomain(); + $domainInAttributes = $this->getDomainFromAttributes($platform); + $domain->copy($domainInAttributes); $this->name = $this->getAttribute('name'); $this->phpName = $this->getAttribute('phpName'); @@ -312,10 +310,7 @@ protected function setupObject(): void */ $this->phpNamingMethod = $this->getAttribute('phpNamingMethod', $database->getDefaultPhpNamingMethod()); - $this->namePrefix = $this->getAttribute( - 'prefix', - $this->parentTable->getAttribute('columnPrefix'), - ); + $this->namePrefix = $this->getAttribute('prefix', $this->parentTable->getAttribute('columnPrefix')); // Accessor visibility - no idea why this returns null, or the use case for that $visibility = $this->getMethodVisibility('accessorVisibility', 'defaultAccessorVisibility') ?: ''; @@ -433,13 +428,11 @@ private function getDatabase(): ?Database */ public function getDomain(): Domain { - $domain = $this->domain; - if ($domain === null) { - $domain = new Domain(); - $this->domain = $domain; + if ($this->domain === null) { + $this->domain = new Domain(); } - return $domain; + return $this->domain; } /** diff --git a/src/Propel/Generator/Model/Table.php b/src/Propel/Generator/Model/Table.php index f9e94ecfd..c44450baa 100644 --- a/src/Propel/Generator/Model/Table.php +++ b/src/Propel/Generator/Model/Table.php @@ -565,35 +565,35 @@ public function setBaseQueryClass(string $class): void */ public function addColumn($col): Column { - if ($col instanceof Column) { - if (isset($this->columnsByName[$col->getName()])) { - throw new EngineException(sprintf('Column "%s" declared twice in table "%s"', $col->getName(), $this->getName())); - } - - $col->setTable($this); + if (is_array($col)) { + $column = new Column($col['name']); + $column->setTable($this); + $column->loadMapping($col); - if ($col->isInheritance()) { - $this->inheritanceColumn = $col; - } + $col = $column; + } - $this->columns[] = $col; - $this->columnsByName[(string)$col->getName()] = $col; - $this->columnsByLowercaseName[strtolower((string)$col->getName())] = $col; - $this->columnsByPhpName[(string)$col->getPhpName()] = $col; - $col->setPosition(count($this->columns)); + if (isset($this->columnsByName[$col->getName()])) { + throw new EngineException(sprintf('Column "%s" declared twice in table "%s"', $col->getName(), $this->getName())); + } - if ($col->requiresTransactionInPostgres()) { - $this->needsTransactionInPostgres = true; - } + $col->setTable($this); - return $col; + if ($col->isInheritance()) { + $this->inheritanceColumn = $col; } - $column = new Column($col['name']); - $column->setTable($this); - $column->loadMapping($col); + $this->columns[] = $col; + $this->columnsByName[(string)$col->getName()] = $col; + $this->columnsByLowercaseName[strtolower((string)$col->getName())] = $col; + $this->columnsByPhpName[(string)$col->getPhpName()] = $col; + $col->setPosition(count($this->columns)); + + if ($col->requiresTransactionInPostgres()) { + $this->needsTransactionInPostgres = true; + } - return $this->addColumn($column); // call self w/ different param + return $col; } /** diff --git a/src/Propel/Generator/Platform/MysqlPlatform.php b/src/Propel/Generator/Platform/MysqlPlatform.php index 91470ead8..2d9a2219e 100644 --- a/src/Propel/Generator/Platform/MysqlPlatform.php +++ b/src/Propel/Generator/Platform/MysqlPlatform.php @@ -89,8 +89,6 @@ public function setGeneratorConfig(GeneratorConfigInterface $generatorConfig): v } /** - * Setter for the tableEngineKeyword property - * * @param string $tableEngineKeyword * * @return void @@ -101,8 +99,6 @@ public function setTableEngineKeyword(string $tableEngineKeyword): void } /** - * Getter for the tableEngineKeyword property - * * @return string */ public function getTableEngineKeyword(): string @@ -111,8 +107,6 @@ public function getTableEngineKeyword(): string } /** - * Setter for the defaultTableEngine property - * * @param string $defaultTableEngine * * @return void @@ -123,8 +117,6 @@ public function setDefaultTableEngine(string $defaultTableEngine): void } /** - * Getter for the defaultTableEngine property - * * @return string */ public function getDefaultTableEngine(): string diff --git a/tests/Propel/Tests/Helpers/CheckMysql8Trait.php b/tests/Propel/Tests/Helpers/CheckMysql8Trait.php index 49bde0117..8392de7d7 100644 --- a/tests/Propel/Tests/Helpers/CheckMysql8Trait.php +++ b/tests/Propel/Tests/Helpers/CheckMysql8Trait.php @@ -13,7 +13,7 @@ trait CheckMysql8Trait { /** - * @var ?bool|null + * @var bool|null */ protected static $isAtLeastMysql8 = null; From 60873cc10f67b4fd3c10574c806c91aa7a72550f Mon Sep 17 00:00:00 2001 From: mringler Date: Sat, 10 Dec 2022 19:48:28 +0100 Subject: [PATCH 4/4] Set UUID column type for MySQL in configuration --- .../Common/Config/PropelConfiguration.php | 1 + .../Generator/Platform/MysqlPlatform.php | 46 +++++++++++++++++-- .../Generator/Reverse/MysqlSchemaParser.php | 1 + .../Generator/Platform/MysqlPlatformTest.php | 36 ++++++++++++--- 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/src/Propel/Common/Config/PropelConfiguration.php b/src/Propel/Common/Config/PropelConfiguration.php index 0099f03a9..5cf62a7c0 100644 --- a/src/Propel/Common/Config/PropelConfiguration.php +++ b/src/Propel/Common/Config/PropelConfiguration.php @@ -180,6 +180,7 @@ protected function addDatabaseSection(ArrayNodeDefinition $node): void ->children() ->scalarNode('tableType')->defaultValue('InnoDB')->treatNullLike('InnoDB')->end() ->scalarNode('tableEngineKeyword')->defaultValue('ENGINE')->end() + ->scalarNode('uuidColumnType')->defaultValue('binary')->end() ->end() ->end() ->arrayNode('sqlite') diff --git a/src/Propel/Generator/Platform/MysqlPlatform.php b/src/Propel/Generator/Platform/MysqlPlatform.php index 2d9a2219e..b7950c64f 100644 --- a/src/Propel/Generator/Platform/MysqlPlatform.php +++ b/src/Propel/Generator/Platform/MysqlPlatform.php @@ -46,6 +46,11 @@ class MysqlPlatform extends DefaultPlatform */ protected $serverVersion; + /** + * @var bool + */ + protected $useUuidNativeType = false; + /** * Initializes db specific domain mapping. * @@ -68,8 +73,7 @@ protected function initializeTypeMap(): void $this->setSchemaDomainMapping(new Domain(PropelTypes::REAL, 'DOUBLE')); $this->setSchemaDomainMapping(new Domain(PropelTypes::UUID_BINARY, 'BINARY', 16)); - // no native UUID type, use UUID_BINARY - $this->schemaDomainMap[PropelTypes::UUID] = $this->schemaDomainMap[PropelTypes::UUID_BINARY]; + $this->setUuidTypeMapping(); } /** @@ -80,12 +84,46 @@ protected function initializeTypeMap(): void public function setGeneratorConfig(GeneratorConfigInterface $generatorConfig): void { parent::setGeneratorConfig($generatorConfig); - if ($defaultTableEngine = $generatorConfig->get()['database']['adapters']['mysql']['tableType']) { + + $mysqlConfig = $generatorConfig->get()['database']['adapters']['mysql']; + + if ($defaultTableEngine = $mysqlConfig['tableType']) { $this->defaultTableEngine = $defaultTableEngine; } - if ($tableEngineKeyword = $generatorConfig->get()['database']['adapters']['mysql']['tableEngineKeyword']) { + if ($tableEngineKeyword = $mysqlConfig['tableEngineKeyword']) { $this->tableEngineKeyword = $tableEngineKeyword; } + if ($uuidColumnType = $mysqlConfig['uuidColumnType']) { + $enable = strtolower($uuidColumnType) === 'native'; + $this->setUuidNativeType($enable); + } + } + + /** + * @param bool $enable + * + * @return void + */ + public function setUuidNativeType(bool $enable): void + { + $this->useUuidNativeType = $enable; + $this->setUuidTypeMapping(); + } + + /** + * Set column type for UUIDs according to MysqlPlatform::useUuidNativeType. + * + * Currently, only MariaDB has a native UUID type. + * + * @return void + */ + protected function setUuidTypeMapping(): void + { + $domain = ($this->useUuidNativeType) + ? new Domain(PropelTypes::UUID, 'UUID') + : $this->schemaDomainMap[PropelTypes::UUID_BINARY]; + + $this->schemaDomainMap[PropelTypes::UUID] = $domain; } /** diff --git a/src/Propel/Generator/Reverse/MysqlSchemaParser.php b/src/Propel/Generator/Reverse/MysqlSchemaParser.php index b473faa35..9db92823f 100644 --- a/src/Propel/Generator/Reverse/MysqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/MysqlSchemaParser.php @@ -68,6 +68,7 @@ class MysqlSchemaParser extends AbstractSchemaParser 'enum' => PropelTypes::CHAR, 'set' => PropelTypes::CHAR, 'binary' => PropelTypes::BINARY, + 'uuid' => PropelTypes::UUID, // for MariaDB ]; /** diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php index 845ff5f0e..f75fef81b 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php @@ -836,15 +836,13 @@ public function testAddExtraIndicesForeignKeys() $schema = ' - - +
- - + @@ -857,10 +855,8 @@ public function testAddExtraIndicesForeignKeys() $expectedRelationSql = " CREATE TABLE `bar` ( - `name` VARCHAR(255), `subid` INTEGER, - `id` INTEGER NOT NULL AUTO_INCREMENT, - PRIMARY KEY (`id`), + `id` INTEGER, INDEX `bar_fi_bb8268` (`id`, `subid`), CONSTRAINT `bar_fk_bb8268` FOREIGN KEY (`id`,`subid`) @@ -1013,4 +1009,30 @@ public function testCreateSchemaWithUuidBinaryColumns($schema) $this->assertCreateTableMatches($expected, $schema); } + + /** + * @return void + */ + public function testUuidColumnTypeDefaultsToBinary() + { + $platform = new MysqlPlatform(); + + $uuidSqlType = $platform->getDomainForType(PropelTypes::UUID)->getSqlType(); + $this->assertEquals(PropelTypes::BINARY, $uuidSqlType); + } + + /** + * @return void + */ + public function testEnableUuidNativeType() + { + $platform = new MysqlPlatform(); + + $configProp['propel']['database']['adapters']['mysql']['uuidColumnType'] = 'native'; + $config = new GeneratorConfig(__DIR__ . '/../../../../Fixtures/bookstore', $configProp); + $platform->setGeneratorConfig($config); + + $uuidSqlType = $platform->getDomainForType(PropelTypes::UUID)->getSqlType(); + $this->assertEquals(PropelTypes::UUID, $uuidSqlType); + } }