Skip to content

Commit ce62326

Browse files
authored
Merge pull request #8031 from michalsn/fix/processIndexes
fix: add a primary key to an existing table
2 parents 07d3ef8 + c61d8e7 commit ce62326

File tree

4 files changed

+90
-9
lines changed

4 files changed

+90
-9
lines changed

phpstan-baseline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@
12381238
];
12391239
$ignoreErrors[] = [
12401240
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
1241-
'count' => 16,
1241+
'count' => 13,
12421242
'path' => __DIR__ . '/system/Database/Forge.php',
12431243
];
12441244
$ignoreErrors[] = [

system/Database/Forge.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ public function processIndexes(string $table): bool
10731073
$sqls = [];
10741074
$fk = $this->foreignKeys;
10751075

1076-
if (empty($this->fields)) {
1076+
if ($this->fields === []) {
10771077
$this->fields = array_flip(array_map(
10781078
static fn ($columnName) => $columnName->name,
10791079
$this->db->getFieldData($this->db->DBPrefix . $table)
@@ -1082,20 +1082,18 @@ public function processIndexes(string $table): bool
10821082

10831083
$fields = $this->fields;
10841084

1085-
if (! empty($this->keys)) {
1085+
if ($this->keys !== []) {
10861086
$sqls = $this->_processIndexes($this->db->DBPrefix . $table, true);
1087+
}
10871088

1088-
$pk = $this->_processPrimaryKeys($table, true);
1089-
1090-
if ($pk !== '') {
1091-
$sqls[] = $pk;
1092-
}
1089+
if ($this->primaryKeys !== []) {
1090+
$sqls[] = $this->_processPrimaryKeys($table, true);
10931091
}
10941092

10951093
$this->foreignKeys = $fk;
10961094
$this->fields = $fields;
10971095

1098-
if (! empty($this->foreignKeys)) {
1096+
if ($this->foreignKeys !== []) {
10991097
$sqls = array_merge($sqls, $this->_processForeignKeys($table, true));
11001098
}
11011099

tests/system/Database/Live/ForgeTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,87 @@ public function testProcessIndexes(): void
16571657
$this->forge->dropTable('user2', true);
16581658
}
16591659

1660+
public function testProcessIndexesWithKeyOnly(): void
1661+
{
1662+
// make sure tables don't exist
1663+
$this->forge->dropTable('actions', true);
1664+
1665+
$this->createActionsTable();
1666+
$this->forge->addKey('name', false, false, 'db_actions_name');
1667+
1668+
// create indexes
1669+
$this->forge->processIndexes('actions');
1670+
1671+
// get a list of all indexes
1672+
$allIndexes = $this->db->getIndexData('actions');
1673+
1674+
// check that db_actions_name key exists
1675+
$indexes = array_filter(
1676+
$allIndexes,
1677+
static fn ($index) => ($index->name === 'db_actions_name')
1678+
&& ($index->fields === [0 => 'name'])
1679+
);
1680+
$this->assertCount(1, $indexes);
1681+
1682+
// drop tables to avoid any future conflicts
1683+
$this->forge->dropTable('actions', true);
1684+
}
1685+
1686+
public function testProcessIndexesWithPrimaryKeyOnly(): void
1687+
{
1688+
// make sure tables don't exist
1689+
$this->forge->dropTable('actions', true);
1690+
1691+
$this->createActionsTable();
1692+
$this->forge->addPrimaryKey('id');
1693+
1694+
// create indexes
1695+
$this->forge->processIndexes('actions');
1696+
1697+
// get a list of all indexes
1698+
$allIndexes = $this->db->getIndexData('actions');
1699+
1700+
// check that the primary key exists
1701+
$indexes = array_filter(
1702+
$allIndexes,
1703+
static fn ($index) => $index->type === 'PRIMARY'
1704+
);
1705+
$this->assertCount(1, $indexes);
1706+
1707+
// drop tables to avoid any future conflicts
1708+
$this->forge->dropTable('actions', true);
1709+
}
1710+
1711+
public function testProcessIndexesWithForeignKeyOnly(): void
1712+
{
1713+
// make sure tables don't exist
1714+
$this->forge->dropTable('actions', true);
1715+
$this->forge->dropTable('user2', true);
1716+
1717+
$this->createUser2TableWithKeys();
1718+
$this->populateUser2Table();
1719+
$this->createActionsTable();
1720+
1721+
// SQLite does not support custom foreign key name
1722+
if ($this->db->DBDriver === 'SQLite3') {
1723+
$this->forge->addForeignKey('userid', 'user', 'id');
1724+
$this->forge->addForeignKey('userid2', 'user2', 'id');
1725+
} else {
1726+
$this->forge->addForeignKey('userid', 'user', 'id', '', '', 'db_actions_userid_foreign');
1727+
$this->forge->addForeignKey('userid2', 'user2', 'id', '', '', 'db_actions_userid2_foreign');
1728+
}
1729+
1730+
// create indexes
1731+
$this->forge->processIndexes('actions');
1732+
1733+
// check that the two foreign keys exist
1734+
$this->assertCount(2, $this->db->getForeignKeyData('actions'));
1735+
1736+
// drop tables to avoid any future conflicts
1737+
$this->forge->dropTable('actions', true);
1738+
$this->forge->dropTable('user2', true);
1739+
}
1740+
16601741
private function createUser2TableWithKeys(): void
16611742
{
16621743
$fields = [

user_guide_src/source/changelogs/v4.4.2.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Bugs Fixed
4444
Page Not Found.
4545
- **Spark:** Fixed a bug that caused spark to not display exceptions in the
4646
production mode or to display backtrace in json when an exception occurred.
47+
- **Forge:** Fixed a bug where adding a Primary Key to an existing table was
48+
ignored if there were no other Keys added too.
4749

4850
See the repo's
4951
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)