Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichsg committed Sep 22, 2020
2 parents 7115eea + 42c2731 commit c455e24
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 18 deletions.
17 changes: 12 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
language: php
sudo: false

branches:
only:
- master
- develop

php:
- 5.4
- 5.5
- 5.6
- hhvm
- 7.1
- 7.2
- 7.3

services:
- mysql

before_script:
- composer install
- mysql -e 'create database behavior_test;'

script:
- phpunit
- vendor/bin/phpunit

env:
- DB_USER=travis DB_PASS= DB_NAME=behavior_test
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ $userDocumentId = $document->getUserDocumentId();
// $userDocumentId contains 1
```

## Table Name Aliases

It is possible to use an alias instead of the actual table name for the auto-generated column and trigger names
by specifying the `localTableAlias` parameter. For example, this definition:

```xml
<table name="document">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
<behavior name="\Finanzcheck\CompositeNumberRange\CompositeNumberRangeBehavior">
<parameter name="foreignTable" value="user"/>
<parameter name="localTableAlias" value="doc"/>
</behavior>
</table>
```

will result in the composite id column being named `user_doc_id` and the trigger `SetDocUserId`.

## Migrations Notice
Because `propel:migrations:diff` does not know anything about triggers, they have to be checked via SQL Statement. If you use triggers and change something on
any table having triggers appended, they will always appear in your `down()` section of the migration. At the moment you have to remove them by hand,
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"Finanzcheck\\CompositeNumberRange\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Finanzcheck\\CompositeNumberRange\\": "tests/"
}
},
"authors": [
{
"name": "Alexander Pinnecke",
Expand Down
23 changes: 23 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type: library
owners:
- [email protected]
product: propel-plugins
id: finanzcheck-composite-number-range-behavior
vertical: finanzcheck
availability: false
confidentiality: company-internal
integrity: false
authenticity: false
exposure:
- non-applicable
lifecycle: maintenance
name: Composite Number Range Behavior
description: Propel behavior for client scoped IDs
links:
- type: repository
title: GitHub repository
address: https://github.com/finanzcheck/composite-number-range-behavior
tags:
- php
- propel
license: Proprietary
27 changes: 26 additions & 1 deletion src/CompositeNumberRangeBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CompositeNumberRangeBehavior extends Behavior

protected $parameters = array(
'foreignTable' => null,
'localTableAlias' => null,
'refPhpName' => null,
'phpName' => null,
);
Expand All @@ -33,6 +34,21 @@ public function getForeignTable()
return strtolower($table);
}

/**
* Returns the name that should be used (in column names) for the table that the behavior is applied to.
* The default is the actual table name, but it can be overridden with the localTableAlias parameter.
*
* @return string
*/
public function getLocalTableName()
{
$alias = $this->getParameter('localTableAlias');
if ($alias === null) {
return $this->getTable()->getName();
}
return $alias;
}

/**
* Gets the refPhpName parameter from config array.
*
Expand Down Expand Up @@ -61,6 +77,14 @@ protected function getPhpName()
return $name;
}

public function getCompositeKeyColumnName()
{
$tableName = $this->getLocalTableName();
$foreignTableName = $this->getForeignTable();

return $foreignTableName . '_' . $tableName . '_id';
}

/**
* Adds all columns, indexes, constraints and additional tables.
*/
Expand All @@ -75,7 +99,8 @@ public function modifyTable()
$table->setReloadOnInsert(true);

$foreignIdColumnName = $foreignTableName . '_id';
$this->compositeKeyColumnName = $foreignTableName . '_' . $tableName . '_id';

$this->compositeKeyColumnName = $this->getCompositeKeyColumnName();

if ($table->hasBehavior('concrete_inheritance')) {
// we're a child in a concrete inheritance
Expand Down
21 changes: 11 additions & 10 deletions src/Platform/MysqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,13 @@ protected function createDropTriggersDDL(Table $table)
*/
protected function getTriggerNames(Table $table)
{
$tableName = $table->getName();

/** @var CompositeNumberRangeBehavior $behavior */
$behavior = $table->getBehavior(self::BEHAVIOR_NAME);
$foreignTableName = $behavior->getForeignTable();
$compositeKeyColumnName = $behavior->getCompositeKeyColumnName();
$triggerColumnName = ucwords($compositeKeyColumnName, '_');

$insertTrigger = str_replace(' ', '', ucwords(str_replace('_', ' ', 'set' . ucfirst($foreignTableName) . ucfirst($tableName) . 'Id')));
$updateTrigger = str_replace(' ', '', ucwords(str_replace('_', ' ', 'setOnUpdate' . ucfirst($foreignTableName) . ucfirst($tableName) . 'Id')));
$insertTrigger = str_replace(' ', '', ucwords(str_replace('_', ' ', 'set' . $triggerColumnName)));
$updateTrigger = str_replace(' ', '', ucwords(str_replace('_', ' ', 'setOnUpdate' . $triggerColumnName)));

return [ $insertTrigger, $updateTrigger ];
}
Expand All @@ -177,6 +176,8 @@ protected function createTriggersDDL(Table $table)
$foreignTableName = $behavior->getForeignTable();
$triggerNames = $this->getTriggerNames($table);
$tableName = $table->getName();
$tableNameOrAlias = $behavior->getLocalTableName();
$compositeKeyColumnName = $behavior->getCompositeKeyColumnName();

$sql = "
DELIMITER $;
Expand All @@ -188,11 +189,11 @@ protected function createTriggersDDL(Table $table)
INSERT INTO ${foreignTableName}_sequence (
table_name, ${foreignTableName}_id, ${foreignTableName}_max_sequence_id
) VALUES (
'${tableName}', NEW.${foreignTableName}_id, LAST_INSERT_ID(1)
'${tableNameOrAlias}', NEW.${foreignTableName}_id, LAST_INSERT_ID(1)
) ON DUPLICATE KEY
UPDATE ${foreignTableName}_max_sequence_id = LAST_INSERT_ID(${foreignTableName}_max_sequence_id +1);
SET NEW.${foreignTableName}_${tableName}_id = LAST_INSERT_ID();
SET NEW.${compositeKeyColumnName} = LAST_INSERT_ID();
END
DELIMITER ;
Expand All @@ -203,15 +204,15 @@ protected function createTriggersDDL(Table $table)
BEFORE UPDATE ON ${tableName}
FOR EACH ROW
BEGIN
IF NEW.${foreignTableName}_${tableName}_id IS NULL OR NEW.${foreignTableName}_${tableName}_id = 0 THEN
IF NEW.${compositeKeyColumnName} IS NULL OR NEW.${compositeKeyColumnName} = 0 THEN
INSERT INTO ${foreignTableName}_sequence (
table_name, ${foreignTableName}_id, ${foreignTableName}_max_sequence_id
) VALUES (
'${tableName}', NEW.${foreignTableName}_id, LAST_INSERT_ID(1)
'${tableNameOrAlias}', NEW.${foreignTableName}_id, LAST_INSERT_ID(1)
) ON DUPLICATE KEY
UPDATE ${foreignTableName}_max_sequence_id = LAST_INSERT_ID(${foreignTableName}_max_sequence_id +1);
SET NEW.${foreignTableName}_${tableName}_id = LAST_INSERT_ID();
SET NEW.${compositeKeyColumnName} = LAST_INSERT_ID();
END IF;
END
Expand Down
70 changes: 68 additions & 2 deletions tests/CompositeNumberRangeBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace Propel\Tests\Generator\Behavior\CompositeNumberRange;
namespace Finanzcheck\CompositeNumberRange;

use Finanzcheck\CompositeNumberRange\Platform\MysqlPlatform;
use Propel\Generator\Util\QuickBuilder;
use Propel\Runtime\Adapter\Pdo\MysqlAdapter;
use Propel\Runtime\Collection\ObjectCollection;
use Propel\Runtime\Propel;
use Propel\Tests\TestCase;

class CompositeNumberRangeBehaviorTest extends TestCase
Expand Down Expand Up @@ -35,7 +36,12 @@ public function setUp()
$builder = new QuickBuilder();
$builder->setPlatform(new MysqlPlatform());
$builder->setSchema($schema);
$builder->build('mysql:host=127.0.0.1;dbname=' . getenv('DB_NAME'), getenv('DB_USER'), getenv('DB_PASS'), new MysqlAdapter());
$builder->build(
'mysql:host=127.0.0.1;dbname=' . getenv('DB_NAME'),
getenv('DB_USER'),
getenv('DB_PASS'),
new MysqlAdapter()
);
}

$this->parent = new \ParentTable();
Expand Down Expand Up @@ -164,6 +170,10 @@ public function testInsertThirdRowWithDifferentParentTableIdCreatesAnotherParent
*/
public function testNullingParentTableIdForExistingRowCreatesNewParentTableId()
{
$tableMapClass = \ParentTable::TABLE_MAP;
$con = Propel::getServiceContainer()->getWriteConnection($tableMapClass::DATABASE_NAME);
$con->exec('ALTER TABLE child_table CHANGE parent_table_id parent_table_id INT(11) DEFAULT NULL');

$parentId = $this->parent->getId();

$child = new \ChildTable();
Expand All @@ -176,8 +186,64 @@ public function testNullingParentTableIdForExistingRowCreatesNewParentTableId()

$child->setParentTableId(null);
$child->save();
$child->reload();

$this->assertGreaterThan($firstParentTableId, $child->getParentTableId());
}

public function testUsesTheLocalTableAliasParameterWhenGiven()
{
$schema = <<<EOF
<database name="composite_number_range_test">
<table name="foo">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
</table>
<table name="bar">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
<behavior name="\Finanzcheck\CompositeNumberRange\CompositeNumberRangeBehavior">
<parameter name="foreignTable" value="foo"/>
<parameter name="localTableAlias" value="baz"/>
</behavior>
</table>
</database>
EOF;

$builder = new QuickBuilder();
$builder->setPlatform(new MysqlPlatform());
$builder->setSchema($schema);
$builder->build(
'mysql:host=127.0.0.1;dbname=' . getenv('DB_NAME'),
getenv('DB_USER'),
getenv('DB_PASS'),
new MysqlAdapter()
);

$this->assertTrue(class_exists('\Bar'));
$this->assertTrue(class_exists('\BarQuery'));
$this->assertTrue(class_exists('\Foo'));
$this->assertTrue(class_exists('\FooQuery'));
$this->assertTrue(class_exists('\FooSequence'));
$this->assertTrue(class_exists('\FooSequenceQuery'));

$entity = new \Bar();

$this->assertTrue(method_exists($entity, 'setFoo'));
$this->assertTrue(method_exists($entity, 'setFooId'));
$this->assertTrue(method_exists($entity, 'setFooBazId'));

$sequenceEntity = new \FooSequence();

$this->assertTrue(method_exists($sequenceEntity, 'setTableName'));
$this->assertTrue(method_exists($sequenceEntity, 'setFooId'));
$this->assertTrue(method_exists($sequenceEntity, 'setFooMaxSequenceId'));

$foo = new \Foo();
$foo->save();

$entity->setFoo($foo);
$entity->save();

$this->assertEquals($foo->getId(), $entity->getFooBazId());
}
}

0 comments on commit c455e24

Please sign in to comment.