Skip to content

Commit

Permalink
Merge branch 'master' of github.com:propelorm/Propel2
Browse files Browse the repository at this point in the history
  • Loading branch information
mringler committed Apr 18, 2024
2 parents d2bc7b8 + e32108e commit 4a2032b
Show file tree
Hide file tree
Showing 29 changed files with 483 additions and 65 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ jobs:
matrix:
php-version: [ '7.4', '8.2' ]
db-type: [ sqlite, mysql, pgsql, agnostic ]
symfony-version: [ '4-min', '4-max', '5-min', '5-max', '6-min', '6-max' ]
symfony-version: [ '5-min', '5-max', '6-min', '6-max', '7-min', '7-max']
exclude:
- symfony-version: '4-min'
php-version: '8.2'
- symfony-version: '6-min'
php-version: '7.4'
- symfony-version: '6-max'
php-version: '7.4'
- symfony-version: '7-min'
php-version: '7.4'
- symfony-version: '7-max'
php-version: '7.4'
env:
DB_NAME: 'propel_tests'
DB_USER: 'propel'
Expand Down Expand Up @@ -125,6 +127,8 @@ jobs:
else
vendor/bin/phpunit -c tests/${{ matrix.db-type }}.phpunit.xml
fi
env:
SYMFONY_VERSION: ${{ matrix.symfony-version }}

- name: Code Coverage Report
if: success() && matrix.php-version == '7.4' && matrix.symfony-version == '5-max'
Expand Down Expand Up @@ -166,6 +170,8 @@ jobs:
run: composer install --prefer-dist --no-interaction

- name: PHPStan
env:
PHPSTAN: 1
run: composer stan

- name: Psalm
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"require": {
"php": ">=7.4",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"symfony/yaml": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/config": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/console": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/filesystem": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/finder": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/translation": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/validator": "^4.4.0 || ^5.0.0 || ^6.0.0"
"symfony/yaml": "^5.0.0 || ^6.0.0 || ^7.0.0",
"symfony/config": "^5.0.0 || ^6.0.0 || ^7.0.0",
"symfony/console": "^5.0.0 || ^6.0.0 || ^7.0.0",
"symfony/filesystem": "^5.0.0 || ^6.0.0 || ^7.0.0",
"symfony/finder": "^5.0.0 || ^6.0.0 || ^7.0.0",
"symfony/translation": "^5.0.0 || ^6.0.0 || ^7.0.0",
"symfony/validator": "^5.0.0 || ^6.0.0 || ^7.0.0"
},
"require-dev": {
"ext-pdo": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function preUpdate(AbstractOMBuilder $builder): string
: '\\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision()';

return 'if ($this->isModified() && !$this->isColumnModified(' . $this->getColumnConstant('update_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('update_column') . "(${valueSource});
\$this->" . $this->getColumnSetter('update_column') . "({$valueSource});
}";
}

Expand All @@ -131,7 +131,7 @@ public function preInsert(AbstractOMBuilder $builder): string
: '$highPrecision';
$script .= "
if (!\$this->isColumnModified(" . $this->getColumnConstant('create_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('create_column') . "(${valueSource});
\$this->" . $this->getColumnSetter('create_column') . "({$valueSource});
}";
}

Expand All @@ -141,7 +141,7 @@ public function preInsert(AbstractOMBuilder $builder): string
: '$highPrecision';
$script .= "
if (!\$this->isColumnModified(" . $this->getColumnConstant('update_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('update_column') . "(${valueSource});
\$this->" . $this->getColumnSetter('update_column') . "({$valueSource});
}";
}

Expand Down
53 changes: 31 additions & 22 deletions src/Propel/Generator/Builder/Om/AbstractOMBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,34 +686,43 @@ public function getFKPhpNameAffix(ForeignKey $fk, bool $plural = false): string
* @return string
*/
protected function getCrossFKsPhpNameAffix(CrossForeignKeys $crossFKs, bool $plural = true): string
{
$baseName = $this->buildCombineCrossFKsPhpNameAffix($crossFKs, false);

$existingTable = $this->getDatabase()->getTableByPhpName($baseName);
$isNameCollision = $existingTable && $this->getTable()->isConnectedWithTable($existingTable);

return ($plural || $isNameCollision) ? $this->buildCombineCrossFKsPhpNameAffix($crossFKs, $plural, $isNameCollision) : $baseName;
}

/**
* @param \Propel\Generator\Model\CrossForeignKeys $crossFKs
* @param bool $plural
* @param bool $withPrefix
*
* @return string
*/
protected function buildCombineCrossFKsPhpNameAffix(CrossForeignKeys $crossFKs, bool $plural = true, bool $withPrefix = false): string
{
$names = [];
if ($withPrefix) {
$names[] = 'Cross';
}
$fks = $crossFKs->getCrossForeignKeys();
$lastCrossFk = array_pop($fks);
$unclassifiedPrimaryKeys = $crossFKs->getUnclassifiedPrimaryKeys();
$lastIsPlural = $plural && !$unclassifiedPrimaryKeys;

if ($plural) {
if ($crossFKs->getUnclassifiedPrimaryKeys()) {
//we have a non fk as pk as well, so we need to make pluralisation on our own and can't
//rely on getFKPhpNameAffix's pluralisation
foreach ($crossFKs->getCrossForeignKeys() as $fk) {
$names[] = $this->getFKPhpNameAffix($fk, false);
}
} else {
//we have only fks, so give us names with plural and return those
$lastIdx = count($crossFKs->getCrossForeignKeys()) - 1;
foreach ($crossFKs->getCrossForeignKeys() as $idx => $fk) {
$needPlural = $idx === $lastIdx; //only last fk should be plural
$names[] = $this->getFKPhpNameAffix($fk, $needPlural);
}
foreach ($fks as $fk) {
$names[] = $this->getFKPhpNameAffix($fk, false);
}
$names[] = $this->getFKPhpNameAffix($lastCrossFk, $lastIsPlural);

return implode('', $names);
}
} else {
// no plural, so $plural=false
foreach ($crossFKs->getCrossForeignKeys() as $fk) {
$names[] = $this->getFKPhpNameAffix($fk, false);
}
if (!$unclassifiedPrimaryKeys) {
return implode('', $names);
}

foreach ($crossFKs->getUnclassifiedPrimaryKeys() as $pk) {
foreach ($unclassifiedPrimaryKeys as $pk) {
$names[] = $pk->getPhpName();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Command/MigrationUpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function configure()
*
* @throws \Propel\Runtime\Exception\RuntimeException
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$configOptions = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Model/CrossForeignKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function getIncomingForeignKey(): ?ForeignKey
*/
public function isAtLeastOneLocalPrimaryKeyNotCovered(ForeignKey $fk): bool
{
$primaryKeys = $fk->getLocalPrimaryKeys();
$primaryKeys = $fk->getLocalColumnObjects();
foreach ($primaryKeys as $primaryKey) {
$covered = false;
foreach ($this->getCrossForeignKeys() as $crossFK) {
Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Model/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ public function isAtLeastOneLocalColumnRequired(): bool
*/
public function isAtLeastOneLocalPrimaryKeyIsRequired(): bool
{
foreach ($this->getLocalPrimaryKeys() as $pk) {
foreach ($this->getLocalColumnObjects() as $pk) {
if ($pk->isNotNull() && !$pk->hasDefaultValue()) {
return true;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Propel/Generator/Model/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2288,4 +2288,18 @@ public function getAdditionalModelClassImports(): ?array

return null;
}

/**
* Check if there is a FK rellation between the current table and the given
* table in either direction.
*
* @param \Propel\Generator\Model\Table $table
*
* @return bool
*/
public function isConnectedWithTable(Table $table): bool
{
return $this->getForeignKeysReferencingTable($table->getName()) ||
$table->getForeignKeysReferencingTable($this->getName());
}
}
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 @@ -515,6 +516,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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Propel\Generator\Platform\Util;

use Propel\Generator\Model\Table;
use Propel\Generator\Util\SqlParser;

/**
* Merges several ALTER TABLE statements when creating migrations.
Expand Down Expand Up @@ -66,7 +67,10 @@ public function __construct(Table $table)
*/
public function mergeStatements(string $sql): string
{
$statements = explode(';', $sql);
$sqlParser = new SqlParser();
$sqlParser->setSQL($sql);
$statements = $sqlParser->explodeIntoStatements();

$blocks = [];
$currentBlock = [];

Expand Down
3 changes: 3 additions & 0 deletions src/Propel/Generator/Reverse/MysqlSchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ public function getColumnFromRow(array $row, Table $table): Column
}
if (in_array($default, ['CURRENT_TIMESTAMP', 'current_timestamp()'], true)) {
$default = 'CURRENT_TIMESTAMP';
if (strpos(strtolower($row['Extra']), 'on update current_timestamp') !== false) {
$default = 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
}
$type = ColumnDefaultValue::TYPE_EXPR;
} else {
$type = ColumnDefaultValue::TYPE_VALUE;
Expand Down
26 changes: 25 additions & 1 deletion src/Propel/Generator/Util/SqlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,37 @@ public function explodeIntoStatements(): array
public function getNextStatement(): string
{
$isAfterBackslash = false;
$isCommentLine = false;
$isInString = false;
$stringQuotes = '';
$parsedString = '';
$lowercaseString = ''; // helper variable for performance sake
while ($this->pos <= $this->len) {
$char = $this->sql[$this->pos] ?? '';

$newLine = !isset($this->sql[$this->pos - 1]) || $this->sql[$this->pos - 1] === PHP_EOL;

// Skip comments
if ($isCommentLine === true) {
$this->pos++;
if ($char === PHP_EOL) {
$isCommentLine = false; // end of comments line
}

continue;
}
// check flags for strings or escaper
switch ($char) {
case '#':
// detect comment line
if ($newLine === true && $isCommentLine === false) {
$this->pos++;
$isCommentLine = true;

continue 2;
}

break;
case '\\':
$isAfterBackslash = true;

Expand Down Expand Up @@ -295,8 +318,9 @@ public function getNextStatement(): string
// check for end of statement
if ($char . $nextChars == $this->delimiter) {
$this->pos += $i; // increase position
$parsedTrimmedString = trim($parsedString);

return trim($parsedString);
return $parsedTrimmedString ?: $parsedString; // to check empty line to avoid stop parsing
}
// avoid using strtolower on the whole parsed string every time new character is added
// there is also no point in adding characters which are in the string
Expand Down
18 changes: 12 additions & 6 deletions src/Propel/Runtime/Collection/CollectionIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ public function append($value): void
/**
* @param int $flags Not used
*
* @return bool
* @return true
*/
#[\ReturnTypeWillChange]
public function asort(int $flags = SORT_REGULAR): bool
{
parent::asort();
Expand All @@ -261,8 +262,9 @@ public function asort(int $flags = SORT_REGULAR): bool
/**
* @param int $flags Not used
*
* @return bool
* @return true
*/
#[\ReturnTypeWillChange]
public function ksort(int $flags = SORT_REGULAR): bool
{
parent::ksort();
Expand All @@ -274,8 +276,9 @@ public function ksort(int $flags = SORT_REGULAR): bool
/**
* @param callable $callback
*
* @return bool
* @return true
*/
#[\ReturnTypeWillChange]
public function uasort($callback): bool
{
parent::uasort($callback);
Expand All @@ -287,8 +290,9 @@ public function uasort($callback): bool
/**
* @param callable $callback
*
* @return bool
* @return true
*/
#[\ReturnTypeWillChange]
public function uksort($callback): bool
{
parent::uksort($callback);
Expand All @@ -298,8 +302,9 @@ public function uksort($callback): bool
}

/**
* @return bool
* @return true
*/
#[\ReturnTypeWillChange]
public function natsort(): bool
{
parent::natsort();
Expand All @@ -309,8 +314,9 @@ public function natsort(): bool
}

/**
* @return bool
* @return true
*/
#[\ReturnTypeWillChange]
public function natcasesort(): bool
{
parent::natcasesort();
Expand Down
5 changes: 0 additions & 5 deletions src/Propel/Runtime/Validator/Constraints/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

class Date extends SymfonyDateConstraint
{
/**
* @var string
*/
public $message = 'This value is not a valid date.';

/**
* @var string
*/
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
Loading

0 comments on commit 4a2032b

Please sign in to comment.