diff --git a/src/Propel/Generator/Model/Column.php b/src/Propel/Generator/Model/Column.php index a6f38b219f..ff86938874 100644 --- a/src/Propel/Generator/Model/Column.php +++ b/src/Propel/Generator/Model/Column.php @@ -1323,6 +1323,16 @@ public function isTemporalType(): bool return PropelTypes::isTemporalType($this->getType()); } + /** + * Returns whether this column is a uuid type. + * + * @return bool + */ + public function isUuidType(): bool + { + return PropelTypes::isUuidType($this->getType()); + } + /** * Returns whether the column is an array column. * diff --git a/src/Propel/Generator/Model/PropelTypes.php b/src/Propel/Generator/Model/PropelTypes.php index cf364f7a84..276232a0e3 100644 --- a/src/Propel/Generator/Model/PropelTypes.php +++ b/src/Propel/Generator/Model/PropelTypes.php @@ -341,6 +341,16 @@ class PropelTypes */ public const JSON_TYPE = 'string'; + /** + * @var string + */ + public const UUID = 'UUID'; + + /** + * @var string + */ + public const UUID_NATIVE_TYPE = 'string'; + /** * Propel mapping types. * @@ -381,6 +391,7 @@ class PropelTypes self::BU_TIMESTAMP, self::SET, self::JSON, + self::UUID, ]; /** @@ -421,6 +432,7 @@ class PropelTypes self::SET => self::SET_NATIVE_TYPE, self::GEOMETRY => self::GEOMETRY, self::JSON => self::JSON_TYPE, + self::UUID => self::UUID_NATIVE_TYPE, ]; /** @@ -466,6 +478,7 @@ class PropelTypes self::BU_DATE => PDO::PARAM_STR, self::BU_TIMESTAMP => PDO::PARAM_STR, self::JSON => PDO::PARAM_STR, + self::UUID => PDO::PARAM_STR, ]; /** @@ -542,7 +555,7 @@ public static function isTemporalType(string $type): bool self::TIMESTAMP, self::BU_DATE, self::BU_TIMESTAMP, - ]); + ], true); } /** @@ -566,7 +579,7 @@ public static function isTextType(string $mappingType): bool self::BU_DATE, self::BU_TIMESTAMP, self::JSON, - ]); + ], true); } /** @@ -588,7 +601,7 @@ public static function isNumericType(string $mappingType): bool self::NUMERIC, self::DECIMAL, self::REAL, - ]); + ], true); } /** @@ -600,7 +613,7 @@ public static function isNumericType(string $mappingType): bool */ public static function isBooleanType(string $mappingType): bool { - return in_array($mappingType, [self::BOOLEAN, self::BOOLEAN_EMU]); + return in_array($mappingType, [self::BOOLEAN, self::BOOLEAN_EMU], true); } /** @@ -612,7 +625,21 @@ public static function isBooleanType(string $mappingType): bool */ public static function isLobType(string $mappingType): bool { - return in_array($mappingType, [self::VARBINARY, self::LONGVARBINARY, self::BLOB, self::OBJECT, self::GEOMETRY]); + return in_array($mappingType, [self::VARBINARY, self::LONGVARBINARY, self::BLOB, self::OBJECT, self::GEOMETRY], true); + } + + /** + * Returns whether the given type is a UUID type. + * + * @param string $type + * + * @return bool + */ + public static function isUuidType(string $type): bool + { + return in_array($type, [ + self::UUID, + ], true); } /** @@ -624,7 +651,7 @@ public static function isLobType(string $mappingType): bool */ public static function isPhpPrimitiveType(string $phpType): bool { - return in_array($phpType, ['boolean', 'int', 'double', 'float', 'string']); + return in_array($phpType, ['boolean', 'int', 'double', 'float', 'string'], true); } /** @@ -636,7 +663,7 @@ public static function isPhpPrimitiveType(string $phpType): bool */ public static function isPhpPrimitiveNumericType(string $phpType): bool { - return in_array($phpType, ['boolean', 'int', 'double', 'float']); + return in_array($phpType, ['boolean', 'int', 'double', 'float'], true); } /** @@ -648,7 +675,7 @@ public static function isPhpPrimitiveNumericType(string $phpType): bool */ public static function isPhpObjectType(string $phpType): bool { - return !self::isPhpPrimitiveType($phpType) && !in_array($phpType, ['resource', 'array']); + return !self::isPhpPrimitiveType($phpType) && !in_array($phpType, ['resource', 'array'], true); } /** diff --git a/src/Propel/Generator/Platform/MssqlPlatform.php b/src/Propel/Generator/Platform/MssqlPlatform.php index ba5e33c462..ff2cfcb637 100644 --- a/src/Propel/Generator/Platform/MssqlPlatform.php +++ b/src/Propel/Generator/Platform/MssqlPlatform.php @@ -57,6 +57,7 @@ protected function initialize(): void $this->setSchemaDomainMapping(new Domain(PropelTypes::PHP_ARRAY, 'VARCHAR(MAX)')); $this->setSchemaDomainMapping(new Domain(PropelTypes::ENUM, 'TINYINT')); $this->setSchemaDomainMapping(new Domain(PropelTypes::SET, 'INT')); + $this->setSchemaDomainMapping(new Domain(PropelTypes::UUID, 'UNIQUEIDENTIFIER')); } /** diff --git a/src/Propel/Generator/Platform/MysqlPlatform.php b/src/Propel/Generator/Platform/MysqlPlatform.php index 3c95f79958..435de420ae 100644 --- a/src/Propel/Generator/Platform/MysqlPlatform.php +++ b/src/Propel/Generator/Platform/MysqlPlatform.php @@ -53,6 +53,7 @@ class MysqlPlatform extends DefaultPlatform protected function initialize(): void { parent::initialize(); + unset($this->schemaDomainMap[PropelTypes::UUID]); $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 2df48ba199..13567f2a5d 100644 --- a/src/Propel/Generator/Platform/OraclePlatform.php +++ b/src/Propel/Generator/Platform/OraclePlatform.php @@ -60,6 +60,7 @@ protected function initialize(): void $this->setSchemaDomainMapping(new Domain(PropelTypes::PHP_ARRAY, 'NVARCHAR2', 2000)); $this->setSchemaDomainMapping(new Domain(PropelTypes::ENUM, 'NUMBER', 3, 0)); $this->setSchemaDomainMapping(new Domain(PropelTypes::SET, 'NUMBER')); + $this->setSchemaDomainMapping(new Domain(PropelTypes::UUID, 'UUID')); } /** diff --git a/src/Propel/Generator/Platform/PgsqlPlatform.php b/src/Propel/Generator/Platform/PgsqlPlatform.php index 96481f944f..fe13006646 100755 --- a/src/Propel/Generator/Platform/PgsqlPlatform.php +++ b/src/Propel/Generator/Platform/PgsqlPlatform.php @@ -62,6 +62,7 @@ protected function initialize(): void $this->setSchemaDomainMapping(new Domain(PropelTypes::SET, 'INT4')); $this->setSchemaDomainMapping(new Domain(PropelTypes::DECIMAL, 'NUMERIC')); $this->setSchemaDomainMapping(new Domain(PropelTypes::DATETIME, 'TIMESTAMP')); + $this->setSchemaDomainMapping(new Domain(PropelTypes::UUID, 'uuid')); } /** @@ -582,7 +583,7 @@ public function supportsSchemas(): bool */ public function hasSize(string $sqlType): bool { - return !in_array($sqlType, ['BYTEA', 'TEXT', 'DOUBLE PRECISION']); + return !in_array(strtoupper($sqlType), ['BYTEA', 'TEXT', 'DOUBLE PRECISION'], true); } /** @@ -670,9 +671,6 @@ public function getModifyColumnDDL(ColumnDiff $columnDiff): string } if (!$toColumn->isAutoIncrement() && $fromColumn->isAutoIncrement()) { - $changedProperties['defaultValueValue'] = [$fromColumn->getDefaultValueString(), null]; - $toColumn->setDefaultValue(null); - //remove sequence if ($fromTable->getDatabase()->hasSequence($seqName)) { $this->createOrDropSequences .= sprintf( @@ -730,6 +728,18 @@ public function getModifyColumnDDL(ColumnDiff $columnDiff): string return $ret; } + /** + * @param string $type + * + * @return bool + */ + public function isUuid(string $type): bool + { + $strings = ['UUID']; + + return in_array(strtoupper($type), $strings, true); + } + /** * @param string $type * @@ -739,7 +749,7 @@ public function isString(string $type): bool { $strings = ['VARCHAR']; - return in_array(strtoupper($type), $strings); + return in_array(strtoupper($type), $strings, true); } /** @@ -751,7 +761,7 @@ public function isNumber(string $type): bool { $numbers = ['INTEGER', 'INT4', 'INT2', 'NUMBER', 'NUMERIC', 'SMALLINT', 'BIGINT', 'DECIMAL', 'REAL', 'DOUBLE PRECISION', 'SERIAL', 'BIGSERIAL']; - return in_array(strtoupper($type), $numbers); + return in_array(strtoupper($type), $numbers, true); } /** @@ -766,10 +776,6 @@ public function getUsingCast(Column $fromColumn, Column $toColumn): string $toSqlType = strtoupper($toColumn->getDomain()->getSqlType()); $name = $fromColumn->getName(); - if ($this->isNumber($fromSqlType) && $this->isString($toSqlType)) { - //cast from int to string - return ' '; - } if ($this->isString($fromSqlType) && $this->isNumber($toSqlType)) { //cast from string to int return " @@ -782,16 +788,18 @@ public function getUsingCast(Column $fromColumn, Column $toColumn): string return " USING decode(CAST($name as text), 'escape')"; } - if ($fromSqlType === 'DATE' && $toSqlType === 'TIME') { - return ' USING NULL'; - } - - if ($this->isNumber($fromSqlType) && $this->isNumber($toSqlType)) { + if ( + ($this->isNumber($fromSqlType) && $this->isNumber($toSqlType)) || + ($this->isString($fromSqlType) && $this->isString($toSqlType)) || + ($this->isNumber($fromSqlType) && $this->isString($toSqlType)) || + ($this->isUuid($fromSqlType) && $this->isString($toSqlType)) + ) { + // no cast necessary return ''; } - if ($this->isString($fromSqlType) && $this->isString($toSqlType)) { - return ''; + if ($this->isString($fromSqlType) && $this->isUuid($toSqlType)) { + return " USING $name::uuid"; } return ' USING NULL'; diff --git a/src/Propel/Generator/Platform/SqlitePlatform.php b/src/Propel/Generator/Platform/SqlitePlatform.php index 78d356a177..4cfb174b60 100644 --- a/src/Propel/Generator/Platform/SqlitePlatform.php +++ b/src/Propel/Generator/Platform/SqlitePlatform.php @@ -59,6 +59,8 @@ protected function initialize(): void $this->foreignKeySupport = version_compare($version, '3.6.19') >= 0; + unset($this->schemaDomainMap[PropelTypes::UUID]); + $this->setSchemaDomainMapping(new Domain(PropelTypes::NUMERIC, 'DECIMAL')); $this->setSchemaDomainMapping(new Domain(PropelTypes::LONGVARCHAR, 'MEDIUMTEXT')); $this->setSchemaDomainMapping(new Domain(PropelTypes::DATE, 'DATETIME')); diff --git a/src/Propel/Generator/Reverse/AbstractSchemaParser.php b/src/Propel/Generator/Reverse/AbstractSchemaParser.php index fd8fa9b55b..7064d2941d 100644 --- a/src/Propel/Generator/Reverse/AbstractSchemaParser.php +++ b/src/Propel/Generator/Reverse/AbstractSchemaParser.php @@ -191,11 +191,7 @@ protected function getMappedPropelType(string $nativeType): ?string $this->nativeToPropelTypeMap = $this->getTypeMapping(); } - if (isset($this->nativeToPropelTypeMap[$nativeType])) { - return $this->nativeToPropelTypeMap[$nativeType]; - } - - return null; + return $this->nativeToPropelTypeMap[$nativeType] ?? null; } /** diff --git a/src/Propel/Generator/Reverse/MssqlSchemaParser.php b/src/Propel/Generator/Reverse/MssqlSchemaParser.php index 3626a743a9..1e0c70cf08 100644 --- a/src/Propel/Generator/Reverse/MssqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/MssqlSchemaParser.php @@ -60,7 +60,7 @@ class MssqlSchemaParser extends AbstractSchemaParser 'timestamp' => PropelTypes::BINARY, 'tinyint identity' => PropelTypes::TINYINT, 'tinyint' => PropelTypes::TINYINT, - 'uniqueidentifier' => PropelTypes::CHAR, + 'uniqueidentifier' => PropelTypes::UUID, 'varbinary' => PropelTypes::VARBINARY, 'varbinary(max)' => PropelTypes::CLOB, 'varchar' => PropelTypes::VARCHAR, diff --git a/src/Propel/Generator/Reverse/OracleSchemaParser.php b/src/Propel/Generator/Reverse/OracleSchemaParser.php index a4479b1b93..34942453e7 100644 --- a/src/Propel/Generator/Reverse/OracleSchemaParser.php +++ b/src/Propel/Generator/Reverse/OracleSchemaParser.php @@ -58,6 +58,7 @@ class OracleSchemaParser extends AbstractSchemaParser 'NUMBER' => PropelTypes::INTEGER, 'NVARCHAR2' => PropelTypes::VARCHAR, 'TIMESTAMP' => PropelTypes::TIMESTAMP, + 'UUID' => PropelTypes::UUID, 'VARCHAR2' => PropelTypes::VARCHAR, ]; diff --git a/src/Propel/Generator/Reverse/PgsqlSchemaParser.php b/src/Propel/Generator/Reverse/PgsqlSchemaParser.php index 96f413b09a..e79c95bfb9 100755 --- a/src/Propel/Generator/Reverse/PgsqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/PgsqlSchemaParser.php @@ -73,6 +73,7 @@ class PgsqlSchemaParser extends AbstractSchemaParser 'timestamp with time zone' => PropelTypes::TIMESTAMP, 'double precision' => PropelTypes::DOUBLE, 'json' => PropelTypes::JSON, + 'uuid' => PropelTypes::UUID, ]; /** diff --git a/src/Propel/Generator/Reverse/SqliteSchemaParser.php b/src/Propel/Generator/Reverse/SqliteSchemaParser.php index aaae63b468..1e8d6ecb50 100644 --- a/src/Propel/Generator/Reverse/SqliteSchemaParser.php +++ b/src/Propel/Generator/Reverse/SqliteSchemaParser.php @@ -68,6 +68,7 @@ class SqliteSchemaParser extends AbstractSchemaParser 'text' => PropelTypes::LONGVARCHAR, 'enum' => PropelTypes::CHAR, 'set' => PropelTypes::CHAR, + 'uuid' => PropelTypes::UUID, ]; /** diff --git a/src/Propel/Runtime/ActiveQuery/Criteria.php b/src/Propel/Runtime/ActiveQuery/Criteria.php index c591b3ba49..7e45927215 100644 --- a/src/Propel/Runtime/ActiveQuery/Criteria.php +++ b/src/Propel/Runtime/ActiveQuery/Criteria.php @@ -2357,7 +2357,7 @@ public function getPrimaryKey(?self $criteria = null): ?ColumnMap * WHERE some_column = some value AND could_have_another_column = * another value AND so on. * - * @param \Propel\Runtime\ActiveQuery\Criteria|array $updateValues A Criteria object containing values used in set clause. + * @param \Propel\Runtime\ActiveQuery\Criteria $updateValues A Criteria object containing values used in set clause. * @param \Propel\Runtime\Connection\ConnectionInterface $con The ConnectionInterface connection object to use. * * @return int The number of rows affected by last update statement. @@ -2366,7 +2366,7 @@ public function getPrimaryKey(?self $criteria = null): ?ColumnMap * Note that the return value does require that this information is returned * (supported) by the Propel db driver. */ - public function doUpdate($updateValues, ConnectionInterface $con): int + public function doUpdate(Criteria $updateValues, ConnectionInterface $con): int { return UpdateQueryExecutor::execute($this, $updateValues, $con); } diff --git a/src/Propel/Runtime/ActiveQuery/SqlBuilder/InsertQuerySqlBuilder.php b/src/Propel/Runtime/ActiveQuery/SqlBuilder/InsertQuerySqlBuilder.php index 64653c7d33..6215a07251 100644 --- a/src/Propel/Runtime/ActiveQuery/SqlBuilder/InsertQuerySqlBuilder.php +++ b/src/Propel/Runtime/ActiveQuery/SqlBuilder/InsertQuerySqlBuilder.php @@ -71,6 +71,8 @@ protected function buildSimpleColumnNamesCsv(array $qualifiedColumnNames): strin } /** + * Build a comma separated list of placeholders, i.e. ":p1,:p2,:p3" for 3 placeholders. + * * @param int $numberOfPlaceholders * * @return string diff --git a/src/Propel/Runtime/ActiveQuery/SqlBuilder/UpdateQuerySqlBuilder.php b/src/Propel/Runtime/ActiveQuery/SqlBuilder/UpdateQuerySqlBuilder.php index d48a2b6f84..0f4cf60013 100644 --- a/src/Propel/Runtime/ActiveQuery/SqlBuilder/UpdateQuerySqlBuilder.php +++ b/src/Propel/Runtime/ActiveQuery/SqlBuilder/UpdateQuerySqlBuilder.php @@ -23,7 +23,7 @@ class UpdateQuerySqlBuilder extends AbstractSqlQueryBuilder /** * @psalm-var array> - * @var array + * @var array> */ protected $updateTablesColumns; diff --git a/src/Propel/Runtime/Map/ColumnMap.php b/src/Propel/Runtime/Map/ColumnMap.php index 5e8f38c8f1..13d13938a0 100644 --- a/src/Propel/Runtime/Map/ColumnMap.php +++ b/src/Propel/Runtime/Map/ColumnMap.php @@ -294,6 +294,16 @@ public function isText(): bool ]); } + /** + * Whether this column contains UUIDs. + * + * @return bool + */ + public function isUuid(): bool + { + return $this->type === PropelTypes::UUID; + } + /** * Set the size of this column. * diff --git a/tests/Propel/Tests/Generator/Model/ColumnTest.php b/tests/Propel/Tests/Generator/Model/ColumnTest.php index cbb2027cc6..533248e63b 100644 --- a/tests/Propel/Tests/Generator/Model/ColumnTest.php +++ b/tests/Propel/Tests/Generator/Model/ColumnTest.php @@ -476,6 +476,7 @@ public function providePdoTypes() ['ENUM', PDO::PARAM_INT], ['BU_DATE', PDO::PARAM_STR], ['BU_TIMESTAMP', PDO::PARAM_STR], + ['UUID', PDO::PARAM_STR], ]; } @@ -709,6 +710,31 @@ public function provideMappingNumericTypes() ]; } + /** + * @return void + */ + public function testUuidType() + { + $domain = $this->getDomainMock(); + $domain + ->expects($this->once()) + ->method('setType') + ->with($this->equalTo(PropelTypes::UUID)); + + $domain + ->expects($this->any()) + ->method('getType') + ->will($this->returnValue(PropelTypes::UUID)); + + $column = new Column(''); + $column->setDomain($domain); + $column->setType(PropelTypes::UUID); + + $this->assertSame('string', $column->getPhpType()); + $this->assertTrue($column->isPhpPrimitiveType()); + $this->assertTrue($column->isUuidType()); + } + /** * @dataProvider provideMappingTextTypes * diff --git a/tests/Propel/Tests/Generator/Platform/MssqlPlatformTest.php b/tests/Propel/Tests/Generator/Platform/MssqlPlatformTest.php index feb86af306..c150b1f045 100644 --- a/tests/Propel/Tests/Generator/Platform/MssqlPlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/MssqlPlatformTest.php @@ -697,4 +697,23 @@ public function testGetCommentBlockDDL() "; $this->assertEquals($expected, $this->getPlatform()->getCommentBlockDDL('foo bar')); } + + /** + * @dataProvider providerForTestCreateSchemaWithUuidColumns + * + * @return void + */ + public function testCreateSchemaWithUuidColumns($schema) + { + $table = $this->getTableFromSchema($schema); + $expected = " +CREATE TABLE [foo] +( + [uuid] UNIQUEIDENTIFIER DEFAULT vendor_specific_default() NOT NULL, + [other_uuid] UNIQUEIDENTIFIER NULL, + CONSTRAINT [foo_pk] PRIMARY KEY ([uuid]) +); +"; + $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); + } } diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php index c9c8a9b179..ba61fdbcb8 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php @@ -974,4 +974,33 @@ public function testTypeMapping(string $propelDataType, string $expectedMysqlDat $actualMysqlDataType = $this->getPlatform()->getDomainForType($propelDataType)->getSqlType(); $this->assertEquals($expectedMysqlDataType, $actualMysqlDataType); } + + public function unavailableTypesDataProvider() + { + return [ + [PropelTypes::UUID], + ]; + } + + /** + * @dataProvider unavailableTypesDataProvider + */ + public function testExceptionOnAccessOfUnavailableType(string $propelDataType) + { + $this->expectException(\Propel\Generator\Exception\EngineException::class); + + $this->getPlatform()->getDomainForType($propelDataType); + } + + /** + * @dataProvider providerForTestCreateSchemaWithUuidColumns + * + * @return void + */ + public function testCreateSchemaWithUuidColumns($schema) + { + $this->expectException(\Propel\Generator\Exception\EngineException::class); + + $table = $this->getTableFromSchema($schema); + } } diff --git a/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php index 177593b53d..0f2645d0c3 100644 --- a/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php @@ -518,4 +518,22 @@ public function testGetModifyDatabaseWithBlockStorageDDL() "; $this->assertEquals($expected, $this->getPlatform()->getModifyDatabaseDDL($databaseDiff)); } + + /** + * @dataProvider providerForTestMigrateToUUIDColumn + * + * @return void + */ + public function testMigrateToUUIDColumn($tableDiff) + { + $expected = <<assertEquals($expected, $this->getPlatform()->getModifyTableColumnsDDL($tableDiff)); + } } diff --git a/tests/Propel/Tests/Generator/Platform/OraclePlatformTest.php b/tests/Propel/Tests/Generator/Platform/OraclePlatformTest.php index b207878474..2fa7840e8c 100644 --- a/tests/Propel/Tests/Generator/Platform/OraclePlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/OraclePlatformTest.php @@ -651,4 +651,24 @@ public function testGetOracleBlockStorageDDL() $this->assertEquals($expected, $this->getPlatform()->getAddTablesDDL($database)); } + + /** + * @dataProvider providerForTestCreateSchemaWithUuidColumns + * + * @return void + */ + public function testCreateSchemaWithUuidColumns($schema) + { + $table = $this->getTableFromSchema($schema); + $expected = " +CREATE TABLE foo +( + uuid UUID DEFAULT vendor_specific_default() NOT NULL, + other_uuid UUID +); + +ALTER TABLE foo ADD CONSTRAINT foo_pk PRIMARY KEY (uuid); +"; + $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); + } } diff --git a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php index c09ce4516e..112bc4910b 100755 --- a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php @@ -12,6 +12,7 @@ use Propel\Generator\Model\Column; use Propel\Generator\Model\ColumnDefaultValue; use Propel\Generator\Model\Diff\ColumnComparator; +use Propel\Generator\Model\Diff\TableDiff; use Propel\Generator\Model\Table; use Propel\Generator\Platform\PgsqlPlatform; @@ -20,9 +21,9 @@ class PgsqlPlatformMigrationTest extends PlatformMigrationTestProvider /** * Get the Platform object for this class * - * @return \Propel\Generator\Platform\PlatformInterface + * @return PgsqlPlatform */ - protected function getPlatform() + protected function getPlatform(): PgsqlPlatform { return new PgsqlPlatform(); } @@ -448,4 +449,21 @@ public function testGetModifyTableForeignKeysSkipSql4DDL($databaseDiff) { $this->assertFalse($databaseDiff); } + + /** + * @dataProvider providerForTestMigrateToUUIDColumn + * + * @return void + */ + public function testMigrateToUUIDColumn($tableDiff) + { + $expected = <<assertEquals($expected, $this->getPlatform()->getModifyTableColumnsDDL($tableDiff)); + } } diff --git a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php index 6d9a6ac269..952ee58669 100755 --- a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformTest.php @@ -834,4 +834,25 @@ public function testGetCommentBlockDDL() "; $this->assertEquals($expected, $this->getPlatform()->getCommentBlockDDL('foo bar')); } + + /** + * @dataProvider providerForTestCreateSchemaWithUuidColumns + * + * @return void + */ + public function testCreateSchemaWithUuidColumns($schema) + { + $table = $this->getTableFromSchema($schema); + $expected = <<< 'EOT' + +CREATE TABLE "foo" +( + "uuid" uuid DEFAULT vendor_specific_default() NOT NULL, + "other_uuid" uuid, + PRIMARY KEY ("uuid") +); + +EOT; + $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); + } } diff --git a/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php b/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php index 338195d04a..4dce38d5cc 100644 --- a/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php +++ b/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php @@ -12,6 +12,8 @@ use Propel\Generator\Model\Diff\ColumnComparator; use Propel\Generator\Model\Diff\DatabaseComparator; use Propel\Generator\Model\Diff\TableComparator; +use Propel\Generator\Model\Diff\TableDiff; +use Propel\Generator\Model\PropelTypes; use Propel\Generator\Model\Table; /** @@ -579,4 +581,43 @@ public function providerForTestGetModifyTableForeignKeysSkipSql4DDL() return [[$diff]]; } + + protected function buildTableDiff(string $tableName, string $tableColumnsFrom, string $tableColumnsTo): TableDiff + { + $schema1 = << + + $tableColumnsFrom +
+ +EOF; + $schema2 = << + + $tableColumnsTo +
+ +EOF; + + $t1 = $this->getDatabaseFromSchema($schema1)->getTable($tableName); + $t2 = $this->getDatabaseFromSchema($schema2)->getTable($tableName); + $tc = new TableComparator(); + $tc->setFromTable($t1); + $tc->setToTable($t2); + $tc->compareColumns(); + + return $tc->getTableDiff(); + } + + public function providerForTestMigrateToUUIDColumn() + { + $tableColumnsFrom = << +EOF; + $tableColumnsTo = << +EOF; + + return [[$this->buildTableDiff('foo', $tableColumnsFrom, $tableColumnsTo)]]; + } } diff --git a/tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php b/tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php index 995ab21f05..fb43f71649 100644 --- a/tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php +++ b/tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php @@ -360,4 +360,18 @@ public function providerForTestGetForeignKeysDDL() [$table1], ]; } + + public function providerForTestCreateSchemaWithUuidColumns() + { + $schema = << + + + +
+ +EOF; + + return [[$schema]]; + } } diff --git a/tests/Propel/Tests/Generator/Platform/SqlitePlatformTest.php b/tests/Propel/Tests/Generator/Platform/SqlitePlatformTest.php index dd85e8d4e1..e0adbf0414 100644 --- a/tests/Propel/Tests/Generator/Platform/SqlitePlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/SqlitePlatformTest.php @@ -12,6 +12,7 @@ use Propel\Generator\Model\ColumnDefaultValue; use Propel\Generator\Model\IdMethod; use Propel\Generator\Model\IdMethodParameter; +use Propel\Generator\Model\PropelTypes; use Propel\Generator\Model\Table; use Propel\Generator\Platform\SqlitePlatform; use Propel\Runtime\Adapter\AdapterFactory; @@ -429,4 +430,33 @@ public function testGetCommentBlockDDL() "; $this->assertEquals($expected, $this->getPlatform()->getCommentBlockDDL('foo bar')); } + + public function unavailableTypesDataProvider() + { + return [ + [PropelTypes::UUID], + ]; + } + + /** + * @dataProvider unavailableTypesDataProvider + */ + public function testExceptionOnAccessOfUnavailableType(string $propelDataType) + { + $this->expectException(\Propel\Generator\Exception\EngineException::class); + + $this->getPlatform()->getDomainForType($propelDataType); + } + + /** + * @dataProvider providerForTestCreateSchemaWithUuidColumns + * + * @return void + */ + public function testCreateSchemaWithUuidColumns($schema) + { + $this->expectException(\Propel\Generator\Exception\EngineException::class); + + $table = $this->getTableFromSchema($schema); + } } diff --git a/tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php b/tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php index e43b08446f..6d71d99e26 100644 --- a/tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php +++ b/tests/Propel/Tests/Generator/Reverse/PgsqlSchemaParserTest.php @@ -12,6 +12,7 @@ use Propel\Generator\Config\QuickGeneratorConfig; use Propel\Generator\Model\ColumnDefaultValue; use Propel\Generator\Model\Database; +use Propel\Generator\Model\PropelTypes; use Propel\Generator\Platform\DefaultPlatform; use Propel\Generator\Reverse\PgsqlSchemaParser; use Propel\Runtime\Propel; @@ -58,11 +59,12 @@ protected function tearDown(): void public function parseDataProvider() { return [ - // columnDDL, expectedColumnPhpName, expectedColumnDefaultType, expectedColumnDefaultValue, expectedSize, expectedScale - ['my_column varchar(20) default null', 'MyColumn', ColumnDefaultValue::TYPE_VALUE, 'NULL', 20, null], - ["my_column varchar(20) default ''", 'MyColumn', ColumnDefaultValue::TYPE_VALUE, '', 20, null], - ['my_column numeric(11,0) default 0', 'MyColumn', ColumnDefaultValue::TYPE_VALUE, 0, 11, 0], - ['my_column numeric(55,8) default 0', 'MyColumn', ColumnDefaultValue::TYPE_VALUE, 0, 55, 8], + // columnDDL, expectedColumnPhpName, type, expectedColumnDefaultType, expectedColumnDefaultValue, expectedSize, expectedScale + ['my_column varchar(20) default null', 'MyColumn', PropelTypes::VARCHAR, ColumnDefaultValue::TYPE_VALUE, 'NULL', 20, null], + ["my_column varchar(20) default ''", 'MyColumn', PropelTypes::VARCHAR, ColumnDefaultValue::TYPE_VALUE, '', 20, null], + ['my_column numeric(11,0) default 0', 'MyColumn', PropelTypes::DECIMAL, ColumnDefaultValue::TYPE_VALUE, 0, 11, 0], + ['my_column numeric(55,8) default 0', 'MyColumn', PropelTypes::DECIMAL, ColumnDefaultValue::TYPE_VALUE, 0, 55, 8], + ['my_column uuid default null', 'MyColumn', PropelTypes::UUID, null, null, null, null], ]; } @@ -71,7 +73,7 @@ public function parseDataProvider() * * @return void */ - public function testParse($columnDDL, $expectedColumnPhpName, $expectedColumnDefaultType, $expectedColumnDefaultValue, $expectedSize, $expectedScale) + public function testParse($columnDDL, $expectedPhpName, $expectedType, $expectedDefaultType, $expectedDefaultValue, $expectedSize, $expectedScale) { $this->con->query("create table foo ( {$columnDDL} );"); $parser = new PgsqlSchemaParser($this->con); @@ -86,13 +88,22 @@ public function testParse($columnDDL, $expectedColumnPhpName, $expectedColumnDef $table = $database->getTable('foo'); $columns = $table->getColumns(); $this->assertEquals(1, count($columns)); + $column = $columns[0]; + $this->assertNotEmpty($column); // check out our rev-eng column info - $defaultValue = $columns[0]->getDefaultValue(); - $this->assertEquals($expectedColumnPhpName, $columns[0]->getPhpName()); - $this->assertEquals($expectedColumnDefaultType, $defaultValue->getType()); - $this->assertEquals($expectedColumnDefaultValue, $defaultValue->getValue()); - $this->assertEquals($expectedSize, $columns[0]->getSize()); - $this->assertEquals($expectedScale, $columns[0]->getScale()); + $this->assertEquals($expectedPhpName, $column->getPhpName()); + $this->assertEquals($expectedType, $column->getType()); + + $defaultValue = $column->getDefaultValue(); + if($expectedDefaultType === null){ + $this->assertNull($expectedDefaultType); + } else { + $this->assertEquals($expectedDefaultType, $defaultValue->getType()); + $this->assertEquals($expectedDefaultValue, $defaultValue->getValue()); + } + + $this->assertEquals($expectedSize, $column->getSize()); + $this->assertEquals($expectedScale, $column->getScale()); } } diff --git a/tests/bin/setup.mysql.sh b/tests/bin/setup.mysql.sh index 1b4087e2f5..97d71507bb 100755 --- a/tests/bin/setup.mysql.sh +++ b/tests/bin/setup.mysql.sh @@ -6,7 +6,7 @@ if [ "$mysql" = "" ]; then fi if [ "$mysql" = "" ]; then - echo "Can not find mysql binary. Is it installed?"; + echo "Cannot find mysql binary. Is it installed?"; exit 1; fi @@ -21,20 +21,21 @@ if [ "$DB_PW" != "" ]; then fi DB_HOSTNAME=${DB_HOSTNAME-127.0.0.1}; +DB_NAME=${DB_NAME-test}; "$mysql" --version; retry_count=0 while true; do - "$mysql" --host="$DB_HOSTNAME" -u"$DB_USER" $pw_option -e ' + "$mysql" --host="$DB_HOSTNAME" -u"$DB_USER" $pw_option -e " SET FOREIGN_KEY_CHECKS = 0; -DROP DATABASE IF EXISTS test; +DROP DATABASE IF EXISTS $DB_NAME; DROP SCHEMA IF EXISTS second_hand_books; DROP SCHEMA IF EXISTS contest; DROP SCHEMA IF EXISTS bookstore_schemas; DROP SCHEMA IF EXISTS migration; SET FOREIGN_KEY_CHECKS = 1; -' +" if [ $? -eq 0 ] || [ $retry_count -ge 6 ]; then break; fi retry_count=$((retry_count + 1)) sleep "$(awk "BEGIN{print 2 ^ $retry_count}")" @@ -42,7 +43,7 @@ done "$mysql" --host="$DB_HOSTNAME" -u"$DB_USER" $pw_option -e " SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); -CREATE DATABASE test; +CREATE DATABASE $DB_NAME; CREATE SCHEMA bookstore_schemas; CREATE SCHEMA contest; CREATE SCHEMA second_hand_books; @@ -52,5 +53,5 @@ CREATE SCHEMA migration; DIR=`dirname $0`; -dsn="mysql:host=$DB_HOSTNAME;dbname=test"; +dsn="mysql:host=$DB_HOSTNAME;dbname=$DB_NAME"; php $DIR/../../bin/propel test:prepare --vendor="mysql" --dsn="$dsn" --user="$DB_USER" --password="$DB_PW";