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

3.next - Fix unique constraints getting lost. #967

Merged
merged 1 commit into from
Dec 28, 2023
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
49 changes: 33 additions & 16 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,47 +984,64 @@
return [];
}
$schema = $model->getSchema();
$fields = $schema->columns();
if (empty($fields)) {
$schemaFields = $schema->columns();
if (empty($schemaFields)) {
return [];
}

$uniqueColumns = ['username', 'login'];
if (in_array($model->getAlias(), ['Users', 'Accounts'])) {
$uniqueColumns[] = 'email';
}
$uniqueRules = [];
$uniqueConstraintsColumns = [];

$rules = [];
foreach ($fields as $fieldName) {
if (in_array($fieldName, $uniqueColumns, true)) {
$rules[$fieldName] = ['name' => 'isUnique', 'fields' => [$fieldName], 'options' => []];
}
}
foreach ($schema->constraints() as $name) {
$constraint = $schema->getConstraint($name);
if ($constraint['type'] !== TableSchema::CONSTRAINT_UNIQUE) {
continue;
}

$options = [];
$fields = $constraint['columns'];
foreach ($fields as $field) {
/** @var array<string> $constraintFields */
$constraintFields = $constraint['columns'];
$uniqueConstraintsColumns = [...$uniqueConstraintsColumns, ...$constraintFields];

foreach ($constraintFields as $field) {
if ($schema->isNullable($field)) {
$allowMultiple = !ConnectionManager::get($this->connection)->getDriver() instanceof Sqlserver;
$options['allowMultipleNulls'] = $allowMultiple;
break;
}
}

$rules[$constraint['columns'][0]] = ['name' => 'isUnique', 'fields' => $fields, 'options' => $options];
$uniqueRules[] = ['name' => 'isUnique', 'fields' => $constraintFields, 'options' => $options];
}

$possiblyUniqueColumns = ['username', 'login'];
if (in_array($model->getAlias(), ['Users', 'Accounts'])) {
$possiblyUniqueColumns[] = 'email';
}

$possiblyUniqueRules = [];
foreach ($schemaFields as $field) {
if (
!in_array($field, $uniqueConstraintsColumns, true) &&
in_array($field, $possiblyUniqueColumns, true)
) {
$possiblyUniqueRules[] = ['name' => 'isUnique', 'fields' => [$field], 'options' => []];
}
}

$rules = [...$possiblyUniqueRules, ...$uniqueRules];

if (empty($associations['belongsTo'])) {
return $rules;
}

foreach ($associations['belongsTo'] as $assoc) {
$rules[$assoc['foreignKey']] = ['name' => 'existsIn', 'extra' => $assoc['alias'], 'options' => []];
$rules[] = [
'name' => 'existsIn',
'fields' => (array)$assoc['foreignKey'],
'extra' => $assoc['alias'],
'options' => [],
];
}

return $rules;
Expand Down Expand Up @@ -1355,7 +1372,7 @@
$fixture = new FixtureCommand();
$fixtureArgs = new Arguments(
[$className],
['table' => $useTable] + $args->getOptions(),

Check failure on line 1375 in src/Command/ModelCommand.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

InvalidArgument

src/Command/ModelCommand.php:1375:13: InvalidArgument: Argument 2 of Cake\Console\Arguments::__construct expects array<string, bool|null|string>, but non-empty-array<string, bool|list<string>|null|string> provided (see https://psalm.dev/004)
['name']
);
$fixture->execute($fixtureArgs, $io);
Expand All @@ -1377,7 +1394,7 @@
$test = new TestCommand();
$testArgs = new Arguments(
['table', $className],
$args->getOptions(),

Check failure on line 1397 in src/Command/ModelCommand.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

InvalidArgument

src/Command/ModelCommand.php:1397:13: InvalidArgument: Argument 2 of Cake\Console\Arguments::__construct expects array<string, bool|null|string>, but array<string, bool|list<string>|null|string> provided (see https://psalm.dev/004)
['type', 'name']
);
$test->execute($testArgs, $io);
Expand Down
6 changes: 3 additions & 3 deletions templates/bake/Model/table.twig
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ class {{ name }}Table extends Table{{ fileBuilder.classBuilder.implements ? ' im
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
{% for field, rule in rulesChecker %}
{% set fields = rule.fields is defined ? Bake.exportArray(rule.fields) : Bake.exportVar(field) %}
{% for rule in rulesChecker %}
{% set fields = Bake.exportArray(rule.fields) %}
{% set options = '' %}
{% for optionName, optionValue in rule.options %}
{%~ set options = (loop.first ? '[' : options) ~ "'#{optionName}' => " ~ Bake.exportVar(optionValue) ~ (loop.last ? ']' : ', ') %}
{% endfor %}
$rules->add($rules->{{ rule.name }}({{ fields|raw }}{{ (rule.extra|default ? ", '#{rule.extra}'" : '')|raw }}{{ (options ? ', ' ~ options : '')|raw }}), ['errorField' => '{{ field }}']);
$rules->add($rules->{{ rule.name }}({{ fields|raw }}{{ (rule.extra|default ? ", '#{rule.extra}'" : '')|raw }}{{ (options ? ', ' ~ options : '')|raw }}), ['errorField' => '{{ rule.fields[0] }}']);
{% endfor %}

return $rules;
Expand Down
140 changes: 131 additions & 9 deletions tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ public function testGetRules()
],
[
'alias' => 'Sites',
'foreignKey' => 'site_id',
'foreignKey' => ['site_id1', 'site_id2'],
],
],
'hasMany' => [
Expand All @@ -1382,18 +1382,20 @@ public function testGetRules()
$args = new Arguments([], [], []);
$result = $command->getRules($model, $associations, $args);
$expected = [
'username' => [
[
'name' => 'isUnique',
'fields' => ['username'],
'options' => [],
],
'country_id' => [
[
'name' => 'existsIn',
'fields' => ['country_id'],
'extra' => 'Countries',
'options' => [],
],
'site_id' => [
[
'name' => 'existsIn',
'fields' => ['site_id1', 'site_id2'],
'extra' => 'Sites',
'options' => [],
],
Expand All @@ -1404,9 +1406,6 @@ public function testGetRules()
/**
* Tests the getRules with unique keys.
*
* Multi-column constraints are ignored as they would
* require a break in compatibility.
*
* @return void
*/
public function testGetRulesUniqueKeys()
Expand All @@ -1416,7 +1415,7 @@ public function testGetRulesUniqueKeys()
'type' => 'unique',
'columns' => ['title'],
]);
$model->getSchema()->addConstraint('ignored_constraint', [
$model->getSchema()->addConstraint('unique_composite', [
'type' => 'unique',
'columns' => ['title', 'user_id'],
]);
Expand All @@ -1425,7 +1424,12 @@ public function testGetRulesUniqueKeys()
$args = new Arguments([], [], []);
$result = $command->getRules($model, [], $args);
$expected = [
'title' => [
[
'name' => 'isUnique',
'fields' => ['title'],
'options' => [],
],
[
'name' => 'isUnique',
'fields' => ['title', 'user_id'],
'options' => [],
Expand All @@ -1434,6 +1438,124 @@ public function testGetRulesUniqueKeys()
$this->assertEquals($expected, $result);
}

/**
* Tests that there are no conflicts between neither multiple constraints,
* nor with foreign keys that share one or more identical column.
*/
public function testGetRulesNoColumnNameConflictForUniqueConstraints(): void
{
$model = $this->getTableLocator()->get('Users');
$model->setSchema([
'department_id' => ['type' => 'integer', 'null' => false],
'username' => ['type' => 'string', 'null' => false],
'email' => ['type' => 'string', 'null' => false],
]);

$model->getSchema()->addConstraint('unique_composite_1', [
'type' => 'unique',
'columns' => ['department_id', 'username'],
]);
$model->getSchema()->addConstraint('unique_composite_2', [
'type' => 'unique',
'columns' => ['department_id', 'email'],
]);

$command = new ModelCommand();
$args = new Arguments([], [], []);
$associations = [
'belongsTo' => [
['alias' => 'Departments', 'foreignKey' => 'department_id'],
],
];

$result = $command->getRules($model, $associations, $args);
$expected = [
[
'name' => 'isUnique',
'fields' => ['department_id', 'username'],
'options' => [],
],
[
'name' => 'isUnique',
'fields' => ['department_id', 'email'],
'options' => [],
],
[
'name' => 'existsIn',
'fields' => ['department_id'],
'extra' => 'Departments',
'options' => [],
],
];
$this->assertEquals($expected, $result);
}

/**
* Tests generating unique rules for possibly unique columns based on
* column names instead of on actual unique constraints.
*/
public function testGetRulesForPossiblyUniqueColumns(): void
{
$model = $this->getTableLocator()->get('Users');
$model->setSchema([
'department_id' => ['type' => 'integer', 'null' => false],
'username' => ['type' => 'string', 'null' => false],
'login' => ['type' => 'string', 'null' => false],
'email' => ['type' => 'string', 'null' => false],
]);

$command = new ModelCommand();
$args = new Arguments([], [], []);
$result = $command->getRules($model, [], $args);
$expected = [
[
'name' => 'isUnique',
'fields' => ['username'],
'options' => [],
],
[
'name' => 'isUnique',
'fields' => ['login'],
'options' => [],
],
[
'name' => 'isUnique',
'fields' => ['email'],
'options' => [],
],
];
$this->assertEquals($expected, $result);

// possibly unique columns should not cause additional rules
// to be generated in case the column is already present in
// an actual unique constraint

$model->getSchema()->addConstraint('unique_composite', [
'type' => 'unique',
'columns' => ['department_id', 'username'],
]);

$result = $command->getRules($model, [], $args);
$expected = [
[
'name' => 'isUnique',
'fields' => ['login'],
'options' => [],
],
[
'name' => 'isUnique',
'fields' => ['email'],
'options' => [],
],
[
'name' => 'isUnique',
'fields' => ['department_id', 'username'],
'options' => [],
],
];
$this->assertEquals($expected, $result);
}

/**
* Test that specific behaviors are auto-detected
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function initialize(array $config): void
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn('category_id', 'Categories'), ['errorField' => 'category_id']);
$rules->add($rules->existsIn('product_id', 'Products'), ['errorField' => 'product_id']);
$rules->add($rules->existsIn(['category_id'], 'Categories'), ['errorField' => 'category_id']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);

return $rules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function validationDefault(Validator $validator): Validator
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn('product_id', 'Products'), ['errorField' => 'product_id']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);

return $rules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function validationDefault(Validator $validator): Validator
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn('product_id', 'Products'), ['errorField' => 'product_id']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);

return $rules;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/comparisons/Model/testBakeTableConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function validationDefault(Validator $validator): Validator
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn('user_id', 'Users'), ['errorField' => 'user_id']);
$rules->add($rules->existsIn(['user_id'], 'Users'), ['errorField' => 'user_id']);

return $rules;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/comparisons/Model/testBakeTableWithCounterCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function initialize(array $config): void
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn('todo_item_id', 'TodoItems'), ['errorField' => 'todo_item_id']);
$rules->add($rules->existsIn(['todo_item_id'], 'TodoItems'), ['errorField' => 'todo_item_id']);

return $rules;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/comparisons/Model/testBakeUpdateTableNoFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function validationDefault(Validator $validator): Validator
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn('user_id', 'Users'), ['errorField' => 'user_id']);
$rules->add($rules->existsIn(['user_id'], 'Users'), ['errorField' => 'user_id']);

return $rules;
}
Expand Down
Loading