Skip to content

Commit 7eec987

Browse files
committed
Fixed primary composite key detection
1 parent 5179b16 commit 7eec987

File tree

9 files changed

+32
-24
lines changed

9 files changed

+32
-24
lines changed

Tests/Unit/MigrationTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public function testDropAddPrimaryKey() : void
114114
$table = 'invoice';
115115
$fields = \PHPFUI\ORM::describeTable($table);
116116
$fieldName = $table . '_id';
117-
$this->assertTrue($fields[$fieldName]->primaryKey);
118117
$this->assertTrue($fields[$fieldName]->autoIncrement);
119118
$this->assertFalse($fields[$fieldName]->nullable);
120119

@@ -126,34 +125,31 @@ public function testDropAddPrimaryKey() : void
126125
}
127126

128127
$migration = new \Tests\Fixtures\MigrationWrapper();
129-
$migration->dropPrimaryKeyTest($table);
128+
$this->assertTrue($migration->dropPrimaryKeyTest($table));
130129
$migration->executeAlters();
131130
$this->assertEquals('', \PHPFUI\ORM::getLastError());
132131

133132
$fields = \PHPFUI\ORM::describeTable($table);
134-
$this->assertFalse($fields[$fieldName]->primaryKey);
135133
$this->assertFalse($fields[$fieldName]->autoIncrement);
136134
$this->assertFalse($fields[$fieldName]->nullable);
137135

138-
$migration->addPrimaryKeyTest($table, [$fieldName]);
136+
$this->assertTrue($migration->addPrimaryKeyTest($table, [$fieldName]));
139137
$migration->executeAlters();
140138
$this->assertEquals('', \PHPFUI\ORM::getLastError());
141139

142140
$fields = \PHPFUI\ORM::describeTable($table);
143-
$this->assertTrue($fields[$fieldName]->primaryKey);
144141
$this->assertFalse($fields[$fieldName]->autoIncrement);
145142
$this->assertFalse($fields[$fieldName]->nullable);
146143

147-
$migration->dropPrimaryKeyTest($table);
144+
$this->assertTrue($migration->dropPrimaryKeyTest($table));
148145
$migration->executeAlters();
149146
$this->assertEquals('', \PHPFUI\ORM::getLastError());
150147

151-
$migration->addPrimaryKeyAutoIncrementTest($table);
148+
$this->assertTrue($migration->addPrimaryKeyAutoIncrementTest($table));
152149
$migration->executeAlters();
153150
$this->assertEquals('', \PHPFUI\ORM::getLastError());
154151

155152
$fields = \PHPFUI\ORM::describeTable($table);
156-
$this->assertTrue($fields[$fieldName]->primaryKey);
157153
$this->assertTrue($fields[$fieldName]->autoIncrement);
158154
$this->assertFalse($fields[$fieldName]->nullable);
159155
}
@@ -187,7 +183,6 @@ public function testFields() : void
187183

188184
$this->assertArrayHasKey('string_record_id', $fields);
189185
$this->assertTrue($fields['string_record_id']->autoIncrement);
190-
$this->assertTrue($fields['string_record_id']->primaryKey);
191186
$this->assertFalse($fields['string_record_id']->nullable);
192187
$this->assertEquals('int', \substr($fields['string_record_id']->type, 0, 3)); // ignore precision
193188

Tests/Unit/MiscellaneousTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ enum TestEnum : int
1010

1111
class MiscellaneousTest extends \PHPUnit\Framework\TestCase
1212
{
13+
public function testCompositePrimaryKey() : void
14+
{
15+
$keys = \Tests\App\Record\EmployeePrivilege::getPrimaryKeys();
16+
17+
$this->assertEquals(['employee_id', 'privilege_id', ], $keys);
18+
19+
$keys = \Tests\App\Record\Order::getPrimaryKeys();
20+
21+
$this->assertEquals(['order_id', ], $keys);
22+
}
23+
1324
public function testEnumCondition() : void
1425
{
1526
$condition = new \PHPFUI\ORM\Condition('field', TestEnum::YES);

northwind/pgsql/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ CREATE TABLE IF NOT EXISTS "employee_privilege" (
108108
FOREIGN KEY ("privilege_id")
109109
REFERENCES "privilege" ("privilege_id")
110110
ON DELETE NO ACTION
111-
ON UPDATE NO ACTION);
111+
ON UPDATE NO ACTION,
112+
PRIMARY KEY (`employee_id`,`privilege_id`));
112113

113114
CREATE INDEX "employee_privilege_employee_id" ON "employee_privilege" ("employee_id");
114115
CREATE INDEX "employee_privilege_privilege_id" ON "employee_privilege" ("privilege_id");

northwind/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ CREATE TABLE IF NOT EXISTS `employee_privilege` (
8989
FOREIGN KEY (`privilege_id`)
9090
REFERENCES `privilege` (`privilege_id`)
9191
ON DELETE NO ACTION
92-
ON UPDATE NO ACTION) ENGINE=InnoDB;
92+
ON UPDATE NO ACTION,
93+
PRIMARY KEY (`employee_id`,`privilege_id`)) ENGINE=InnoDB;
9394

9495
CREATE INDEX `employee_privilege_employee_id` ON `employee_privilege` (`employee_id`);
9596
CREATE INDEX `employee_privilege_privilege_id` ON `employee_privilege` (`privilege_id`);

northwind/sqlite/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ CREATE TABLE IF NOT EXISTS `employee_privilege` (
8585
FOREIGN KEY (`privilege_id`)
8686
REFERENCES `privilege` (`privilege_id`)
8787
ON DELETE NO ACTION
88-
ON UPDATE NO ACTION);
88+
ON UPDATE NO ACTION,
89+
PRIMARY KEY (`employee_id`,`privilege_id`));
8990

9091
CREATE INDEX `employee_privilege_employee_id` ON `employee_privilege` (`employee_id`);
9192
CREATE INDEX `employee_privilege_privilege_id` ON `employee_privilege` (`privilege_id`);

src/PHPFUI/ORM.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function commit() : bool
6363
}
6464

6565
/**
66-
* @return array<\PHPFUI\ORM\Schema\Field>
66+
* @return array<string, \PHPFUI\ORM\Schema\Field>
6767
*/
6868
public static function describeTable(string $table) : array
6969
{
@@ -159,7 +159,7 @@ public static function getDataObjectCursor(string $sql = 'select 0 limit 0', arr
159159
}
160160

161161
/**
162-
* @return array<\PHPFUI\ORM\Schema\ForeignKey>
162+
* @return array<string, \PHPFUI\ORM\Schema\ForeignKey>
163163
*/
164164
public static function getForeignKeys(string $table) : array
165165
{

src/PHPFUI/ORM/Migration.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,16 @@ protected function dropIndex(string $table, string | array $fields) : bool
405405
protected function dropPrimaryKey(string $table) : bool
406406
{
407407
$fields = \PHPFUI\ORM::describeTable($table);
408+
$indexes = \PHPFUI\ORM::getIndexes($table);
408409

409-
foreach ($fields as $field)
410+
foreach ($indexes as $index)
410411
{
411-
if ($field->primaryKey)
412+
if ($index->primaryKey)
412413
{
413414
$sql = 'alter table ' . $table;
414415

416+
$field = $fields[$index->name];
417+
415418
if ($field->autoIncrement)
416419
{
417420
$nullable = $field->nullable ? '' : 'NOT NULL';

src/PHPFUI/ORM/PDOInstance.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function clearErrors() : void
5454
}
5555

5656
/**
57-
* @return array<\PHPFUI\ORM\Schema\Field>
57+
* @return array<string, \PHPFUI\ORM\Schema\Field>
5858
*/
5959
public function describeTable(string $table) : array
6060
{
@@ -204,7 +204,7 @@ public function getDSN() : string
204204
}
205205

206206
/**
207-
* @return array<\PHPFUI\ORM\Schema\ForeignKey>
207+
* @return array<string, \PHPFUI\ORM\Schema\ForeignKey>
208208
*/
209209
public function getForeignKeys(string $table) : array
210210
{
@@ -327,11 +327,7 @@ public function getIndexes(string $table) : array
327327
foreach ($rows as $row)
328328
{
329329
$index = new \PHPFUI\ORM\Schema\Index($this, $row);
330-
331-
if ('PRIMARY' !== $index->keyName)
332-
{
333-
$fields[$index->keyName] = $index;
334-
}
330+
$fields[] = $index;
335331
}
336332

337333
return $fields;

src/PHPFUI/ORM/Schema/Field.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(\PHPFUI\ORM\PDOInstance $pdo, array $fields, bool $a
4646
{
4747
$this->defaultValue = 'CURRENT_TIMESTAMP';
4848
}
49-
$this->primaryKey = 'PRI' === ($fields['Key'] ?? ''); // use indexes to find primary keys
49+
$this->primaryKey = false;
5050
$this->autoIncrement = \str_contains($fields['Extra'], 'auto_increment');
5151
$this->extra = \str_replace('auto_increment', '', $fields['Extra']);
5252

0 commit comments

Comments
 (0)