Merge 332a78fdc0f6d04308341ed1dcebb03e98ab5222 into a348792bf28207ce29acfb88f6dd0e48bc01a447

This commit is contained in:
Michal Sniatala 2025-02-19 20:48:22 +00:00 committed by GitHub
commit 996684b123
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 64 additions and 6 deletions

View File

@ -469,9 +469,8 @@ class Builder extends BaseBuilder
return ($index->type === 'UNIQUE' || $index->type === 'PRIMARY') && $hasAllFields;
});
foreach (array_map(static fn ($index) => $index->fields, $allIndexes) as $index) {
$constraints[] = current($index);
// only one index can be used?
foreach ($allIndexes as $index) {
$constraints = $index->fields;
break;
}

View File

@ -151,8 +151,8 @@ class Builder extends BaseBuilder
return ($index->type === 'PRIMARY' || $index->type === 'UNIQUE') && $hasAllFields;
});
foreach (array_map(static fn ($index) => $index->fields, $allIndexes) as $index) {
$constraints[] = current($index);
foreach ($allIndexes as $index) {
$constraints = $index->fields;
break;
}

View File

@ -47,6 +47,15 @@ class Migration_Create_test_tables extends Migration
'value' => ['type' => 'VARCHAR', 'constraint' => 400, 'null' => true],
])->addKey('id', true)->createTable('misc', true);
// Team members Table (composite key)
$this->forge->addField([
'team_id' => ['type' => 'INTEGER', 'constraint' => 3],
'person_id' => ['type' => 'INTEGER', 'constraint' => 3],
'role' => ['type' => 'VARCHAR', 'constraint' => 40],
'status' => ['type' => 'VARCHAR', 'constraint' => 40],
'created_at' => ['type' => 'DATETIME', 'null' => true],
])->addUniqueKey(['team_id', 'person_id'])->createTable('team_members', true);
// Database Type test table
// missing types:
// TINYINT,MEDIUMINT,BIT,YEAR,BINARY,VARBINARY,TINYTEXT,LONGTEXT,

View File

@ -107,6 +107,20 @@ class CITestSeeder extends Seeder
'value' => 'ടൈപ്പ്',
],
],
'team_members' => [
[
'team_id' => 1,
'person_id' => 22,
'role' => 'member',
'status' => 'active',
],
[
'team_id' => 1,
'person_id' => 33,
'role' => 'mentor',
'status' => 'active',
],
],
'type_test' => [
[
'type_varchar' => 'test',

View File

@ -47,6 +47,7 @@ final class MetadataTest extends CIUnitTestCase
$prefix . 'user',
$prefix . 'job',
$prefix . 'misc',
$prefix . 'team_members',
$prefix . 'type_test',
$prefix . 'empty',
$prefix . 'secondary',

View File

@ -448,7 +448,7 @@ final class UpsertTest extends CIUnitTestCase
$this->assertSame('El Salvador', $data[4]->country);
}
public function testUpsertWithMatchingDataOnUniqueIndexandPrimaryKey(): void
public function testUpsertWithMatchingDataOnUniqueIndexAndPrimaryKey(): void
{
$data = [
'id' => 6,
@ -607,6 +607,36 @@ final class UpsertTest extends CIUnitTestCase
}
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9450
*/
public function testUpsertBatchCompositeUniqueIndex(): void
{
$data = [
[
'team_id' => 1,
'person_id' => 22,
'role' => 'leader',
'status' => 'active',
],
[
'team_id' => 1,
'person_id' => 33,
'role' => 'member',
'status' => 'active',
],
];
// uses (team_id, person_id) - composite unique index
$this->db->table('team_members')->upsertBatch($data);
$this->seeInDatabase('team_members', ['team_id' => 1, 'person_id' => 22, 'role' => 'leader']);
$this->dontSeeInDatabase('team_members', ['team_id' => 1, 'person_id' => 22, 'role' => 'member']);
$this->seeInDatabase('team_members', ['team_id' => 1, 'person_id' => 33, 'role' => 'member']);
$this->dontSeeInDatabase('team_members', ['team_id' => 1, 'person_id' => 33, 'role' => 'mentor']);
}
public function testSetBatchOneRow(): void
{
$data = [

View File

@ -32,6 +32,7 @@ Bugs Fixed
- **CURLRequest:** Fixed an issue where multiple header sections appeared in the CURL response body during multiple redirects from the target server.
- **Cors:** Fixed a bug in the Cors filter that caused the appropriate headers to not be added when another filter returned a response object in the ``before`` filter.
- **Database:** Fixed a bug in ``Postgre`` and ``SQLite3`` handlers where composite unique keys were not fully taken into account for ``upsert`` type of queries.
See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

View File

@ -937,6 +937,10 @@ constraint by default. Here is an example using an array:
.. literalinclude:: query_builder/112.php
.. note:: For databases other than MySQL, if a table has multiple keys (primary or unique),
the primary key will be prioritized by default when handling constraints. If you prefer
to use a different unique key instead of the primary key, use the ``onConstraint()`` method.
The first parameter is an associative array of values.
Here is an example using an object: