Skip to content

Commit 6a50bb3

Browse files
committed
feat: support auto increment on a composite pk
1 parent fba7f06 commit 6a50bb3

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/Codeception/Module/Db.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,8 @@ protected function loadDumpUsingDriver(string $databaseKey): void
759759
}
760760

761761
/**
762-
* Inserts an SQL record into a database. This record will be erased after the test,
763-
* unless you've configured "skip_cleanup_if_failed", and the test fails.
762+
* Inserts an SQL record into a database. This record will be erased after the test,
763+
* unless you've configured "skip_cleanup_if_failed", and the test fails.
764764
*
765765
* ```php
766766
* <?php
@@ -802,12 +802,14 @@ private function addInsertedRow(string $table, array $row, $id): void
802802
$primary = [];
803803
if ($primaryKey !== []) {
804804
$filledKeys = array_intersect($primaryKey, array_keys($row));
805-
$primaryKeyIsFilled = count($filledKeys) === count($primaryKey);
805+
$missingPrimaryKeyColumns = array_diff_key($primaryKey, $filledKeys);
806806

807-
if ($primaryKeyIsFilled) {
807+
if (count($missingPrimaryKeyColumns) === 0) {
808808
$primary = array_intersect_key($row, array_flip($primaryKey));
809-
} elseif ($id && count($primaryKey) === 1) {
810-
$primary[$primaryKey[0]] = $id;
809+
} elseif (count($missingPrimaryKeyColumns) === 1) {
810+
$primary = array_intersect_key($row, array_flip($primaryKey));
811+
$missingColumn = reset($missingPrimaryKeyColumns);
812+
$primary[$missingColumn] = $id;
811813
} else {
812814
foreach ($primaryKey as $column) {
813815
if (isset($row[$column])) {

tests/data/dumps/mysql.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ CREATE INDEX counter ON `auto_increment_not_on_pk` (counter);
104104
ALTER TABLE `auto_increment_not_on_pk`
105105
MODIFY counter int AUTO_INCREMENT;
106106

107+
108+
CREATE TABLE `auto_increment_on_composite_pk` (
109+
`id` int(11) NOT NULL,
110+
`counter` int(11) AUTO_INCREMENT NOT NULL,
111+
PRIMARY KEY (`id`, `counter`)
112+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
113+
107114
CREATE TABLE `empty_table` (
108115
`id` int(11) NOT NULL AUTO_INCREMENT,
109116
`field` varchar(255),

tests/unit/Codeception/Module/Db/MySqlDbTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testConnectionIsResetOnEveryTestWhenReconnectIsTrue()
5151

5252
// Simulate a test that runs
5353
$this->module->_before($testCase1);
54-
54+
5555
$connection1 = $this->module->dbh->query('SELECT CONNECTION_ID()')->fetch(PDO::FETCH_COLUMN);
5656
$this->module->_after($testCase1);
5757

@@ -83,7 +83,7 @@ public function testInitialQueriesAreExecuted()
8383
];
8484
$this->module->_reconfigure($config);
8585
$this->module->_before(Stub::makeEmpty(TestInterface::class));
86-
86+
8787
$usedDatabaseName = $this->module->dbh->query('SELECT DATABASE();')->fetch(PDO::FETCH_COLUMN);
8888

8989
$this->assertSame($dbName, $usedDatabaseName);
@@ -113,4 +113,16 @@ public function testHaveInDatabaseAutoIncrementOnANonPrimaryKey()
113113

114114
$this->module->dontSeeInDatabase('auto_increment_not_on_pk', $testData);
115115
}
116+
117+
public function testHaveInDatabaseAutoIncrementOnCompositePrimaryKey()
118+
{
119+
$testData = [
120+
'id' => 777,
121+
];
122+
$this->module->haveInDatabase('auto_increment_on_composite_pk', $testData);
123+
$this->module->seeInDatabase('auto_increment_on_composite_pk', $testData);
124+
$this->module->_after(Stub::makeEmpty(TestInterface::class));
125+
126+
$this->module->dontSeeInDatabase('auto_increment_on_composite_pk', $testData);
127+
}
116128
}

0 commit comments

Comments
 (0)