Skip to content

Commit 6b23949

Browse files
committed
fix: add a primary key to an existing table
1 parent 155859b commit 6b23949

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

system/Database/Forge.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,12 +1084,10 @@ public function processIndexes(string $table): bool
10841084

10851085
if (! empty($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 (! empty($this->primaryKeys)) {
1090+
$sqls[] = $this->_processPrimaryKeys($table, true);
10931091
}
10941092

10951093
$this->foreignKeys = $fk;

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
@@ -42,6 +42,8 @@ Bugs Fixed
4242
Page Not Found.
4343
- **Spark:** Fixed a bug that caused spark to not display exceptions in the
4444
production mode or to display backtrace in json when an exception occurred.
45+
- **Forge:** Fixed a bug where adding a Primary Key to an existing table was
46+
ignored if there were no other Keys added too.
4547

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

0 commit comments

Comments
 (0)