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

UUID adjustments for mariadb #1924

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Propel/Common/Config/PropelConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
65 changes: 29 additions & 36 deletions src/Propel/Generator/Model/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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');
Expand All @@ -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') ?: '';
Expand Down Expand Up @@ -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;
}

/**
Expand Down
44 changes: 22 additions & 22 deletions src/Propel/Generator/Model/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/Propel/Generator/Platform/DefaultPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
56 changes: 43 additions & 13 deletions src/Propel/Generator/Platform/MysqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ class MysqlPlatform extends DefaultPlatform
*/
protected $serverVersion;

/**
* @var bool
*/
protected $useUuidNativeType = false;

/**
* Initializes db specific domain mapping.
*
* @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'));
Expand All @@ -68,8 +73,7 @@ protected function initialize(): 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();
}

/**
Expand All @@ -80,17 +84,49 @@ protected function initialize(): 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';

Choose a reason for hiding this comment

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

[Minor] Wouldn't be better to have a standalone platform for MariaDb that extends a MySQL one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that would be great, but that is its own PR

$this->setUuidNativeType($enable);
}
}

/**
* @param bool $enable
*
* @return void
*/
public function setUuidNativeType(bool $enable): void
{
$this->useUuidNativeType = $enable;
$this->setUuidTypeMapping();
}

/**
* Setter for the tableEngineKeyword property
* 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;
}

/**
* @param string $tableEngineKeyword
*
* @return void
Expand All @@ -101,8 +137,6 @@ public function setTableEngineKeyword(string $tableEngineKeyword): void
}

/**
* Getter for the tableEngineKeyword property
*
* @return string
*/
public function getTableEngineKeyword(): string
Expand All @@ -111,8 +145,6 @@ public function getTableEngineKeyword(): string
}

/**
* Setter for the defaultTableEngine property
*
* @param string $defaultTableEngine
*
* @return void
Expand All @@ -123,8 +155,6 @@ public function setDefaultTableEngine(string $defaultTableEngine): void
}

/**
* Getter for the defaultTableEngine property
*
* @return string
*/
public function getDefaultTableEngine(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Propel/Generator/Platform/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/Propel/Generator/Platform/PgsqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
12 changes: 10 additions & 2 deletions src/Propel/Generator/Platform/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class SqlitePlatform extends DefaultPlatform
protected $tableAlteringWorkaround = true;

/**
* Initializes db specific domain mapping.
*
* @return void
*/
protected function initialize(): void
Expand All @@ -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'));
Expand Down
1 change: 1 addition & 0 deletions src/Propel/Generator/Reverse/MysqlSchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class MysqlSchemaParser extends AbstractSchemaParser
'enum' => PropelTypes::CHAR,
'set' => PropelTypes::CHAR,
'binary' => PropelTypes::BINARY,
'uuid' => PropelTypes::UUID, // for MariaDB
];

/**
Expand Down
Loading